Zero Downtime Using Kubernetes
As it is a continuation of my previous task without the use of k8s . This is with the use of kubernetes.
Problem statement :
1. Create container image that’s 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 starts job on slave based on the label provided for dynamic approach.
3. Create a job chain of job1 & job2 using build pipeline plugin in Jenkins
4. Job1 : Pull the Github repo automatically when some developers push repo to Github and perform the following operations as:
4.1 Create the new image dynamically for the application and copy the application code into that corresponding docker image
4.2 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 Kubernetes cluster performing following operations:
5.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.
5.2 If Application created first time, then Expose the application. Else don’t expose it.
JOB1(Git Pull):
This Job pull the code o get the code as soon as the developer uploads which includes one Dockerfile as well this time and I copied those files to on folder.
JOB2:
This Job2 on Jenkins runs on the dynamic slave of Jenkins configured with Kubernetes kubectl command
My dockerfile code
FROM centos
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 mv kubectl /usr/bin/
COPY ca.crt /root/
COPY client.crt /root/
COPY client.key /root/
COPY myinfo /
ADD myinfo /.kube/config
RUN yum install java-11-openjdk.x86_64 -y
RUN yum install git -y
RUN yum install openssh-server -y
RUN ssh-keygen -A
ADD ssh /etc/ssh/ssh
RUN echo root:redhat | chpasswd
EXPOSE 20
CMD ["/usr/sbin/sshd","-D"]
Also I’ve created a configuratio file which provides the permission to run kubectl command
apiVersion: v1
kind: Config
clusters:
- cluster:
server: https://192.168.99.104:8291
certificate-authority: /root/ca.crt
name: mycluster
contexts:
- context:
cluster: cluster1
user: samkit
users:
- name:
user:
client-key: /root/client.key
client-certificate: /root/client.crt
After this we build this Dockerfile
Next step is to configure Node and clouds
Cloud has been configured
I have created a deployment of the pod .I created job such that if deployment already exists then rollout of the existing pod making zero downtime for the user.
Job2 console output
Build pipeline
Thanks for reading !