Running Django Application On Kubernetes

Deepak Patil - Jun 3 - - Dev Community

Imagine your Django application running in Kubernetes equipped with features of Kubernetes building a strong scalable and highly available application. In this post let's see how to dockerize and deploy a Django application on Kubernetes.

Deployment of an application always starts with having a nice and error-free application. To begin with, we will use a sample Django application with a sample view to provide Hello World! response.

<-- You can use your application or To follow along you can refer to the code from the below repo -->


Now that the application is ready, start with dockerizing and building an image. For this, we will create a requirements.txt of required modules.

pip freeze > "requirements.txt"
Enter fullscreen mode Exit fullscreen mode



Lets write a recipe for our docker image in Dockerfile. The minimal configuration for Dockerfile is provided below you can modify it accordingly.

FROM python:3.9.19-alpine
RUN mkdir /DjangoHelloWorld
WORKDIR /DjangoHelloWorld
COPY / .
RUN pip install -r requirements.txt
EXPOSE 8000
CMD python manage.py runserver 0.0.0.0:8000
Enter fullscreen mode Exit fullscreen mode

To use SQLite database you can simply add migrations and make migration commands in CMD at the last line

CMD python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000
Enter fullscreen mode Exit fullscreen mode



Done!✅ Build the docker image using below using Docker.

docker build -t deepcodr/node-hello-world .
Enter fullscreen mode Exit fullscreen mode

That should create image for our application. Spin up a container if you want to test the application.

docker run -dp 8000:8000 deepcodr/node-hello-world
Enter fullscreen mode Exit fullscreen mode

Test the application thoroughly and then we are ready for deployment.


Lets provide YAML configuration for Kubernetes Deployment and Service.

apiVersion : v1
kind : Service
metadata :
  name : djangohelloworld
spec :
  selector :
    app : djangohelloworld
  type : LoadBalancer
  ports :
    - port : 8000
      targetPort : 8000
---
apiVersion : apps/v1
kind : Deployment
metadata :
  name : djangohelloworld
spec :
  replicas : 1
  selector :
    matchLabels :
      app : djangohelloworld
  template :
    metadata :
      labels :
        app : djangohelloworld
    spec :
      containers :
      - name : djangohelloworld
        image : deepcodr/django-hello-world
        ports :
          - containerPort : 8000
        imagePullPolicy : Always
Enter fullscreen mode Exit fullscreen mode

The above configuration is sufficient to run our application with minimal configuration. Save the configuration and apply it with kubectl

kubectl apply -f nodehelloworld.yaml
Enter fullscreen mode Exit fullscreen mode

This should create a service and deployment in Kubernetes.Lets view running pods and services in cluster.

kubectl get svc
kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Image description
If you see a service and pods running. Your application should work 🎉


Test the application by hitting URL in browser. You will see your homepage.

Image description

Now that you know how to deploy Django application on Kubernetes 🗿, You are ready to build and configure more complex scenarios for Django with Kubernetes and explore Kubernetes in depth.

. . . . . . .