Web Server with Live Updates Roll Out using Jenkins and Kubernetes
>>> TASK:
- 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 ).
- When we launch the job it should automatically starts 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:
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.
Github Link :-
https://github.com/skabhi001/dev-task4
We need to install these plugins in Jenkins:
Github
Docker
- We need to allow docker daemon to be accessed remotely. For that, we need to edit /usr/lib/systemd/system/docker.service.
- The following has been appended to ExecStart. You can bind it with any available port. 0.0.0.0 denotes any IP that will be given to the host.
→ -H tcp://0.0.0.0:1234
- Then restart the services.
→ systemctl daemon-reload
→systemctl restart docker.service
- Now create your docker image using the Dockerfile as:
FROM centos
RUN yum install sudo -y
RUN yum install java -y
RUN yum install openssh-server -y
RUN /usr/sbin/sshd -D &
RUN ssh-keygen -A
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 mkdir /root/.kube /root/jenkins
RUN sudo mv ./kubectl /usr/local/bin/kubectl
COPY ca.crt client.crt client.key config mydeploy.yml /root/
RUN mv /root/config /root/.kube/config
RUN yum install httpd -y
RUN yum install php -y
COPY index.html /var/www/html/
EXPOSE 80
CMD /usr/sbin/httpd -DFOREGROUND
- Now we need to configure our config file as :
apiVersion: v1
kind: Configclusters:
- cluster:
server: https://192.168.99.100:8443
certificate-authority: /root/ca.crt
name: minikubecontexts:
- context:
cluster: minikube
user: abhishekusers:
- name: abhishek
user:
client-key: /root/client.key
client-certificate: /root/client.crt
- Now create your deployment file as:
→Create JOB-1 in Jenkins :
- Build a github hooks which will inform jenkins as soon as developer upload code to github.
- Image is successfully uploaded to docker hub :
- Now Create JOB-2 in Jenkins :
Now Configure Cloud for launching Dynamic slave of Jenkins :
Build Pipeline for job1 and job2 :
JOB2 successfully running :