Join our Discord Server
Abraham Dahunsi Web Developer 🌐 | Technical Writer ✍️| DevOps Enthusiast👨‍💻 | Python🐍 |

How to Deploy Web Applications on Kubernetes with Helm

5 min read

Deploying web applications efficiently is crucial for maintaining performance, scalability, and reliability. Kubernetes and Helm are powerful tools that simplify this process. Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. Helm, on the other hand, is a package manager for Kubernetes that streamlines the deployment process by using charts to define, configure, and manage applications.

Why Efficient Deployment Matters

Efficient deployment ensures that your web applications run smoothly, scale seamlessly, and minimize downtime. Whether you’re deploying a small microservice or a complex application, Kubernetes and Helm can play a crucial role in achieving reliability and agility.

This article explains how to deploy web applications on Kubernetes using Helm.

Prerequisites

Before you begin:

Set Up Your Environment

Before deploying your web application with Kubernetes and Helm, you need to set up your environment. This involves installing Helm and Kubernetes, and setting up a Kubernetes cluster.

Install Helm and Kubernetes (Optional)

Helm can be installed on various operating systems. Use the following commands to install it on Ubuntu.

curl https://baltocdn.com/helm/signing.asc | sudo apt-key add -
sudo apt-get install apt-transport-https --yes
echo "deb https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
sudo apt-get update
sudo apt-get install helm

Verify the installation by running.

helm version

Kubernetes can be installed using various methods. For a local setup, Minikube is a popular choice.

Here’s how to install Minikube on Ubuntu.

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

Verify the installation by running.

minikube version

For cloud options:

Set Up a Kubernetes Cluster

Using Minikube

Start a local Kubernetes cluster with Minikube.

minikube start

Check the status of your cluster:

kubectl cluster-info

Configure kubectl

kubectl is the command-line tool for interacting with your Kubernetes cluster. Follow the steps below to install and configure kubectl.

sudo apt-get update && sudo apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubectl

Verify the installation by running.

kubectl version --client

Creating a Helm Chart

Helm charts are the cornerstone of deploying applications with Helm. They package all the Kubernetes resources required for an application into a single, reusable unit.

Structure of a Helm Chart

A Helm chart consists of several files and directories organized in a specific structure. Here’s a typical layout:

my-web-app/
  ├── charts/
  ├── templates/
  │   ├── deployment.yaml
  │   ├── service.yaml
  │   └── _helpers.tpl
  ├── Chart.yaml
  ├── values.yaml
  └── README.md
  • charts/: This directory can contain sub-charts, which are dependencies for your main chart.
  • templates/: This directory contains Kubernetes manifest templates. These templates are rendered into Kubernetes manifests using the values provided.
    • deployment.yaml: Defines the deployment configuration for your application.
    • service.yaml: Defines the service configuration to expose your application.
    • _helpers.tpl: Contains helper templates that can be used across other templates.
  • Chart.yaml: The main descriptor file for the chart. It includes metadata such as the chart name, version, and description.
  • values.yaml: The default configuration values for the chart. These values can be overridden during installation.
  • README.md: Optional file to provide information about the chart.

Create a Basic Helm Chart

Create a basic Helm chart for a web application. We’ll use Nginx.

  1. Create a New Helm Chart.
helm create my-web-app

This command generates a new Helm chart with the default structure.

  1. Customize the Chart.

Chart.yaml

apiVersion: v2
name: my-web-app
description: A Helm chart for deploying a web application
version: 0.1.0

values.yaml

replicaCount: 2

image:
  repository: nginx
  tag: stable
  pullPolicy: IfNotPresent

service:
  type: LoadBalancer
  port: 80

ingress:
  enabled: false

resources: {}

nodeSelector: {}

tolerations: []

affinity: {}

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}-nginx
  labels:
    app: {{ .Release.Name }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}
    spec:
      containers:
        - name: nginx
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - containerPort: 80

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: {{ .Release.Name }}-nginx
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: 80
  selector:
    app: {{ .Release.Name }}
  1. Deploy the Chart

Use Helm to deploy your chart to the Kubernetes cluster.

helm install my-web-app ./my-web-app

Verify the deployment.

kubectl get all

Customizing Your Deployment

Helm charts are highly customizable, allowing you to tailor deployments to your specific needs. Follow the steps below to modify values in the Helm chart and leverage Helm’s templating capabilities for dynamic configuration.

Modify the Values in the Helm Chart

Helm charts use a values.yaml file to define default configuration values. These values can be overridden during deployment to customize the behavior of your application.

  1. Change the service port to modify the service.port value in the values.yaml file.
service:
  type: LoadBalancer
  port: 8080
  1. Adjust the number of replicas (instances) of your application by modifying the replicaCount value.
replicaCount: 3
  1. You can define environment variables for your application in the values.yaml file and reference them in your deployment template.
env:
  - name: ENV_VAR_NAME
    value: "value"

In the deployment.yaml template, use the following to inject these environment variables.

spec:
  containers:
    - name: my-container
      image: my-image
      env:
        - name: ENV_VAR_NAME
          value: {{ .Values.env[0].value }}

Helm’s Templating Capabilities

Helm uses the Go templating engine to render Kubernetes manifests dynamically. This allows for powerful and flexible configurations.

  1. Helm provides a variety of built-in functions to manipulate strings, numbers, lists, and more. For example, you can use the default function to provide a default value if a variable is not set.
image:
  repository: nginx
  tag: {{ .Values.image.tag | default "latest" }}
  1. You can use conditional statements to include or exclude parts of the template based on values.
{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ .Release.Name }}-ingress
spec:
  rules:
    - host: {{ .Values.ingress.host }}
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: {{ .Release.Name }}-service
                port:
                  number: {{ .Values.service.port }}
{{- end }}
  1. You can iterate over lists or maps using loops.
spec:
  containers:
    - name: my-container
      image: my-image
      ports:
        {{- range .Values.ports }}
        - containerPort: {{ . }}
        {{- end }}

Example: Customizing Deployment with Values

Here’s an example of a values.yaml file with custom configurations.

replicaCount: 3

image:
  repository: nginx
  tag: stable
  pullPolicy: IfNotPresent

service:
  type: LoadBalancer
  port: 8080

env:
  - name: ENV_VAR_NAME
    value: "value"

ingress:
  enabled: true
  host: my-app.example.com

Deploying Your Web Application

With your Helm chart ready and customized, it’s time to deploy your web application to the Kubernetes cluster. Follow the steps below, using Helm to install your application, verifying the deployment status, and accessing the application.

Use Helm to Install Your Application

  1. Initialize Helm (if not already done)
helm repo add stable https://charts.helm.sh/stable
helm repo update
  1. Install the Helm Chart

Navigate to the directory containing your Helm chart and run the following command.

helm install my-web-app ./my-web-app
  • my-web-app is the release name for your deployment.
  • ./my-web-app is the path to your Helm chart.
  1. Override Default Values (Optional)

To override any default values in the values.yaml file, use the --set flag.

helm install my-web-app ./my-web-app --set service.port=8080 --set replicaCount=3

Verify the Deployment Status

After installing the Helm chart, verify the deployment status using kubectl commands.

  1. Check All Resources
kubectl get all

This command lists all the resources created by the Helm chart, including pods, services, and deployments.

  1. Check Pod Status
kubectl get pods

Ensure that all pods are running and in a healthy state.

  1. Describe the Deployment
kubectl describe deployment my-web-app-nginx

This command provides detailed information about the deployment, including events and any potential issues.

Access the Application

Depending on the service type defined in your Helm chart, you can access your web application in different ways:

  1. LoadBalancer Service

If you have configured a LoadBalancer service, you can get the external IP address using.

kubectl get services

Expected output:

NAME                    TYPE                CLUSTER-IP      EXTERNAL-IP     PORT(S)                AGE
my-web-app-nginx LoadBalancer   10.96.0.1            34.123.45.67        8080:32000/TCP    5m

Open your browser and navigate to http://34.123.45.67:8080 to access your web application.

  1. NodePort Service

If you have configured a NodePort service, you can access the application using the node’s IP address and the NodePort.

minikube service my-web-app-nginx --url

This command provides the URL to access the application when using Minikube.

  1. Ingress

If you have configured an Ingress resource, ensure that your Ingress controller is running and configured correctly. Access the application using the specified host and path.

Conclusion

In this article, you have learned how to deploy web applications on Kubernetes using Helm. You have set up your environment, created a Helm chart, customized your deployment, and successfully deployed your application to a Kubernetes cluster. Helm and Kubernetes provide a powerful combination for managing and scaling your applications efficiently. For more information and advanced configurations, please visit the official Helm and Kubernetes documentation.

Have Queries? Join https://launchpass.com/collabnix

Abraham Dahunsi Web Developer 🌐 | Technical Writer ✍️| DevOps Enthusiast👨‍💻 | Python🐍 |
Join our Discord Server
Index