Deploy a LAMP stack as a Helm Chart

Written by asadfaizi | Published 2022/04/12
Tech Story Tags: cloud-computing | kubernetes | gitops | devops | containers | service-mesh | cloud | docker

TLDRKubernetes deployments are becoming more complex, with more and more applications shifting to microservices and event-driven design patterns. Helm Charts provides users a simplified way of deploying applications across a Kubernees cluster. Helm charts can be versioned and published in a repository like Artifacthub.io and utilized to install applications in any Kubernes cluster. For proprietary applications, users can provision private repositories using helm charts across their test and production environments. It eliminates the need to copy-paste deployments or configurations.via the TL;DR App

Deploying applications to Kubernetes can be a complex process, as even the simplest applications contain multiple containers or Pods. Kubernetes deployments are becoming even more complex, with more and more applications shifting to decoupled architectures using microservices and event-driven design patterns.

Helm is a good solution to bridge this deployment complexity and package Kubernetes applications with all dependencies, including other containers/Pods, services, secrets, ConfigMaps, etc. Moreover, helm provides users a simplified way of deploying applications across a Kubernetes cluster. In this post, we will see how to deploy a LAMP stack in a K8s as a Helm Chart.

Why Helm Charts?

Helm acts as a package manager for Kubernetes. Similar to traditional package managers like apt, yum, packman, and widget, which allow users to simply search, install, upgrade, and delete software on their targeted operating systems, helm aims to provide the same functionality in a Kubernetes environment.

Helm utilizes Helm Charts to package Kubernetes applications. These charts can then be versioned and published in a repository like Artifacthub.io and utilized to install applications in any Kubernetes cluster. Since helm charts are versioned, updating applications is as simple as running the newest version of the chart. There’s no need to change manifest files manually to update applications. For proprietary applications, users can provision private repositories using helm charts across their test and production environments. It eliminates the need to copy-paste deployments or configurations.

Traditional LAMP stack Deployment

Before moving into using Helm charts, let’s see how to normally deploy a LAMP stack in a Kubernetes cluster. A LAMP stack consists of Apache, MySQL, and PHP. Thus, users will have to manage at least two deployments as one for a container consisting of a database (MySQL) and the other for the webserver with PHP (Apache). Before that, you will need to set up Secrets, storage volumes, ConfigMaps, etc., and then create a deployment file and deploy the application. Following is an example deployment file for MySQL with a secret for the root password, persistent volume, and service.

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:8.0.28
        name: mysql
        env:
        - name: MYSQL_DATABASE
          value: web_db
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password.txt
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

You will need to create another deployment manifest for Apache and PHP. When it’s time to update the deployment, you will need to modify the files and redeploy them again. If you have deployed them in multiple clusters, you will need to change the deployment manifest in each cluster and redeploy. This approach has its own risks since an error in a modification can completely break an application. There is also a high chance of creating an incorrect configuration if manifests are constantly changed and clusters are managed by multiple parties.

LAMP stack using a Helm Chart

Since LAMP consists of common applications, you can simply search for the appropriate charts within a public repository like Artifacthub.io (The default option) and directly install charts. It can be done either via the command line or by visiting the site and searching.

Searching For Helm Charts

Simply run the search command with the name of the repository and chart to search via the CLI. Here we have searched for an apache chart.

helm search hub apache

Installing Helm Charts

Once you find the appropriate charts, the next step is to install them. In this example, we will use verified charts from a well-known publisher, bitnami, to provision Apache and MySQL within the cluster using the provided helm charts.

helm repo add bitnami https://charts.bitnami.com/bitnami
helm install k8s-apache --set imagePullPolicy=Always bitnami/apache
helm install k8s-mysql --set auth.rootPassword=testpassword123,auth.database=lamp_database bitnami/mysql

The above commands will install apache and MySQL within our Kubernetes cluster. We have provided the imagePullPolicy parameter for the Apache chart to always download the container image and set the root password and the database for MySQL. Even though there is a misconception that charts cannot be customized, it is not true. However, the values that can be modified will depend on the configuration of the chart. These bitnami charts allow users to change almost all the configurations for their respective containers.

Apache Install

MySQL Install

We can use the get all command to verify if the appropriate resources are created in our cluster.

kubectl get all

Uninstall the Charts

If you need to uninstall the charts, simply run the helm uninstall command to remove these applications from the cluster.

helm uninstall k8s-apache k8s-mysql

Installing a Complete LAMP Stack

In the previous section, we covered how to create a LAMP stack by installing individual helm charts for the different components of the stack without creating any manifest files or configurations. We can further simplify this process by looking for a chart that consists of a complete LAMP stack.

Step 1 - Search for a chart that consists of a LAMP stack

helm search hub lamp

Step 2 - Install the appropriate chart using the following command

helm repo add lamp https://lead4good.github.io/lamp-helm-repository
helm install k8s-lamp lamp/lamp

Step 1 - Verify the installation

kubectl get all

That’s it, and you have successfully created a LAMP stack in Kubernetes using a single Helm Chart.

Conclusion

Helm is a powerful tool to manage applications within a Kubernetes cluster, simplifying deployments and updates. However, there is one pain point in using helm. The users will be responsible for creating and maintaining these helm charts, which can be a complex task, especially when it comes to custom applications. A Tool like Cloudplex aims to solve this problem by offering features like automated Helm chart generation and managed deployments for cloud-native containerized applications, all within a low code solution. So why not give the free account of Cloudplex a try the next time you create a Helm chart for your K8s application.

Asad Faizi

Founder CEO

CloudPlex.io, Inc

asad@cloudplex.io

Also published here.


Written by asadfaizi | Founder and CEO @ CloudPlex.io | Entrepreneur | Technologist | Mad Cloud Scientist
Published by HackerNoon on 2022/04/12