Job on dynamic slave in jenkins

Dhiraj Kumar
5 min readSep 10, 2020

Jenkins: jenkins is the devops tool which helps to automation of projects , by which projects are tracked and done automatically. jenkins can be used as integration tool also . By jenkins many devops tool are integrated and done project in loop automaticaly without using more human resources .

When job is running on jenkins then cpu, ram and other hardware or software is used of that system where jenkins is running , due to this when more heavy job is running then system might be hang up . For avoiding this problem ,it is necessary to create distributed job cluster.

For creating distributed cluster one jenkins connect with two or more than two VM or OS via SSH in linux and via JNLP in windows .

The OS which gives service to jenkins for run job, that is known as worker/slave OS and the OS, over which jenkins is running is known as master OS.

Distributed cluster is of two types, static distributed cluster and dynamic distributed cluster.

When VM or laptop(windows/linux/mac OS ) are used as slave OS, then static distributed cluster is configured. But when docker container is used then dynamic distributed cluster is configured.

In dynamic distributed cluster automatically slave node is launched , when job is build , via SSH , job is run and then slave node is terminated .

For creating dynamic distributed cluster , first of it is necessary to do some setup .

docker can be managed by other OS also, when docker server is in one OS and docker client is in other OS , For this network have to be established between two VMs using tcp protocol .

The OS which support docker server is known as DOCKER_HOST .

The OS which support docker client is known as cloud Node .

For setup OS as docker server for accessing from other OS for dynamic node cluster , first of all network have to be set .

edit file “/usr/lib/systemd/system/docker.service”

this will exposed docker server , after edit file run cmd

systemctl daemon-reload
systemctl restart docker

Task description:

  1. Create container image that has linux and other basic configuration required to run slave for jenkins .
  2. When i launch the job then automatically start job on slave based on the label provided for dynamic approach .

job in jenkins:

Job1 : Pull the github repo automatically when some developers push repo to Github and perform the following operations as:

1. Create the new image dynamically for the application and copy the application code into that corresponding docker image

2. Push that image to the docker hub (Public repository)

( Github code contain the application code and Dockerfile to create a new image )

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.

First of all, i have created a Dockerfile for creating dynamic slave .

mkdir devops_task4
cd devops_task4

Dockerfile:

docker build -t slave_image:v1 .
docker tag slave_image:v1 firsttalk26/slave_image:v1
docker push firsttalk26/slave_image:v1

now for creating dynamic distributed cluster jenkins is used, In jenkins “Docker” and “Yet Another Docker” plugin are required

Now go to “Manage jenkins>> Manage Nodes and Clouds>> Configure clouds

Job1:

Execute shell:

sudo mkdir /root/devops_task4
cp -rvf * /root/devops_task4

output:

Job2:

Execute shell:

if kubectl get deployment | grep httpd-deployment
then
kubectl set image deploy httpd-deployment webserver=firsttalk26/httpd_image:v1 --record
else
kubectl apply -f httpd_deployment.yml
kubectl apply -f httpd_service.yml
fi

output:

Job3:

Execute shell:

status=$(curl -o /dev/null -s -w "%{http_code}" http://192.168.99.109:30009)if [ $status == 200 ]
then
exit 0
else
exit 1

fi

--

--

Dhiraj Kumar

Expertise on Cloud Computing who has helped many startups to reduce cloud cost upto 40% based on business need. Focused to optimize development process.