Dynamic-jenkins-cluster (docker-jenkins-kubernetes integration)

Amangupta
4 min readOct 16, 2020

--

Create A dynamic Jenkins cluster and using the dynamic Jenkins cluster.
Steps to proceed as:

  1. Create a container image that 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 start the job on slave based on the label provided for the dynamic approach.
    3. Create a job chain of job1 & job2 using the build pipeline plugin in Jenkins
    4. Job1: Pull the Github repo automatically when some developers push the repo to Github and perform the following operations as:
    (i). Create the new image dynamically for the application and copy the application code into that corresponding docker image
    (ii). 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 the Kubernetes cluster performing the following operations:
    (i). If launching the first time then creates 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.
    (ii). If the Application created the first time, then Expose the application. Else don’t expose it.

Dockerfile code for building a container image which is used as a slave.

Job1: Pull the Github repo automatically when some developers push the repo to Github and perform the following operations as:
(i). Create the new image dynamically for the application and copy the application code into that corresponding docker image
(ii). Push that image to the docker hub (Public repository)
( Github code contains the application code and Dockerfile to create a new

few plugins which are required to install:- Docker, GitHub, Build pipeline.

use Git as an SCM tool, Github link:https://github.com/amang1451/task4.git

Job1 Shell commands:-

DOCKER_HOST=192.168.99.101:4243
export DOCKER_HOST
cp -r * /root/task4Slave
docker build -t amangupta2562/slavejenkins-kubectl /root/task4Slave

#added docker hub credential by using docker login command
docker push amangupta2562/slavejenkins-kubectl

console output and docker image at docker hub:-

console output of job1 while building a container image and push to docker hub
container image at docker hub:amangupta2562/slavejenkins-kubectl

job1 finish!

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

first of all, set the cloud to build a job inside a dynamic slave:-

cloud at jenkins

job2 configuration to build in dynamic slave:-

set the dynamic slave label
execute shell commands

if ls /root/ | grep .html
then
if
kubectl get pvc | grep htmlwebpvc
then
echo “pvc already exist”
kubectl apply -f /root/htmlwebpvc.yml
else
kubectl create -f /root/htmlwebpvc.yml
sleep 5
fi
if kubectl get deployment | grep myhtmlweb
then
echo “deployment already exist”
kubectl apply -f /root/htmlwebdeployment.yml
kubectl cp /root/index.html $(kubectl get pods -o=jsonpath={.items[0].metadata.name}):/usr/local/apache2/htdocs/
else
kubectl create -f /root/htmlwebdeployment.yml
sleep 15
kubectl cp /root/index.html $(kubectl get pods -o=jsonpath={.items[0].metadata.name}):/usr/local/apache2/htdocs/
fi
else
if
kubectl get pvc | grep phpwebpvc
then
echo “pvc already exist”
kubectl apply -f /root/phpwebpvc.yml
else
kubectl create -f /root/phpwebpvc.yml
sleep 5
fi
if kubectl get deployment | grep myphpweb
then
echo “deployment already exist”
kubectl apply -f /root/phpwebdeployment.yml
else
kubectl create -f /root/phpwebdeployment.yml
sleep 15
kubectl cp /root/$(ls /root/ | grep .php) $(kubectl get pods -o=jsonpath={.items[0].metadata.name}):/var/www/html/
fi
fi

launch Slave at a run time automatically to build a job when a job label(slave) matches this cloud label.

deployment configured by Jenkins(job2)

job2 finish!

Build pipeline view
web page at port 31000 by kubectl command

finally task4 complete!

--

--

No responses yet