Building the CI/CD Pipeline using Jenkins along with using Docker to build the Image and Deploy in Kubernetes using ArgoCd.
Introduction
Here in this Project, we will deploy the simple Nodejs application on Kubernetes following the CI/CD practices. We will use Jenkins as our Continuous Integration tool and we use ArgoCd as a CD part and deploy our app in Kubernetes using it. We use Docker to build the images and push them to the docker hub.
Objective
To Deploy the simple NodeJs application on k8s using the CI/CD pipeline.
Continuous integration(CI)
The Steps followed in this part of the project are as follows:
AWS
We first Create the AWS EC2 Instance ( t2.large) where we will install all our tools.
Now after creating the AWS EC2 instance the next work is to connect this instance from our terminal through SSH.
ssh -i <path_to_private_key_file> @<public_dns_name_or_ip_address>
Installing Jenkins
Now we should install Java as a prerequisite for Installing Jenkins
sudo apt update
sudo apt install openjdk-11-jre
Check if Java is installed
java -version
Now we can install the Jenkins
curl -fsSL
https://pkg.jenkins.io/debian/jenkins.io-2023.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
binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins
Jenkins Is Installed in the EC2 instance.
Jenkins can be accessed in default port 8080. But for accessing Jenkins we first add the Inbound rule giving access to the port 8080.
We can check the Jenkins using this command in the terminal
ps -ef | grep jenkins
We create the pipeline project in Jenkins.
We provide the Jenkinsfile to the Jenkins through SCM i.e. Git Hub.
The Link to the source code is here.
We use docker as an agent which will help us achieve a reduced footprint of build agents, minimizing resource consumption such as memory and CPU.
We use Docker to build the images of the application and push it on the docker hub.
Installing Docker
Now we install the docker in our root user following the command:
sudo apt update
sudo apt install
docker.io
Grant Jenkins user and Ubuntu user permission to docker daemon.
sudo su -
usermod -aG docker jenkins
usermod -aG docker ubuntu
systemctl restart docker
We restart our Jenkins as Restarting Jenkins after downloading and installing plugins is necessary to ensure that the new plugins are fully loaded and integrated into the Jenkins environment.
Pipeline
The Jenkinsfile we provide to Jenkins is used for automating our tasks.
We configure the required credential of docker, and GitHub.
After configuring we again restarted the Jenkins server.
We build the pipeline and wait for the pipeline to be passed successfully. We Passed the pipeline-building process on 19th attempt😅 .
As per the Jenkinsfile , docker is used as an agent to build the container required to run the steps.
The first stage is for checking if Jenkins is able to clone the GitHub file or not.
In the second stage, Jenkins tries to build the file using the docker.
In The fourth stage, we run the docker container.
fig: Accessing the app in AWS EC2 Instances
In the fourth stage, we push it to the docker hub.
fig: DockerHub Dashboard.
In This way we automate the building, testing, building the images, and pushing them to the docker hub.
Continuous Deployment(CD)
As the next part, we should implement the CD part where we use ArgoCd to deploy our app images in Kubernetes.
We can use our local terminal and with the help of Minikube, we can achieve the required process.
We will create a new namespace, argocd
, where Argo CD services and application resources will live.
We use this command to install the Argocd in minikube cluster.
kubectl create namespace argocd
kubectl apply -n argocd -f
https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
We can list out the services using the command:
kubectl get svc
We edit the argocd-server type as Nodeport from clusterIP
Minikube provides us with a special command which it will generate the URL using which we can access in our browser.
minikube service argocd-server
argocd-server is the name of the svc.
It will provide us the URL through which we can access the ArgoCd.The username is admin and the password is stored in the secret.
After logging we create the new app and enter the required field as necessary. And it will take the deployment file we wrote and pushed in the Git Hub and deploy the images in Kubernetes.
ArgoCd
In This way, we deploy our App in Kubernetes following the CI/CD practices.
Conclusion
In conclusion, implementing a CI/CD pipeline using SonarQube, Maven, and Docker helps us to achieve reliable software development. Jenkins is used to implement the CI part and Argocd is used for the CD part.