TASK 4 DevOps AL-Jenkins Dynamic nodes and Kubernetes

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:

No alt text provided for this image

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:

No alt text provided for this image

The /root/task4 will be the working directory.The index.html will be stored in /var/www/html.




index.html:

No alt text provided for this image




Github repository after pushing files:

No alt text provided for this image

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.

No alt text provided for this image

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


No alt text provided for this image

STEP 3) Now launch Jenkins. And create job1




No alt text provided for this image
No alt text provided for this image

Provide the Github Repository Url here in the Git section of SCM from where code will be pulled.



No alt text provided for this image

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.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Now , if we update anything to code from CLI and add and commit. The push and build will be triggered automatically.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image
No alt text provided for this image

Now go to Manage Jenkins---> Manage Nodes & Clouds ---->Configure Clouds

No alt text provided for this image

And add a node with the name kubectl_node , here specify the IP. Specify the home directory.

No alt text provided for this image

Provide the image name that was created for Jenkins slave.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

The job1 will run successfully, building and pushing our image to the DockerHub.:

No alt text provided for this image

Now, job2 will be built launching a slave node to run this job.

No alt text provided for this image

After successful built of job2 our deployment will be launched hosting our site, ensuring 0 downtimes.

No alt text provided for this image

The site is hosted successfully. View the webpage using MiikubeIP:portno

No alt text provided for this image

Thats it, our setup is done.

Github URL:https://github.com/TanyaChetnaVaish/devaltask4

Ritesh Singh

DevOps Engineer | Graduate Student at Pace University

4 年

Well explained...Tanya Chetna Vaish

回复
Arsh Mishra

Data Scientist & Machine Learning Engineer | Building Scalable Models for Real-World Impact

4 年
回复

要查看或添加评论,请登录

Tanya Chetna Vaish的更多文章

社区洞察

其他会员也浏览了