How to Deploy a Django Application in Kubernetes (original) (raw)

Last Updated : 23 Jul, 2025

In this article, we will study how we can deploy Django web applications to Kubernetes. We will also see how to dockerize and build an image of the Django application using Dockerfile. For this article, you should know about setting up a VM in Azure. We will see how to deploy applications on Linux Server.

Introduction to Django

Django is a Python framework for developing web applications and APIs. It is most commonly used for Content Management Systems and APIs but it also supports microservices. Django is based on Python, which allows integrations with the most used Python libraries and AI/ML libraries.

Steps to Deploy Django Application in Kubernetes

Step 1: Create and set up a Django Project

Create a new Django project or use an existing one using django-admin. For this article, you can use the project from the below link

GitHub Link

Once the project is done, start the project and test it in the browser

python manage.py runserver

runserver

Hit the URL in the browser to get the desired output.

browser to get

If the project is working correctly, then freeze the pip modules in requirements.txt file.

pip freeze > "requirements.txt"

As Docker currently supports python version 3.9.19, make sure your requirements contains module versions according to python version.

**Step 2: Create Dockerfile and build Docker image

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

docker build -t /: .

docker build

docker run -dp : IMAGE-NAME

docker run

If everything is correct, you should be able to view the application in the browser. After you are satisfied with the output, kill the container.

Step 3: Create Deployment configuration and deploy to Kubernetes

Before going to Kubernetes, push the docker image to the docker Hub.

docker push

docker push

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

kubectl apply -f

apply

kubectl get svc

kubectl get pods

svc

Step 4: Test the application

oie_27173113JL8kqatJ