How to Deploy a Django Application in Kubernetes
Last Updated :
30 May, 2024
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

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

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
- Now create a new Dockerfile inside root folder of project where manage.py is located.
- The file will look like below.
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
- In above file, we have added python image version from which the image should be built. Then we added a new project directory and copied project data to that folder.
- Then we have installed all modules from requirements.txt and exposed port 8000.
- Modify the file according to your project requirements. Once the file is ready, we can build the image using below command.
docker build -t <REPOSITORY-NAME>/<IMAGE-NAME>:<TAG> .

- Wait for the build to be complete. If the build is successful, you can spin up a new container from image using below command.
docker run -dp <hostport>:<containerport> IMAGE-NAME

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 <IMAGE-NAME>

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
- We have added two set of configurations, one for service and other for deployment. We have specified kind of deployment and the metadata to be used.
- In the containers section, we have added port and image to be used by container. Similarly for service, we have added listening and forwarding port.
- Now apply the configuration using kubectl.
kubectl apply -f <FILE-NAME>

- Check if the service and pods are created and running successfully using below commands.
kubectl get svc
kubectl get pods

Step 4: Test the application
- Now that the service and pods are running successfully, we can use the localhost IP address to test the application.
- If you see the result page, then the deployment is successful.
Similar Reads
How to Deploy Java Application in Kubernetes?
Kubernetes is a platform for automating deployment, scaling, and management of containerized applications. Pods are basic units that hold one or more containers. Deployments manage application deployment and updates. Services provide stable endpoints for accessing pods. Ingress manages external acce
9 min read
Deploying a React Application in Kubernetes
Kubernetes is an open-source free manager for your computer program. These programs can be in the containers. Each container holds a program and everything it needs to run. To Keep track of all these containers that contain your application this is where the Kubernetes role comes in. Kubernetes does
5 min read
How To Deploy Python Application In Kubernetes ?
In today's IT world we are moving from the monolithic to microservice architecture to make our applications highly available and scalable to bring fault tolerance. In this transformation, containerization i.e., containerizing the application are a fundamental aspect of this micro services. In this a
6 min read
How to Deploy Node.js Application in Kubernetes?
Deploying the Node.js application on Kubernetes in a containerized manner makes it highly scalable,fault-tolerant, and allows for zero downtime. Kubernetes allows container orchestration that can be used in many ways during deployment. Load balancing is provided, which helps with automatic load tran
4 min read
Deploying A Node.js Application In kubernetes
Kubernetes, or K8s, is an open-sourced container orchestration technology that is used to automate the manual processes of deploying, managing, and scaling applications with the help of containers. Kubernetes was originally developed by engineers at Google, and In 2015, it was donated to CNCF (Cloud
9 min read
How to Deploy Spring Boot Application in Kubernetes ?
The Spring Boot framework provides an assortment of pre-configured templates and tools to make the development of Java-based applications simpler. With little configuration, it enables developers to quickly design production-ready, stand-alone apps. Kubernetes, commonly referred to as K8s, is an ope
7 min read
How to Deploy Django Application in AWS Lambda?
Pre-requisite: AWS , Python Django is a Python web framework that makes it easy to build web applications quickly and securely. It has a large and helpful community that provides support and contributes to its development. AWS Lambda is a serverless computing platform that runs your code in Docker c
7 min read
How To Deploy PHP Application On Kubernetes ?
Prologue to deploying a PHP applications on Kubernetes starts with understanding the containerization, where applications and their libraries are packaged into convenient, lightweight containers. Docker is generally utilized for this reason. Kubernetes then deals with these containers, abstracting a
9 min read
How to Deploy Django Application in AWS EC2?
In this article, we will study how we can deploy our existing Django web application to Windows Server in AWS EC2. We will also see how to use the public IP of the EC2 instance to access the Django application. For this article, you should know about setting up EC2 in AWS. We will see how to deploy
3 min read
How to Deploy Angular App in Kubernetes ?
In the modern world of web development, Angular has become one of the most popular frameworks for building dynamic and responsive web applications. As the demand for scalability and reliability increases, deploying these applications in a containerized environment using Kubernetes has become a commo
5 min read