Streamlining CI/CD for Python Django Application
Introduction
In the fast-paced realm of software development, Continuous Integration and Continuous Deployment (CI/CD) have emerged as indispensable practices. This blog aims to unravel the significance of CI/CD in the software development cycle and implementation strategies. Let's dive into the world of CI/CD and unlock the potential for seamless, efficient and error-free software release of Python Django Application.
Overview of Python Django Application
Project source code: https://github.com/Anandhi-23/Python_CICD/tree/master/myproject
The above GitHub repository contains the source code for a straightforward Python Django static application.
This application can be locally executed by running the command python manage.py runserver
from the 'myproject' root directory. By default, when you run a Django application using the above command, it starts the development server on 127.0.0.1:8000
.
Pre-requisites to run the application locally:
Python
Django module
Let's containerize the application using Docker now. Here are the resources for containerizing the application.
Requirements file location: https://github.com/Anandhi-23/Python_CICD/blob/master/requirements.txt
Dockerfile location: https://github.com/Anandhi-23/Python_CICD/blob/master/Dockerfile
⚙ Continuous Integration and Continuous Deployment
Let's set up a Jenkins pipeline for continuous integration process. This pipeline involves cloning the GitHub repository, building, and pushing the Docker image back to Dockerhub. Additionally, it updates the manifest file using a Shell script, incorporating the image tag as the build number. For the continuous deployment process, the Kubernetes operator Argo CD is utilized. Argo CD consistently monitors changes in the manifest file and promptly deploys the updated application version into the Kubernetes cluster.
Pre-requisites
A virtual machine in the cloud (Ex: AWS EC2 instance)
Install Java, Jenkins, Docker, minikube, kubectl (CLI for K8s), Argo CD operator, Argo CD CLI
Allow necessary ports in the Inbound rules of the Security group
Install the required Jenkins plugin (Docker Pipeline, Parameterized Trigger Plugin)
GitHub and Dockerhub account
Tools installation
https://github.com/Anandhi-23/Python_CICD/blob/master/README.md
⚙ Continuous Integration Workflow
Step 1: Create a Jenkins pipeline using the provided script below.
https://github.com/Anandhi-23/Python_CICD/blob/master/jenkinsfile
Step 2: In the Jenkins pipeline, navigate to Configure and enable "This project is parametrized" to configure Jenkins parameters. Select 'String Parameter' as the parameter type, and set the values for the following parameters. Click apply and save.
GIT_REPO_NAME (Example: Python_CICD)
GIT_USER_NAME
DOCKER_IMAGE (Example: <ENTER DOCKERHUB USERNAME>/python-cicd:${BUILD_NUMBER})
Step 4: Click 'Build with Parameters' from left panel and input the desired value to execute the pipeline.
The Jenkins pipeline runs winthin a Docker container instead of on a node to save resources. Once the manifest file is updated with the new image tag, the Argo CD operator will initiate the deployment of the application's latest version to the Kubernetes cluster. Follow the instructions below to establish continuous deployment using Argo CD.
⚙ Continuous Deployment Workflow
Step 1: Argo CD cluster creation
Here is a manifest file for establishing a new Argo CD cluster with the default configuration.
Step 2: Apply the changes using kubectl apply -f <name of the manifest file.yml>
Step 3: Check the running pods using kubectl get pods -w
Step 4: Verify the service using kubectl get svc
Step 5: The 'example-argocd-server' service facilitates access to the Argo CD UI from the browser. Modify the type from 'ClusterIP' to 'NodePort' using the command: kubectl edit svc example-argocd-server
.
Step 6: Execute the command kubectl get svc
to check the service type.
Step 7: Run minikube service list
to get the url for accessing the Argo CD UI.
⚙ Instructions for deploying the app using the Argo CD CLI
Step 1: Login to CLI using the command argocd login <url:nodeport>
By default, the username is set to 'admin'
, and the password can be obtained by executing the command kubectl edit secret example-argocd-cluster
.
Kubernetes password are base64 encrypted and it can be decrypted by echo <password> | base64 –d
Step 2: Run the below command to deploy the application to deploy the application using Argo CD CLI.
argocd app create kaizenapp --repo https://github.com/Anandhi-23/Python_CICD.git --path python-app-manifests --dest-namespace default --dest-server https://kubernetes.default.svc --directory-recurse
Step 3: Get the application status using argocd app get kaizenapp
.
Step 4: The application status will be initially in OutOfSync
state since the application has yet to be deployed, and no Kubernetes resources have been created. To sync (deploy) the application, run argocd app sync kaizenapp
.
Step 5: Check the application status, deployments and pods using kubectl get application
, kubectl get deploy
and kubectl get pods
respectively.
The application has been deployed utilizing the Argo CD CLI.
📑 References
https://www.youtube.com/watch?v=JGQI5pkK82w&t=4664s
https://www.youtube.com/watch?v=ZgJQG475oME
https://argo-cd.readthedocs.io/en/stable/
https://argo-cd.readthedocs.io/en/stable/user-guide/commands/argocd/