Deploy a Django Application Using Jenkins, Docker, and Kubernetes

Harshitha G MHarshitha G M
3 min read

Deploying your Django app to Kubernetes might seem intimidating โ€” but with Jenkins, Docker, and Kubernetes, you can automate the entire process easily!

๐Ÿ”ง Prerequisites

Before you start, ensure you have:

  • A Django app hosted on GitHub

  • Jenkins installed and running (Dockerized or standalone)

  • Docker Hub account to push your Docker images

  • A Kubernetes cluster running (like Minikube, MicroK8s, or any cloud provider)

  • kubectl CLI configured to manage your Kubernetes cluster

๐Ÿ“‚ Step 1: Prepare Your Django Application

Make sure your Django project contains:

  • A Dockerfile to build your app container

  • A Kubernetes manifest file (e.g., k8s-deploy.yaml) describing your deployment and service

  • Your code pushed to a GitHub repository

Example Dockerfile

FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["gunicorn", "blog.wsgi:application", "--bind", "0.0.0.0:8000"]

Example k8s-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: django-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: django-blog
  template:
    metadata:
      labels:
        app: django-blog
    spec:
      containers:
      - name: django-container
        image: yourdockerhubusername/django-blog:latest
        ports:
        - containerPort: 8000

---
apiVersion: v1
kind: Service
metadata:
  name: django-service
spec:
  selector:
    app: django-blog
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000
  type: NodePort

๐Ÿค– Step 2: Setup Jenkins Pipeline for CI/CD

1. Run Jenkins (Docker example)

docker run -d --name jenkins -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts

2. Install Jenkins Plugins:

  • Git Plugin

  • Docker Pipeline

  • Kubernetes CLI Plugin

  • Credentials Binding Plugin

3. Add Credentials:

  • Add your Docker Hub credentials in Jenkins (Manage Jenkins โ†’ Credentials)

  • Add your GitHub credentials if your repo is private

4. Create a Jenkins Pipeline Job:

  • Link it to your GitHub repo URL

  • Use the following Jenkinsfile (explained next)

๐Ÿงช Step 3: Jenkinsfile (Pipeline Script)

pipeline {
    agent any

    environment {
        IMAGE_NAME = "yourdockerhubusername/django-blog:latest"
        DOCKERHUB_CREDENTIALS = credentials('dockerhub')
    }

    stages {
        stage('Clean Workspace') {
            steps {
                deleteDir()
            }
        }

        stage('Checkout Code') {
            steps {
                git url: 'https://github.com/yourusername/your-django-repo.git', branch: 'master'
            }
        }

        stage('Build Docker Image') {
            steps {
                sh "docker build -t ${IMAGE_NAME} ."
            }
        }

        stage('Docker Login') {
            steps {
                sh "echo ${DOCKERHUB_CREDENTIALS_PSW} | docker login -u ${DOCKERHUB_CREDENTIALS_USR} --password-stdin"
            }
        }

        stage('Push Docker Image') {
            steps {
                sh "docker push ${IMAGE_NAME}"
            }
        }

        stage('Deploy to Kubernetes') {
            steps {
                sh "kubectl apply -f k8s-deploy.yaml"
                sh "kubectl set image deployment/django-deployment django-container=${IMAGE_NAME} --record"
            }
        }
    }
}

๐ŸŒŸ Step 4: Run Your Pipeline and Access Your App!

  • Trigger your Jenkins pipeline

  • Jenkins will build the Docker image, push it to Docker Hub, and update your Kubernetes deployment

  • Access your app on your Kubernetes NodePort IP and port

๐Ÿ’ก Tips & Tricks

  • Make sure your Jenkins user has permissions to access Docker daemon (check Docker socket permissions)

  • Configure your kubeconfig inside Jenkins if needed

  • Use Kubernetes Dashboard or kubectl get pods to check deployment status

๐Ÿ™Œ Final Thoughts

Using Jenkins + Docker + Kubernetes gives you an automated, professional way to deploy your Django apps โ€” perfect for real-world production workflows! ๐Ÿš€

If you want me to help you with the exact repo, Dockerfile, Kubernetes manifests, or Jenkinsfile, just ask! ๐Ÿ˜Š

0
Subscribe to my newsletter

Read articles from Harshitha G M directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harshitha G M
Harshitha G M

I am a DevOps enthusiast with hands-on experience in CI/CD pipelines, containerization, and automation using tools like Jenkins, Docker, Kubernetes, and Ansible. I work on building and deploying Flask-based applications and configuring end-to-end DevOps workflows from scratch. Passionate about cloud-native technologies and automation, I focus on optimizing deployment processes and ensuring smooth software delivery.