Creating CI/CD pipeline using dynamic distributed cluster

TASK
- 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
4. Job1 : Pull the Github repo automatically when some developers push repo to Github and perform the following operations as:
a. Create the new image dynamically for the application and copy the application code into that corresponding docker image
b. Push that image to the docker hub (Public repository)
( Github code contain the application code and Dockerfile to create a new image )
5. Job2 ( Should be run on the dynamic slave of Jenkins configured with Kubernetes kubectl command): Launch the application on the top of Kubernetes cluster performing following operations:
1. 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.
2. If Application created first time, then Expose the application. Else don’t expose it.
Lets start the task
Step 1. Creating the container image that’s has Linux and other basic configuration required to run Slave for Jenkins. ( example here we require kubectl to be configured ).
First I have created one workspace named as devtask in / directory. In this workspace I have created one Dockerfile for build the image that has linux and other basic configuration required to run Slave for Jenkins. ( example here we require kubectl to be configured ).In this workspace I have added one hidden folder “.kube” in which all the certificates ,key and config file that is required by kubectl to connect to kubernetes is added.

This is the Dockerfile that I have created. I have copied the “.kube folder” that contain all the requirements for kubectl to connect to kubernetes..

Now after creating the Dockerfile we have to build it for creating the image.
docker build -t task4:v1

Here I am taged the image that we have created to anurag08/task4:v1 so that we can push this image to dockerhub.
docker tag task4:v1 anurag08/task4:v1

For pushing the image to dockerhub we have to use command -
docker push anurag08/task4:v1

As you can see the image has been pushed to docker hub successfully.

After the image has been sent to dockerhub we need to do some settings so that jenkins can able to create dynamic slave for running the second job over it.
For this go to the docker host . In docker host first run “ systemctl status docker” . After this go to this file — /usr/lib/systemd/system/docker.service and edit this file by command — “ vi /usr/lib/systemd/system/docker.service”

In this file add “ -H tcp:///0.0.0.0:4243”. Her we are adding this line so that when jenkins create dynamic slave for running the job it will contact to this docker host to launch one container to run the job by using tcp protocol with 4243 port number.

After adding this line to this file we have to run two commands
systemctl daemon-reload
systemctl restart docker
we have to run these commands so that the changes we have done in the file would get enabled properly.

Step 2. After launching the job jenkins should automatically start the job on the dynamic slave based on the labels , so for that we need to configure the jenkins and we have to also install 2 plugins “Docker and Yet Another Docker”.
Go to jenkins → Manage Nodes and clouds — -> Configure clouds
In the Docker Host Uri — give the ip of dockerhost

In Docker agent templates assign some labels to this slave (by using these labels jenkins can dynamically launch slave for running the job over it). Give the docker image that we have created in which kubectl has been configured so that by using this image dynamic slave launch one container for running the job.

Now we have to inject SSH key

Step 3. Creating the first job for the task-
Through this job jenkins will pull the Github repo automatically when some developers push repo to Github.

In this Github repository developer uploaded a application code and a Dockerfile .

In this Dockerfile at the time of creating the image the code that developer has written will be copied in it and new image will be build with this code.

Operation performed by this job-
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 )

Output of this job —



Step 4. creating the second job that will run on dynamic slave. In this job we are giving the labels that we have assign at the time of configure the cloud so that by using this labels Jenkins will know that they have to run this job on dynamic slave and start creating slave dynamically for running this job.

This job will build only when first job will build successfully .

Job2 ( Should be run on the dynamic slave of Jenkins configured with Kubernetes kubectl command):
Launch the application on the top of Kubernetes cluster performing following operations:
1. 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.
2. If Application created first time, then Expose the application. Else don’t expose it.

output of the second job -

As you can see the deployment has been created successfully on top of kubernetes cluster . All the pods are running fine and service has also created for exposing this deployment to outside world. The pods are running with the image that we have created in the first job.

This is the site running inside the pods .

we have created the task successfully. Here is the beautiful output come up in form of pipeline.
