Dynamic Node Jenkins CI/CD
-In this articles I’m gonna integrate k8s and Dynamic Master-Slave Architecture of Jenkins. First of all, a dockerfile will be pushed to Github along with the code that needs to be deployed.
-That code will be auto downloaded & the web pages that need to be deployed will be added to that Dockerfile.
-The Dockerfile will then be built and the image will be auto pushed to Github repo. Now, that image will be auto downloaded on a system running with Kubernetes. Then, a dynamic Jenkins slave container will auto run that has Kubectl pre configured.
-The task of this slave node will be to deploy this server image using Kubernetes. After the slave node has successfully deployed the pages on the server, the node will auto terminate.
-The deployment of pages using Kubernetes will also ensure Rolling Updates & hence, there will be no downtime while updating the codes.
Lets start …
First we are going to create a Dockefile of Apache web server which you can find in my docker hub repository link :
FROM centos:latest
RUN yum install sudo -y
RUN yum install /sbin/service -y
RUN yum install httpd -y
COPY *.html /var/www/html
CMD /usr/sbin/httpd -DFOREGROUND && /bin/bash
EXPOSE 80
Now we configured jenkins so that it downloads the code automatically from the GitHub.
Using build trigger as Poll SCM
2 . Now Jenkins task will build an image from this Dockerfile. This image would have all the code that needs to be deployed. This image will also be automatically pushed to Github.
3 . Now we have to create a docker container in which kubectl is pre-configured , so that we can use it as a dynamic slave to deploy the server image using Kubernetes.
Kube.repo
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
Now creating Dockerfile for the same
FROM centos:latest
COPY ./kube.repo /etc/yum.repos.d/
RUN yum install openssh-server -y
Run yum install java -y
RUN yum install kubectl -y
RUN yum install git -y
RUN yum install sudo -y
COPY ca.crt /root/
COPY client.crt /root/
COPY client.key /root/
COPY myinfo /root/.kube/
RUN ssh-keygen -A
CMD ["/usr/sbin/sshd", "-D"] && /bin/bash
pre-requisite : You need to have your kubectl configuration file in the same folder.
apiVersion: v1
kind: Config
clusters:
- cluster:
server: https://192.168.99.105:8443
certificate-authority: /root/ca.crt
name: mycluster
contexts:
- context:
cluster: mycl
user: samkituster
users:
- name: samkit
user:
client-key: /root/client.key
client-certificate: /root/client.crt
After creating the Dockerfile we have to build this file using command mention below :
docker build -t <name> /<location of Dockerfile>
We also have to make some changes in the Docker services . Go to -> /usr/lib/systemd/system/docker.service and open text editor . Change the part mention below :
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:4243
Restart your docker services:
systemctl daemon-reload
system restart docker
Now configure clouds from your jenkins portal. Do as follows :
Now create a job and check these options and mention the label
Finally you can see pods running now it will roll out updates when there is any change in the code.
Thanks for reading
Let’s connect on Linkedln .