Running Django Application On Kubernetes
Tags: docker, kubernetes, django, deepcodr
Published: 2024-06-03 19:16:31

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 -->
{% embed https://github.com/Deepcodr/django-hello-world %}

<hr>

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"
```
<br/>
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
```

> _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
```
<br/>
Done!✅ Build the docker image using below using Docker.

```
docker build -t deepcodr/node-hello-world .
```

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
```

Test the application thoroughly and then we are ready for deployment.
<br/>
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
```

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
```

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

```
kubectl get svc
kubectl get pods
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hht7lrjbqpdemjpp170h.png)
If you see a service and pods running. Your application should work 🎉
<br/>
Test the application by hitting URL in browser. You will see your homepage.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bu32sf620obmcusf7d5.png)

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.