Configuring CI/CD on Kubernetes with Jenkins
So, this task is similar to my previous task on Jenkins but this time I have replaced docker with Kubernetes. So, I will try to write as short as possible.
Those who don’t know what Kubernetes is, in short it is a very intelligent tool for container orchestration.
You can study Kubernetes from official documentation.
And to study Docker in depth here is the playlist. – https://www.youtube.com/watch?v=3Kn6_b-1mK4&list=PLAi9X1uG6jZ30QGz7FZ55A27jPeY8EwkE
Here is the link to my last post in case you want to check it out – https://github.com/anantjakhmola/jenkins-project
Codes of Pods, Replica Set, Deployment, PVC and Service are present on : https://github.com/anantjakhmola/Devops-task3
OBJECTIVES
1. Create container image that has Jenkins installed using Dockerfile Or You can use the Jenkins Dedicated Server on RHEL 8/7
2. When we launch this image, it should automatically start Jenkins service in the container.
3. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
4. Job1 : Pull the Github repo automatically when some developers push repo to Github.
5. Job2 :
· By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image container to deploy code on top of Kubernetes ( e.g. If code is of PHP, then Jenkins should start the container that has PHP already installed )
· Expose your pod so that testing team could perform the testing on the pod
· Make the data to remain persistent ( If server collects some data like logs, other user information )
6. Job3 : Test your app if it is working or not.
7. Job4 : if app is not working , then send email to developer with error messages and redeploy the application after code is being edited by the developer
Kubernetes builds and deploy as well as update applications with no downtime. One of the most popular CI/CD tools now is Jenkins.
Here I am using jenkins installed on my rhel8
I think you have setup Kubernetes on your system also you can always follow the official guide to perform the installation of kubernetes.
Step 1: Login into Jenkins account
We need to configure the jobs so that whenever a developer pushes the code in github it automatically deploys it in kubernetes pods.
step 2 :creating first job that automatically pulls the code from github whenever developer pushes it .
Here the code is saved in a folder task3 .
Step 3:Now we have the developers code create a job which by looking at the code or program file, automatically start the respective language interpreter installed image container to deploy code on top of Kubernetes.
Through this script in job if a developer pushes a html code then jenkins automatically starts a html pod and if the pod is not already present ,it creates a pod.
Here is the script for your reference
if sudo ls /task3 | grep html then if sudo kubectl get pvc | grep html then echo "pvc for html already created" else sudo kubectl create -f /task3/html-pvc.yaml fi if sudo kubectl get deploy | grep html-webserver then echo "already running" else sudo kubectl create -f /task3/html-deply.yaml fi else echo "no html code from developer to host" fi htmlpod=$(sudo kubectl get pod -l app=html-webserver -o jsonpath="{.items[0].metadata.name}" ) sudo kubectl cp /task3/*.html $htmlpod:/usr/local/apache2/htdocs
Step 4:Code is finally deployed ,now let’s perform some testing. for testing a jenkins job is created which test whether the code is properly deployed or not. If not, then a mail is sent to the developer to check for the code.
step 5: creating a job which sends the mail to developer if code is not working properly or a job fails.
Here I would like to thank my friend Vaishnavi Agarwal for this code snippet I don’t know why but my mail was not working at that time.
So, we have finally created a pipeline through jenkins that deploys the code in kubernetes whenever a developer pushes a code in github. This pipeline not only deploys but informs the developer if anything went wrong.
Let’s try to run it
we have pushed a code in github repo ,let’s see the deployment.
Our pipeline has started ,it has pulled the code from github repo and stored it.
Deployed the code and tested it ….
Finally, our pipeline is working.
Thinking of it Why not make it faster just from code. Watch the next article to find out how I am going to do it.
-------------------------------------
If you find the article useful do link it or if there are some suggestion do reply. Till then Keep learning.
Java | Spring Boot | Microservices | Backend Developer | Researcher | Machine Learning | MLH Best Social Good Hack Winner
4 年Good Work bro ??