Dynamic Jenkins Cluster Configuration

Shikhar Srivastava
3 min readJul 27, 2020

--

Jenkins is an open-source automation server which enables developers around the world to reliably build, test, and deploy their software. Jenkins is one of the most used Continuous Integration tools for build automation due to its capacity in managing a great number of nodes, that are called slaves, with executors for a wide range of tasks related to project build and deploy. Each executor runs Jenkins jobs, alleviating Jenkins server from running all the tasks.

Here I am going to show how to create a cluster for Jenkins using Kubernetes.

Step 1: Create a docker file required to configure kubectl.

docker build -t k8s:latest .
docker tag k8s:latest ssdock98/k8s:latest
docker push ssdock98/k8s:latest

For setting dynamic Jenkins cluster, we will require 2 VMs, one as a server and another as a client.

Now in VM 2, do some changes in the docker service.

systemctl status docker 
vim /usr/lib/systemd/system/docker.service

Now restart the docker service.

systemctl daemon-reload
systemctl restart docker

Now stop the docker services of VM1 and export the docker host, in order to use VM1 as a docker client.

export DOCKER_HOST=IP of VM2:port

Step 2: Configure the cloud environment. This will create dynamic slaves to perform the task.

Step 3: Create a Jenkins job1. In this, Jenkins will pull the Github repo automatically when some developers push the repo to Github. Now create the new image dynamically for the application and copy the application code into that corresponding docker image. Also, push that image to the docker hub(Public repository).

Dockerfile:

FROM centos:latest
RUN yum install httpd -y
COPY web.html /var/www/html
CMD /usr/sbin/httpd -DFOREGROUND
EXPOSE 80

After this Job run, this will automatically push that image to DockerHub which the developer created.

Step 4: Create Jenkins Job 2. It should run on the dynamic slave of Jenkins configured with K8s kubectl command. If launching the application first time then create a deployment of the pod using the image created in the previous job. Else if deployment already exists then do a rollout of the existing pod making zero downtime for the user. And if the application created the first time, then expose the application, else don’t expose it.

So first we have to give the label.

So one salve is automatically created and it is dynamic.

Hence, our task is complete.

Thank you!!

--

--