How to deploy a Django Application using Helm

data codes through eyeglasses
Reading Time: 3 minutes

Hello Readers, In this we are going to create a helm chart to deploy a Django application. Before you start you must a django application, kubernetes and a helm installed and configured on your system.

What is Python Django Application?

Django is a high-level Python web framework that enables the rapid development of secure and maintainable websites. Built by experienced developers, Django handles much of your web development so you can focus on creating your app without having to reinvent the wheel.

What is a helm chart?

Helm uses a package format called Charts. A chart is a collection of files that describe a set of related Kubernetes resources. You can use a single chart to deploy something as simple as a memcache pod or something as complex as a complete web app stack with HTTP servers, databases, caches, and more.

For helm installation, you can refer URL

Let’s create a skeleton Helm application, use the CLI to do this:

helm create <chart>

helm create cockpitapp

Now you should have a new directory called <chart-name> , which is the same as what you asked Helm to create for you in the previous step.

Now you want to update the values.yaml file and replace a couple of things in the image and service sections:

deploy:

  replicas: 2

  image: 

    name:  <image-name>

    tag: <image-tag>

  imagePullPolicy: IfNotPresent
 
  port: 80

  containerName: <container-name>

service:

  type: NodePort

  port: 8000

  targetPort: 8000

  nodePort: 31009

  name: traffic

So minus all the comments, your values.yaml should now look something like this

In the template section, you will get a deployment.yaml and service.yaml files which is look like this:

{{- with .Values.deploy }}
apiVersion: apps/v1
kind: Deployment
metadata:
    name: {{ $.Release.Name }}-deploy
spec:
    replicas: {{ .replicas }}
    selector:
        matchLabels:
            app: {{ $.Release.Name }}
    {{- if .strategy.custom }}
    strategy: 
   {{- include "strategy" . | indent 6}}
    {{- end }}
    template:
      metadata:
        labels:
          app: {{ $.Release.Name }}
      spec:
       {{- include "imagePullSecrets" . | indent 8 }}
        containers:
        - image: {{ .image.name }}:{{ .image.tag }}
          name: {{ .containerName }}
          imagePullPolicy: {{ .imagePullPolicy }}
          ports:
          - containerPort: {{ .port }}
          env:
          {{- range .env }}
          - name: {{ .name }}
            value: {{ .value | quote }}
          {{- end }}
{{ end }}
apiVersion: v1
kind: Service
metadata:
  name: {{ .Release.Name }}-service
spec:
  {{- with .Values.service }}
  type: {{ default "ClusterIP" .type }}
  ports:
    - name: {{ .name }}
      targetPort: {{ .targetPort }}
      port: {{ .port }}
      {{- include "serviceType" $ | indent 6 }}    
  selector:
    app: {{ $.Release.Name }}
    {{- end }}

Now in the charts.yaml, you can provide some values which are:

apiVersion: v2
name: cockpitapp
description: cockpitapp deployment and nodePort service
appVersion: 1.0.0
version: 1.0.0

After all the changes, you can validate the templates by using the command:

helm template cockpitapp

The Helm template command is one of the useful commands which can be used to verify your Helm Chart before deploying them into the Kubernetes cluster.

After the validation is done, run the command to deploy the chart:

helm install <release-name> <chart-name>
helm install cockpitapp cockpitapp

Now, that your deployment is complete you can check it via command:

kubectl get deployment
kubectl get svc

Now using your web browser, you should be able to access your application.

Written by 

Ashi Dubey is a Software Intern at Knoldus Inc Software. She has a keen interest toward learning new technologies. Her practice area is Devops. When not working, you will find her with a Book.