Devops Project 16

Streamlining CI/CD For Deploying Reddit Clone on a Multinode Kubernetes Cluster

Introduction:

In the ever-evolving landscape of software development, Continuous Integration and Continuous Deployment (CI/CD) pipelines have become indispensable tools for ensuring the efficiency, reliability, and scalability of software delivery processes. In this comprehensive guide, we will delve into the intricacies of setting up a robust CI/CD pipeline to deploy a Reddit Clone application on a Multinode Kubernetes (K8s) cluster.

Overview of the Project:

Our project revolves around automating the deployment process of a Reddit Clone application using a CI/CD pipeline. We start by cloning the application’s code repository from GitHub using the Source Code Management (SCM) stage. Subsequently, we build a Docker image of the application in the Build stage, push it to Docker Hub for storage and distribution in the Push stage, and then deploy the Docker container to the Quality Assurance Testing (QAT) environment in the Deploy stage. Following deployment, we execute tests on the application to ensure its functionality and stability in the Test stage. Once the testing is successful, an approval is sought in the Approve stage before deploying the application to the Multinode Kubernetes cluster in the Final Deployment stage.

PRE-REQUISITES:

1. Jenkins Server:

Refer this Blog to Set-up Jenkins Server: Jenkins : Introduction & Installation Guide

2. Jenkins Master-Node Architecture :

Refer this Blog to set-up Master-Slave Architecture : Simplifying Jenkins Master-Slave Architecture👨‍🍳: A Comprehensive Guide

3. Kubernetes Multi-Node Cluster :

Refer this Blog to setup Kubernetes cluster using Kubeadm : Setting Up a Kubernetes Multi-Node Cluster on AWS: A Step-by-Step Guide🚀

Detailed Steps:

1. Source Code Management (SCM) Stage:

In this initial stage of our CI/CD pipeline, we utilize Jenkins to clone the Reddit Clone application’s code repository from GitHub. By configuring the appropriate credentials and repository URL, Jenkins pulls the latest codebase and prepares it for subsequent stages.

stages {
stage("Checkout From Git") {
steps {
git branch: '<branch-name>', url: '<GitHub-Repo>'
}
}

Replace the <branch-name> with your real branch name and <GitHub-Repo> with real URL of your repository.

In our case, repo link is : https://github.com/harsh2478/reddit-clone-k8s

2. Build Stage:

The Build stage involves compiling the application code and packaging it into a Docker image. Using Docker’s build capabilities, we create an image that encapsulates all the dependencies and configurations required to run the Reddit Clone application seamlessly.

stage("Building Docker Image") {
steps {
script {
sh "sudo docker build -t <dockerhub-username>/reddit-clone:${BUILD_NUMBER} ."
}
}
}

Dockerfile:


FROM node:19-alpine3.15

WORKDIR /reddit-clone

COPY . /reddit-clone

RUN npm install

EXPOSE 3000

CMD ["npm","run","dev"]

3. Push Stage:

After successfully building the Docker image, we proceed to push it to Docker Hub — a central repository for storing and sharing container images. This stage ensures that the Docker image is readily accessible to the deployment environment and other stakeholders involved in the software development lifecycle.

stage ("Pushing To Dockerhub"){
steps{
withCredentials([string(credentialsId: 'DockerHub', variable: 'passwd')]){
sh "sudo docker login -u <dockerhub-username> -p $passwd "
sh "sudo docker push <dockerhub-username>/reddit-clone:${BUILD_NUMBER}"
}
}
}

For this stage, create one credential where you have to store docker hub password. Put the credential ID in the credentialsId section.

4. Deploy Stage:

In the Deploy stage, we orchestrate the deployment of the Docker container to the Quality Assurance Testing (QAT) environment. Leveraging infrastructure-as-code principles, we automate the provisioning of resources and configuration of the deployment environment to ensure consistency and reliability.

stage("Deploying App For QAT"){
steps{
sshagent([<credentialID>]){
sh "ssh -o StrictHostKeyChecking=no ec2-user@<IP-OF-INSTANCE> sudo docker run -dit --name TestingApp-${BUILD_NUMBER} -p ${BUILD_NUMBER}:3000 <dockerhub-username>/reddit-clone:${BUILD_NUMBER}"
}
}
}

Create one credential to store this Private key of the instance and Copy the credentialID here in sshagent and also replace the <IP-OF-INSTANCE> with real IP of your QAT server.

Don’t forget to install the plugin of sshagent for using it inside the code.

5. Test Stage:

The Test stage plays a crucial role in validating the functionality and performance of the deployed application. Through automated testing frameworks and scripts, we execute various test scenarios to verify the application’s behavior under different conditions and identify any potential defects or regressions.

stage("Testing"){
steps{
retry(30){
sh "curl http://<IP-OF-QAT-SERVER>:${BUILD_NUMBER}/ "
}
}
}

6. Approve Stage:

Upon successful completion of testing, the pipeline proceeds to the Approve stage where stakeholders are prompted for approval to proceed with the final deployment. This manual intervention ensures that critical decisions regarding production deployment are made consciously and in alignment with the project’s objectives.

stage("Approving"){
steps{
script{
Boolean userInput = input(id: 'Proceed1', message: 'Ready To Go?', parameters: [[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Please confirm you agree with this']])
echo 'userInput: ' + userInput

if(userInput == true){
// do action
} else {
// not do action
echo "Action was aborted."
}
}
}
}

7. Final Deployment Stage:

In the Final Deployment stage, we deploy the Reddit Clone application to the Multinode Kubernetes cluster — a scalable and resilient container orchestration platform. By leveraging Kubernetes’ advanced features such as service discovery, load balancing, and auto-scaling, we ensure high availability and fault tolerance of the application in production.

stage("deploying On kubernetes MasterNode"){
steps{
sshagent([<credentialID>]){
sh "ssh -o StrictHostKeyChecking=no ec2-user@<Master-Node-IP> rm -rvf reddit-clone-k8s-ingress-manifests/"
sh "ssh ec2-user@<Master-Node-IP> git clone <Git-Url"
sh "ssh ec2-user@<Master-Node-IP> sed -i s/replaceImageTag/${BUILD_NUMBER}/g reddit-clone-k8s-ingress-manifests/deployment.yml"
sh "ssh ec2-user@<Master-Node-IP> kubectl apply -f reddit-clone-k8s-ingress-manifests/deployment.yml"
sh "ssh ec2-user@<Master-Node-IP> kubectl apply -f reddit-clone-k8s-ingress-manifests/service.yml"
}
}
}

Replace credentialID with real credential ID of your private key, and replace the <Master-Node-IP> with real IP, and <Git-Url> with the real repo link where you stored the K8S manifests.

In Our Case, this is our URL : https://github.com/harsh2478/reddit-clone-k8s-manifests

8. COMPLETE PIPELINE

pipeline {

agent {
label "build-system" //Replace it with your slave node label
}

stages {

stage("Checkout From Git") {
steps {
git branch: '<branch-name>', url: '<GitHub-Repo>'
}
}

stage("Building Docker Image") {
steps {
script {
sh "sudo docker build -t <dockerhub-username>/reddit-clone:${BUILD_NUMBER} ."
}
}
}

stage ("Pushing To Dockerhub"){
steps{
withCredentials([string(credentialsId: 'DockerHub', variable: 'passwd')]){
sh "sudo docker login -u <dockerhub-username> -p $passwd "
sh "sudo docker push <dockerhub-username>/reddit-clone:${BUILD_NUMBER}"
}
}
}

stage("Deploying App For QAT"){
steps{
sshagent([<credentialID>]){
sh "ssh -o StrictHostKeyChecking=no ec2-user@<IP-OF-INSTANCE> sudo docker run -dit --name TestingApp-${BUILD_NUMBER} -p ${BUILD_NUMBER}:3000 <dockerhub-username>/reddit-clone:${BUILD_NUMBER}"
}
}
}

stage("Testing"){
steps{
retry(30){
sh "curl http://<IP-OF-QAT-SERVER>:${BUILD_NUMBER}/ "
}
}
}

stage("Approving"){
steps{
script{
Boolean userInput = input(id: 'Proceed1', message: 'Ready To Go?', parameters: [[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Please confirm you agree with this']])
echo 'userInput: ' + userInput

if(userInput == true){
// do action
} else {
// not do action
echo "Action was aborted."
}
}
}
}

stage("deploying On kubernetes MasterNode"){
steps{
sshagent([<credentialID>]){
sh "ssh -o StrictHostKeyChecking=no ec2-user@<Master-Node-IP> rm -rvf reddit-clone-k8s-ingress-manifests/"
sh "ssh ec2-user@<Master-Node-IP> git clone <Git-Url>"
sh "ssh ec2-user@<Master-Node-IP> sed -i s/replaceImageTag/${BUILD_NUMBER}/g reddit-clone-k8s-ingress-manifests/deployment.yml"
sh "ssh ec2-user@<Master-Node-IP> kubectl apply -f reddit-clone-k8s-ingress-manifests/deployment.yml"
sh "ssh ec2-user@<Master-Node-IP> kubectl apply -f reddit-clone-k8s-ingress-manifests/service.yml"
}
}
}
}
}

OUPUT :

Now you can Access your reddit clone site on http://<Master-node-IP>:31000 endpoint.

Conclusion:

In conclusion, the successful deployment of the Reddit Clone application on a Multinode Kubernetes cluster demonstrates the power and efficacy of a well-designed CI/CD pipeline. By automating the software delivery process and leveraging modern DevOps practices, organizations can accelerate their time-to-market, enhance product quality, and streamline collaboration across development and operations teams. With this comprehensive guide, you are equipped with the knowledge and tools to embark on your CI/CD journey and drive transformative changes in software development practices.

 

Leave a Comment

MFH IT Solutions (Regd No -LIN : AP-03-46-003-03147775)

Consultation & project support organization.

Contact

MFH IT Solutions (Regd)
NAD Kotha Road, Opp Bashyam School, Butchurajupalem, Jaya Prakash Nagar Visakhapatnam, Andhra Pradesh – 530027