In a production or service based company it matters how soon you update or roll out new services or products. This signifies your services are efficient , fast and up to date with regards to today’s tech.
Previously, rolling out updates was not a easy task as it is today. Earlier companies had to restart the whole web server and then changes were seen but now we have more efficient devops way to do things. Let us see how.
So, here is what we will going to setup:
- Create container image that has Linux and other basic configuration required to run Slave for Jenkins ( example here we require kubectl to be configured ).
- When we launch the job it should automatically starts another job on slave based on the label provided for dynamic approach.
- 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 ( 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:
- 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 roll out the updates.
- Expose the services if not otherwise roll out the updates.
We will be using ramped deployment strategy sometimes also known as slow deployment.
My Environment
I have 3 vms:
- rhel 8 clone having jenkins and docker installed used as jenkins server and docker client.
- rhel 8 having docker installed being used as docker server.
- minikube vm for running kubernetes cluster.
For doing this task we first have to setup our docker host and client.
Docker Host and Client setup
Start docker service in one of the vm, In my case rhel 8 is docker host and rhel 8 clone is client.
Start the docker service in your vm and edit the service file as shown below.
Above, we are adding a tcp rule to allow traffic on current IP from 4243 port.
Now in docker client add docker host IP and export it. To make this permanent add this line to /root/.bashrc or /etc/bashrc
export DOCKER_HOST=<docker_host_ip>:4243
Also make sure that docker service has been stopped in docker client if docker is installed in client also.
Now, if we run any docker command from rhel 8 clone(client) it will execute on rhel 8(host).
Dynamic jenkins cluster setup
We are setting up dynamic jenkins cluster using docker by creating a custom image for kubernetes in which kubectl will be configured so that we can use that as slave.
As soon any job will run tagged with this slave we are creating a new docker slave will run on docker host and will be deleted automatically after the job completion.
FROM centos
ENV pass jenkins
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
RUN chmod +x ./kubectl
RUN cp ./kubectl /usr/local/bin/kubectl
RUN cp ./kubectl /usr/bin/
RUN mkdir -p /var/run/sshd
COPY ca.crt /root/
COPY client.key /root/
COPY client.crt /root/
RUN mkdir /root/.kube/
COPY config /root/.kube/
RUN yum install java-1.8.0-openjdk.x86_64 -y
RUN yum install openssh-server -y
RUN ssh-keygen -A
RUN yum install sudo -y
RUN yum install net-tools -y
RUN yum install git -y
RUN echo root:redhat | chpasswd
EXPOSE 22
CMD /usr/sbin/sshd
now build this dockerfile
docker build -t <imagename>:<tag> .
Start the jenkins service …
systemctl start jenkins
Open jenkins dashboard and navigate to manage jenkins then..
From above images we have configured dynamic cluster setup.
To use kubectl in other system you have to transfer configuration and certification.
To know and setup minikube check: https://kubernetes.io/docs/tasks/tools/install-minikube/
To setup kubectl : https://kubernetes.io/docs/tasks/tools/install-kubectl/
Jenkins Jobs
Now it’s time to create job chain of job 1 and 2.
JOB_1
This will pull the github repo which will have our website code as well as Dockerfile for custom web server and build the dockerfile and push the image to docker hub.
Here, we can use poll scm in jenkins to monitor updates on github or we can also create github webhooks. For using webhook on VM you can use ngrok.
JOB_2
This job pull the code for kubernetes deployment from github repo and will deploy on kubernetes cluster and also will rollout updates if any available.
code under build:
cp -rf webdeploy.yml . /root/
if sudo kubectl get deploy | grep myweb-deploy
then
echo "deploymeny exists ...rolling out updaytes"
sudo kubectl rollout restart deployment/myweb-deploy
sudo kubectl rollout status deployment/myweb-deploy
sudo kubectl get service myweb-deploy
else
sudo kubectl create -f /root/webdeploy.yml
sudo kubectl expose deployment myweb-deploy --port=80 --type=NodePort
sudo kubectl get service myweb-deploy
fi
Note:
Also our webdeploy.yml contains:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myweb-deploy
spec:
replicas: 3
selector:
matchLabels:
env: production
strategy:
type: RollingUpdate
template:
metadata:
name: myweb-prod
labels:
env: production
spec:
containers:
- image: mykgod/httpd-php-server
name: myweb-con
And our build pipeline looks like:
Before rollout:
After rollout:
Conclusion
Here, we successfully achieved how to roll out new updates for a website which is running on kubernetes cluster with custom made image of web server and also run our jobs on dynamic jenkins cluster.
Thanks! Comment down any suggestion or queries.



















































































































































