It’s difficult to deploy a new version of code without downtime. To resolve this issue a blue green deployment strategy is used.
Blue green deployment strategy is used to deploy a new version of code. With the help of blue green deployment strategy, it is possible to roll back to the previous version of the application.
For automating this deployment, we are using Jenkins as CICD pipeline to deploy an application on container of Google Kubernetes engine cluster with blue/green deployment strategy using zero downtime deployment. A blue/green deployment strategy will increase application availability and reduces deployment risk by simplifying the rollback process if a deployment fails.
Architecture Diagram:
Advantages:
1. Debugging: In Blue-green deployment, rollbacks always leave the failed deployment intact for analysis.
- Instant rollback: we can easily go back to the previous version in an instant.
-
Zero-downtime: no downtime means that we can make releases at any time.
-
Stability: More stability to your app. You will implement complex pipelines with all layers of automated tests.
Required tools and services:
- GitHub
- Jenkins
- Docker
- Docker Hub
- Google Kubernetes Engine
What is GitHub?
GitHub is a Git repository hosting service. GitHub has many features, such as access control and collaboration. It provides a Web-based graphical interface. It will host the source code of your project in the form of different programming languages and keeps track of the various changes made by programmers. It offers both distributed version control and source code management (SCM) functionality of Git.
What is Jenkins?
Jenkins is a DevOps tool that is used for automation, and it is an open-source server that allows all the developers to build, test and deploy software. It works on java as it is written in java.
What is Continuous Integration?
Continuous Integration requires developers to integrate code into a repository at regular intervals. There is a problem of finding issues in the build lifecycle. Continuous integration requires frequent builds by the developers. When a code commit happens, a build should be triggered.
What is Docker?
Docker is a tool that allows you to build, test, and deploy applications quickly. Docker packages in the form of image will run on units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. With the help of Docker, you can quickly deploy and scale applications into any environment and know your code will run.
What is Docker Hub?
Docker Hub is the repository of container images with an array of content sources including container community developers, open-source projects, and independent software vendors (ISV) building and distributing their code in containers. All users get access to free public repositories for storing and sharing images or can choose subscription plan for private repos.
What is Google Kubernetes Engine?
A managed environment for deployment, managing, and scaling containerized applications is provided by the GKE. The GKE environment consists of machines grouped together to form a cluster.
Blue-green Deployment strategy
A Blue-Green deployment strategy, also known as Red-Black Deployment in software delivery, is one in which the old and new instances of an application or microservice operate in parallel in production in the same time with a load balancer switching traffic from the older version to the newer one.
Prerequisite:
- Launch VM in google cloud platform
- Jenkins Installation
- Gcloud and kubectl installation
- Docker installation on Jenkins server
- Kubernetes cluster configuration
- Jenkins plugins installation
- Jenkins Credentials configuration
- Jenkins pipeline configuration
- Jenkins pipeline job
- Webhook configuration
Step 1: Launch VM in google cloud platform:
- Allow Http and https traffic during VM creation.
- Create and add firewall rule to VM to enable port range for Jenkins.
Step 2: Installation of Jenkins:
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
apt install openjdk-11-jre
java -version
apt-get install jenkins -y
systemctl start jenkins.service
systemctl enable jenkins.service
cat /var/lib/jenkins/secrets/initialAdminPassword
To test Jenkins server status >>> http://<ip-addr>:8080
Provide sudo user permission to Jenkin user.
Step 3: Gcloud and kubectl installation
Following are the commands for installing gcloud and kubectl in Jenkins server.
which gcloud
gcloud version
which bq
gcloud init
gcloud auth list
gcloud info
gcloud config list
snap install kubectl –classic
kubectl version –client
step 4: Docker installation on Jenkins server
apt update -y
apt install docker.io -y
docker –version
Step 5: Kubernetes Cluster Configuration
Go to Google cloud console enable Kubernetes engine API and create standard cluster.
As we create standard cluster, By default 3 node pool instance will be created.
Step 6: Jenkins plugins installation
Following are the plugins required
- Docker pipeline
- Google Kubernetes Engine plugin
Step 7: Jenkins Credentials configuration
Create or modify default google Service Account for integrating Jenkins with GKE cluster
Go to IAM & Admin in GCP and choose service account
Use default service account and generate private key
Go to manage credential in Jenkins and upload the downloaded key (JSON format) into “credentials>system>global credentials” path.
Provide Docker Hub account credential to Jenkins under same path.
For source code reference:
For V1 version of application: https://github.com/suraj11198/k8s.git
For V2 version of application: https://github.com/suraj11198/new-zero-downtime-freestyle.git
Deployment Procedure:
V1 version code deployment:
Create pipeline job for v1 version deployment application and configure SCM into it, apply and save it.
Now, Build job V1
After building a job,
The docker image will be push on docker hub repository.
Output:
Once pipeline successfully runs the code for v1 version of application get deployed on GKE cluster service/application deployed on service section with 2 pods as we mentioned 2 replicas in deployment.yaml file in source code.
Test this deployment with external endpoint IP address.
V2 version code deployment:
Now to deploy second upgraded version of application, create pipeline job for v2 version deployment application and configure SCM into it, apply and save it.
Now, build job V2:
After building a job,
The docker image for v2 version of code pushed into dockerhub repository.
Output:
Once pipeline successfully runs the code for v2 version of application get deployed on GKE cluster service/application deployed on service section with 2 pods as we mentioned 2 replicas in deployment.yaml file in source code.
Test this v2 version deployment
Rollback to previous version with Post build action:
Once job 2 failed it will trigger and build job 1 automatically.
Following script added in v2 version Jenkins file source code.
Once the job v2 get failed, it will automatically rollback to job V1 and trigger pipeline thus application for V1 version get deployed with zero downtime.
To Automate build trigger, add webhooks on GitHub for both the job:
Once Developer commit changes on source code it will automatically trigger pipeline and application will be deployed on containers of GKE cluster.
For V1 code repository(k8s):
Configure webhook on Jenkins v1 job pipeline
Add Jenkins server IP address in payload
For adding secret, we need to create access token inside Jenkins.
Follow same approach for V2 version code repository(new-zero-downtime-freestyle):
Conclusion:
In this way, we have setup CICD pipeline using Jenkins to deploy an application on GKE cluster using blue green deployment strategy with zero downtime. This kind of approach is crucial in the deployment of applications since it allows us to achieve zero downtime while transitioning to different versions of application.
Great Blog !
Informative Blog !
Informative Blog !
Great work Ajrun! A must READ blog. Kudos!