DevOps AL task 4
In this task, task 3 is done using dynamic Jenkins cluster. The Jenkins master acts to schedule the jobs and assign slaves and send builds to slaves to execute the jobs. It will also monitor the slave state (offline or online) and getting back the build result responses from slaves and the display build results on the console output.
TASK DESCRIPTION:-
Create A dynamic Jenkins cluster and perform task-3 using the dynamic Jenkins cluster.
Steps to proceed as:
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:
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 )
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:
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.
In this task, I have used two rhel8 VMs. One working as master(192.168.99.103) and another as slave(192.168.99.104).
NOTE:- Make sure both VMs are in same network.
Now the first thing we have to do is edit /usr/lib/systemd/system/docker.service file so that it can accept all the IPs on the given port as shown below:-
After this we have to run following commands:-
systemctl restart docker systemctl enable docker
After this if we run the command -> systemctl status docker, it will look like this
Exporting docker host so that slave(104) can connect to it:-
export DOCKER_HOST=192.168.99.103:4243
Now, we need to make a image which has all requirements needed to run jenkins-slave. For this we have to make a Dockerfile:-
Requirements to run jenkins-slave are -
1). Linux
2). SSH enabled
3). kubectl
4). user/password
5). Workspace
This is apache.yml file :-
This is config file:-
All the certificates and keys i.e., ca.crt, client.crt and client.key, will be transferred to the workspace using winscp.
Now it's time to build and push the image using command:-
docker build -t dockerhub_username/imagename:version .\ docker push dockerhub_username/imagename:version
After successfully push the image to dockerhub, we will start configuring jobs in Jenkins
JOB 1: 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 )
To create webhook so that job triggers as soon as some developer push code to github I have used ngrok.
GitHub link for Dockerfile and application ocde is given below:-
Now before configuring job 2 we will configure docker cloud.
NOTE:- We will need docker plugin to configure docker cloud.
Notice ip of slave node is given in Docker Host URI. We can test connection by clicking on it. If we everything is good then it will show version.
Here, I have given jenkins-slave image which is build above.
In SSH credentials we will give the credentilas which are configured in jenkins-slave image.
Job 2: ( 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.
Here, label which was given while configuring docker cloud will be given and triggering the job after job 1 has been build.
If the deployment already exists then update otherwise create and expose.
Now everything is done. As soon as developer pushes the code to github, job 1 automatically gets and this is the ouput of job 1:-
Ouput of job 2:-
Output when deployment created first time:-
After updating:-
Build pipeline view:-
Thanks!