Dynamic Slave [JENKINS] & Zero-DownTime Error [k8s]
To create a Dynamic slave for Jenkins which will build the job and terminate itself after the job completion.
Objectives
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.
REQUIREMENTS
- Base OS- Windows10 (k8s & GIT installed)
- VM - RHEL8 (Jenkins & Docker installed)
For configuring git, go through my this article-
Creating container by configuring kubectl command in it, which will be treated as slave for jenkins.
FROM ubuntu:16.04 RUN apt-get update && apt-get install -y openssh-server RUN mkdir -p /var/run/sshd RUN echo 'root:cartoon' | chpasswd RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config # SSH login fix. Otherwise user is kicked off after login RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd ENV NOTVISIBLE "in users profile" RUN echo "export VISIBLE=now" >> /etc/profile RUN apt-get install openjdk-8-jre -y RUN apt-get install curl -y RUN mkdir /root/.kube COPY ./.kube /root/.kube COPY ./ca.crt /root/ COPY ./client.crt /root/ COPY ./client.key /root/ 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/kubectl EXPOSE 22 CMD ["/usr/sbin/sshd", "-D"]
Now create the container for webserver to display our webpage.
FROM centos RUN yum install httpd -y RUN yum install php -y COPY *.php /var/www/html/ RUN echo "/usr/sbin/httpd" >> /root/.bashrc RUN source /root/.bashrc EXPOSE 80 CMD /usr/sbin/httpd -DFOREGROUND
For remotely accessing docker edit the "ExecStart=" line inside the file "/usr/lib/systemd/system/docker.service" by adding "-H tcp://0.0.0.0:4243"
It will allow all the ip addressess which will come at this port.
After making any changes in the configuration file we have to restart the services by commands "systemctl daemon-reload" & "systemctl restart docker.service".
Now from remote system enter the following command-
"export DOCKER_HOST=192.168.43.138:4243"
Before configuring our jobs we have to configure our cloud-
Adding Docker templates
SSH_configuration
Now our cloud is set...........
Now we have to configure the jobs.
JOB_1:
NOTE- It will pull all the files from the github in the directory /root/jenslave and perform docker build and docker push operations, which will build and then push the container to the docker registery (docker hub)
OUTPUT_of_JOB_1-
JOB_2:
-provide the label which is given to the cloud which will only activate that node .
Conclusion-
- To achieve the power of dynamic node , which will complete the job asssigned by the user and terminate automatically after the job finished.
- ZERO-DownTime Error : due to the k8s deployment service , the rollout of pods doesn't give Downtime error.
SnowPro Certified| Talend Administration| DataBricks Fundamentals Certified| ETL|Linux|AWS|SQL|Deep Learning|Machine Learning
4 年Great!
DevOps Engineer at Toorak Capital
4 年good job !