Integrating Jenkins dynamic code with K8's
In this task , I have integrated the jenkins dynamic code with Kubernetes , with the help of Cloud nodes . In dynamic way whenever the jenkins jobs are called , then node will automatically run in background and completes its job .
My task is all about the dynamic way to run the jobs.
TASK DESCRIPTION -
1. Create container image that’s has Linux and other basic configuration required to run Slave for Jenkins. ( example here we require kubectl to be configured )
2. When we launch the job it should automatically starts job on slave based on the label provided for dynamic approach.
3. Create a job chain of job1 & job2 using build pipeline plugin in Jenkins
Job1 : Pull the Github repo automatically when some developers push repo to Github and perform the following operations as:
- Create the new image dynamically for the application and copy the application code into that corresponding docker image
- Push that image to the docker hub (Public repository) ( Github code contain the application code and Dockerfile to create a new image ).
Job2 : Launch the application on the top of Kubernetes cluster performing following operations:
- If launching first time then create a deployment of the pod using the image created in the previous job. Else if deployment already exists then do rollout of the existing pod making zero downtime for the user.
- If Application created first time, then Expose the application. Else don’t expose it.
SOLUTION -
Firstly , we need to setup the Docker Host URI for the docker server so that jenkins will go to node by tcp protocol. Additionally , we need to install docker plugin in jenkins and setup the docker cloud for the slave.
Now , Set up the github repo which contains our Dockerfile , deployment file for kubernetes deploy.yml and our index.html file.
Lets get started with our Jenkins Jobs -
JOB1 - devops
As soon as the developer push the code in the github , jenkins fetches the code either with the webhooks or with the triggers. As soon as the code get fetched , the code get copied , the dockerfile get build and the new image gets created . The new image now get pushed the docker hub (here i used the docker plugin, to build and push the docker image).
After that , the downstream project gets triggered.
Here is my DockerFile-
FROM httpd
COPY index.html /usr/local/apache2/htdocs/
JOB2- kubernetes
This job is triggered only if the Job1 get succeed. In this job , we need to launch our website on the top of the kubernetes cluster , for this we need to create a deployment with the help of the deplop.yml file which creates a deployment of pods with 3 replica sets.
This job works as follows, If the deployment is done first time then pods with 3 replica sets are deployed with the same image as of Job1 which is gets downloaded from the docker hub. Else if deployment already exists then rollout with zero downtime error. Also here i exposed the website so that our clients can see the website.
- When there is no deployment present then the output of the jenkins JOB2 ie before the rolling update .
- When there is already a deployment present then the output of the Jenkins JOB2 ie after the rolling update.
Here our task4 is done !!
Thank You
MY GITHUB REPO-
Senior Solutions Architect @ Red Hat | 7 x AWS Certified | Cloud Architect | Data Architect | Cloud Trainer
4 年Look interesting