CI/CD Pipeline using integration of Jenkins and Kubernetes
Saranya Chattopadhyay
Full Stack Developer - DevSecOps @IBM ? DevOps Practitioner ? Ex Intern @CommVault, HighRadius ? 2x GCP, 1x Microsoft, 1x RedHat Certified Engineer
Hello All!
How about a bit of "out of the way integration" for making a CI/CD pipeline both persistent and automated? Sounds cool right? :) Well in this article, I would be explaining about the process, how to integrate Kubernetes with Jenkins and setup a simple webpage. The setup would be such that any logs generated by the webpage would be made persistent using a PVC so that no data is lost even after the deployment is destroyed. So let's get started.
A quick reference to the problem statement tackled here:-
Using Jenkins server on Redhat-8, create a job chain of Job-1,2,3 as per the following syntax:- JOB-1: Pulls the GitHub repo containing the codes required and copy them into a folder in the base OS. JOB-2: By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image container to deploy the code on the top of Kubernetes (eg. if code is of PHP, then Jenkins will start the container that has PHP already installed). The pod will be exposed so that testing team can perform testing on the pod. The data collected by the pod will also be made persistent using PVC. JOB-3: The app will be tested. If found not working, Jenkins will automatically send e-mail the developer about the build fail for further process.
Pre-Requisites for the task :- i) Redhat-8 with Jenkins server installed ii) Minikube installed and running in the machine iii)Kubectl command configured iv) E-mail sending configured in Jenkins using appropriate plugins.
Plugins used :- i) Github ii)E-mail plugin
Steps performed for configuring the Jenkins jobs :-
JOB-1: This job will to GitHub apparently every minute to look for any updates and copies down the code to our base OS.
JOB-2: Main process is going on in this job itself. It looks for the webpage code and deploys the pod which has the respected interpreter already installed. This will also a deploy a PVC for the webpage data to be stored, followed by exposing the pod to the public world. For this, we will require a kubernetes manifest file (webdeploy.yml in my case) as given below:-
apiVersion: v1 kind: Service metadata: name: webdeploy labels: app: webdeploy spec: ports: - port: 80 nodePort: 30010 selector: app: webdeploy type: NodePort --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: web-pvc labels: app: webdeploy spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi --- apiVersion: apps/v1 kind: Deployment metadata: name: webdeploy labels: app: webdeploy spec: selector: matchLabels: app: webdeploy replicas: 1 template: metadata: labels: app: webdeploy spec: containers: - image: vimal13/apache-webserver-php name: webdeploy ports: - containerPort: 80 name: webdeploy volumeMounts: - name: webdeploy-pv mountPath: /var/www/html volumes: - name: webdeploy-pv persistentVolumeClaim: claimName: web-pvc
The configuration of JOB-2 will be as according:-
To be noted here, we need to add our webpage code (HTML/PHP) to the folder /var/www/html of our pod (if using httpd image for HTML file, the mount directory is /usr/local/apache2/htdocs) so that the page gets properly deployed. So, we need to add the following code at the end of the shell script which will do the needful.
sudo kubectl cp /home/ws_task5/index.html $(sudo kubectl get pod|grep webdeploy| awk '{print$1}'):/var/www/html
Output of JOB-2 :-
JOB-3: This job can be well-defined as the test job, which will test the functionality of the pipeline created. Using the manifest file, we have made our webpage deployment to port no. 30010 and since the infrastructure in on the top of kubernetes, the webpage IP will be the minikube IP. Now, we know that the status code of webpage when deployed successfully is 200. Using this concept, we can test our app as follows:-
Output of JOB-3 :-
Once the webpage is successfully deployed (evident that status code for the site is 220 - successful status code), we can access the webpage from -
https://<minikube ip:port no>
Hence, the webpage has been deployed on the top of Kubernetes, using the automation of Jenkins.
That's all guys! Thanks for giving a patient read :)
--- A COLLABORATIVE PROJECT BY SARANYA CHATTOPADHYAY AND SAPTARSI ROY
SIH Grand Finalist 2020 || MLops || ARTH Ambassador and Learner at LinuxWorld Informatics Pvt. Ltd
4 年Great Work!!!