CI/CD Kubernetes
This article is a simple integration of GitHub, Jenkins, Kubernetes and Docker to automate the process of deploying a website by creating custom Docker images using Dockerfile.
Problem Statement.....
Perform second task on top of Kubernetes where we use Kubernetes resources like Pods, ReplicaSet, Deployment, PVC and Service.
1. Create container image that’s has Jenkins installed using dockerfile Or You can use the Jenkins Server on RHEL 8/7
2. When we launch this image, it should automatically starts 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 :
1. 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 ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed )
2. Expose your pod so that testing team could perform the testing on the pod
3. 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.
Now let's start from scratch
1.Creating Dockerfile and building it.
The docker file includes some software installation and to run some commands.
We have copy some certificates from windows like ca.crt, client.crt, client.key from windows which is to connect to minikube from the docker container.
We also have to create a file called config and copy that to folder /root/.kube/
apiVersion: v1 kind: Config clusters: - cluster: server: https://192.168.99.105:8443 certificate-authority: /root/ca.crt name: api contexts: - context: cluster: api user: yash users: - name: yash user: client-key: /root/client.key client-certificate: /root/client.crt
This file accesses the key and certificates to connect to minikube.
CMD ["java", "-jar", "/usr/lib/jenkins/jenkins.war"]
The above command starts Jenkins service when you launch the container.
docker build -t jen-kube .
Above command is used to build the docker image using the above created Dockerfile.
Now run the docker container and expose it
docker run -dit -p 8081:8080 -v /:/host/repo --name kube_jen jen-kube
Initially, I have created a the folder "/repo" in the host and attached it to the container.
After launching the container you can access jenkins dashboard by using the web address.
IPAddress:Port
This is the Jenkins dashboard.
2.Creating Job1.
We have to create a git repository and add required files.
Now in Jenkins we have configure the git repository.
In Repository URL give your git repository url.
For build trigger we can use "GitHub hook trigger for GITScm polling"
Before this we have to add webhook in our repository
First we have to use ngrok for the site to be accessible anywhere in the world.
Run the command
./ngrok http 8081
add the forwarding address to the payload url.
Now we have to copy the files to container from github.
Job1 is finished.
3.Creating Job2.
This job has to run after Job1.
Now we have to deploy our file in kubernetes pod using respective image.
The kubernetes files are available in the GitHub, link below.
This finishes Job2.
4.Creating Job3.
This runs after Job2.
Here we have to test our site
If the status code is 200 then it means that the site was opened successfully.
5.Email configuration.
First we have to install Email Extention Plugin.
We have to search it in Available section if not installed.
Next we have to go to Manage Jenkins >> Configure System
Scroll to last, and configure in E-mail notification
In Test e-mail recipient put the email address to which you want the email to be delivered.
We can get email of Job3 logs. For that we have to add email notification in post-build-actions.
6.Creating Job4.
This Job is created for monitoring of the container and to launch another if the existing fails.
This runs after Job3.
Here I have scheduled for every 1 minute.
7.Build Pipeline
Install build pipeline plugin
give a name in View name.
click on apply.
Finally we get the pipeline view of our integration.
Web page.
Thank You