TASK 4 DevOps AL-Jenkins Dynamic nodes and Kubernetes
PROBLEM STATEMENT:
Create A dynamic Jenkins cluster and perform task-3 using the dynamic Jenkins cluster. Steps to proceed are:
1) Create a container image that 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 start the job on slave based on the label provided for a dynamic approach.
3) Create a job chain of job1 & job2 using the build pipeline plugin in Jenkins
4) Job1: a) Pull the Github repo automatically when some developers push the repo to Github and perform the following operations as:
b) Create the new image dynamically for the application and copy the application code into that corresponding docker image
c) 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:
a) If launching the first time then create a deployment of the pod using the image created in the previous job. Else if deployment already exists then do a rollout of the existing pod making zero downtime for the user.
b) If Application created the first time, then expose the application. Else don’t expose it.
Assumptions: VirtualBox is installed for virtualization.RHEL 8 VM is already setup and configured. The VM must have internet connectivity. The base system must have Internet connectivity. The Base system, here Windows must have Kubernetes setup in CLI. Jenkins must be set up in RHEL8 VM and Docker as well. Git bash is installed in Windows.
STEPS:
STEP 1) Create a Github repository, and initialize it. Add to files to this repository-Dockerfile and index.html.Dockerfile will be used to create a Docker image to host webserver and index.html is the webpage to be hosted. Add, commit, and push this code to Github in the master branch.
Dockerfile:
The /root/task4 will be the working directory.The index.html will be stored in /var/www/html.
index.html:
Github repository after pushing files:
STEP 2) Log in to Rhel8 VM. And create a folder. Create s Dockerfile to build an image that has Linux and other basic configuration required to run Slave for Jenkins. It must have kubectl configured. Make sure to copy the client.crt,ca.crt,client.key, and config .yml are also present in the same folder as Dockerfile. Copy these files from Windows to RHEL8 using WinSCP.
Now build this image and push it onto DockerHub:
docker build -t tanychetnavaish/jenkins-slave-kub:v1 docker login docker push tanyachetnavaish/jenkins-slave-kub:v1
STEP 3) Now launch Jenkins. And create job1
Provide the Github Repository Url here in the Git section of SCM from where code will be pulled.
Creating a post-commit trigger: Specify the token in Trigger builds remotely section in Build triggers. Now in git bash, get to the .git/hooks folder and create a post-commit file that will push the code on GitHub and build job1, after each commit by the user.
Now , if we update anything to code from CLI and add and commit. The push and build will be triggered automatically.
In Execute shell of job1 write the following code. It will build an image that will host our HTML webpage, using Dockerfile we pushed to Github.
STEP 4) Now go to the following folder and make changes to the docker. service file. In it specify the tcp://0.0.0.0:4243 in order to configure slave on Jenkins.
Now go to Manage Jenkins---> Manage Nodes & Clouds ---->Configure Clouds
And add a node with the name kubectl_node , here specify the IP. Specify the home directory.
Provide the image name that was created for Jenkins slave.
Specify the connection method , here its via SSH. Provide the login credentials for Jenkins..Make sure to specify the non-verifying Verification Strategy.
STEP 5) Now make job2, provide the following configuration.
Restrict the job to be run on the kubectl_node only. Add a build trigger to this job so that it builds only if job1 builds successfully.
Now this will create a Deployment with the name task4. It will have 3 pods. Deployment setup will thus ensure 0 downtimes, in case of any updation from the developer side.
STEP 6) Now create a Build Pipeline. For this make sure that build Pipeline plugin is downloaded. Provide the job1 name as the initial job. Build the job1.
The job1 will run successfully, building and pushing our image to the DockerHub.:
Now, job2 will be built launching a slave node to run this job.
After successful built of job2 our deployment will be launched hosting our site, ensuring 0 downtimes.
The site is hosted successfully. View the webpage using MiikubeIP:portno
Thats it, our setup is done.
DevOps Engineer | Graduate Student at Pace University
4 年Well explained...Tanya Chetna Vaish
Data Scientist & Machine Learning Engineer | Building Scalable Models for Real-World Impact
4 年Great Tanya Chetna Vaish ??