Deploying Web-Server on Kubernertes having Rolling updates using Dynamic Slave Node on Jenkins
Create a WebServer Setup on Kubernetes using the Dynamic Slave Node of Jenkins.
Steps :
1. Create a Dockerfile having SSH and Kubectl configured
2. Configure the Docker server so that Jenkins Docker Plugin can communicate
3. Use the created Dockerfile as the Dynamic Node Template, which can be used by specifying certain labels
4. Create another Dockerfile which acts as a webserver image for the Kubernetes Pods
5. Create Deployment/ RollOut Update to the Kubernetes using the Dynamic Slave.
A) 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 roll-out of the existing pod making zero downtime for the user.
B) If Application created first time, then Expose the application. Else don’t expose it.
Solution :
Create an Image from the Dockerfile having Kubectl configuration file in it, which can be used as a template for creating dynamic slave node.
Kubectl Configuration file:
Dynamic Slave Node Dockerfile Template:
Now use < docker build -t imagename path_of_dockerfile to build the image. In my case the image name is mykube and the Dockerfile is in the current directory, so that command would be:
docker build -t mykube .
Configuring Dynamic Slave Node : First allow Jenkins to communicate with Docker server, for accomplish this, we need to edit /usr/lib/systemd/system/docker.service. Add the following to the ExecStart field: -H tcp://0.0.0.0:4243
Then restart the services :
systemctl daemon-reload systemctl restart docker.service
You can use any free port in place of 4243. Now after this we have to add the dynamic node to the Jenkins for this : Go to Manage Jenkins > Manage Nodes and Clouds > Configure Clouds > Add A New Cloud > Select Docker
The HostURI field contains the tcp://ip:port of the docker server.
We have provided all the necessary parameters like image name, ssh credentials. Since I am using Local images so we have selected Never Pull strategy here. In Usage field select Only Build job with label expressions matching, so that this node will only be created when the labels are specified in the job.
JOB 1 : Create a job in the Jenkins, that will pull the code from the Github (having the Dockerfile of webserver), which builds and uploads the image to Docker Hub registery.
Dockerfile of Webserver:
Image Pushed:
JOB 2 : This job will be executed on the dynamic slave node which will be used to Create Deployment/Roll Update for Webserver on Kubernetes. This is an downstream job for kubejob1.
Here we have selected the option Restrict where this project can be run and provided the label we have used for the dynamic node: docker, So that this job will be executed on the dynamic node.
Running this job for the first time would create a new Deployment on Kubernetes:
Whenever the images or code for the website is updated , the Job1 will again fetch the code, create a new webserver image(with new code) and a Rolling Update will be started on the existing deployment (using Job2), so that the pods will be updated without any downtime.
Github :
All the code and Dockerfiles are present in the github repo :
Systems Engineer at Tata Consultancy Services
4ySuperb😎😎
Systems Engineer at Tata Consultancy Services
4yHighly appreciated
Consultant at Genpact
4y👍👍