Integration of Kubernetes and Jenkins with Dynamic Docker Cluster
We live in a world where we have clear distinction of work boundaries where the task is equally distributed according to level of position and hierarchy.
Taking a cue from our society's division of labor , we have come up with the idea of creating a master-slave cluster wherein we will have our docker client as master and our docker server as the client.
Running Dockerfile with Kubernetes configured within it.
We will first create an image with kubernetes configured within it and also make it on top of our Docker client .
Dockerfile
Configuration File
I have already uploaded this image to my docker hub , from where U can access this image.
For more on its creation refer my previous article : -
Building our Docker cluster
First go to VM , whom you want to make the server and follow the images.
vim /usr/lib/systemd/system/docker.service
Change the lines to the highlighted one.
Come out of the filesystemctl daemon-reload
systemctl restart docker
Now go to the VM whom u want to make the client and whose Jenkins will contact the Docker server to get its task done.
Type the below CMD
export DOCKER_HOST=<ip_of_client>:4243
Configuring our Jenkins
Install docker and yet another docker plugin from Jenkins to configure your Jenkins cloud.
Now we are all set for our Task
TASK
So basically we will be creating our a dockerfile and any html code on our bare metal and push them both to GitHub . After we have pushed our code , Jenkins will contact docker slave, which will create our dockerfile and after its creation push it to docker hub.
We will then take this image and contact kubernetes which will then deploy our html code and moniter it.
Job1 :
Pull the Github repo automatically when some developers push repo to Github and perform the following operations as:
1. Create the new image dynamically for the application and copy the application code into that corresponding docker image
2. Push that image to the docker hub (Public repository)
( Github code contain the application code and Dockerfile to create a new image )
Create Dockerfile.
JOB 1.
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:
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.
2. If Application created first time, then Expose the application. Else don’t expose it.
Let’s Build and test our pipeline
Now since Job 1 is done let’s see our docker hub .
We see that our image is uploaded to Doucker Hub.
And also both our Jobs are completed successfully.
And our Site is all up and running.
Seeing our Kubectl Commands also show us that our pods are up and running
Now if we try to change our HTML code and test our Infrastucture , we see that it is stable.
Thanks for watching .
GitHub link :-
Let’s connect via LinkedIn :-