Integration of Kubernetes and Jenkins (Dynamic Slave)
Abhilash Sharma

Integration of Kubernetes and Jenkins (Dynamic Slave)

No alt text provided for this image

Git is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.


No alt text provided for this image

GitHub offers the distributed version control and source code management (SCM) functionality of Git, plus its own features. It provides access control and several collaboration features such as bug tracking, feature requests, task management, and wikis for every project.


No alt text provided for this image

Jenkins is a free and open source automation server. It helps automate the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery. It is a server-based system that runs in servlet containers such as Apache Tomcat


No alt text provided for this image

Kubernetes is an open-source container-orchestration system for automating computer application deployment, scaling, and management. It was originally designed by Google and is now maintained by the Cloud Native Computing Foundation.


No alt text provided for this image

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.

As while running Jenkins job, it requires resources but when we have large no of jobs then it’s impossible to run on master so we can use clustering in Jenkins. Through this, we can run those jobs in different slaves. In this, we can use different cloud platforms, Docker containers, etc which help in building these jobs.

From making docker container as a slave there are two types of clusters:

  • Static cluster
  • Dynamic cluster

Static cluster: For a static cluster, we can use any either a virtual machine or a docker container. But we have to manage it manually so that containers or virtual machines work with Jenkins. In this process, the container or machine will be running continuously and also consuming resources, whether it is working or not.

Dynamic cluster:Then comes to a dynamic cluster that helps in automation. It can automatically run a docker container when the job starts building and terminate that container after successfully building that job. So, it doesn’t consume many resources.

Setting up Dynamic Cluster of Jenkins:

The requirements are as follow:

  • RedHat Enterprise Linux Version 8
  • Docker Container Engine
  • Jenkins
  • Kubernetes
  • Git and GitHub

For setting our Dynamic cluster there are some prerequisites like:

  • Two virtual machines(VM) either CLI or GUI with RedHat installed, yum must be configured for installation of local available software and docker.
  • Jenkins installed in one VM, which will act as a Master node and Docker installed in another VM, which will act as a Slave Node.
  • Kubernetes installed, here I am using Minikube to run Kubernetes locally.
  • We need to install some plugins in Jenkins like GitHub, Build Pipeline, Docker, Yet Another Docker.

Note: This setup is done on the local system with, 8GB Ram at least is recommended for this entire setup.

Problem Statement :

1. Create a container image that has Linux and other basic configuration required to run Slave for Jenkins. ( eg. 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:

4.1 Create the new image dynamically for the application and copy the application code into that corresponding docker image

4.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.

Solution :

First we have to create the custom image in which Linux and other basic configuration required to run Slave for Jenkins ( example here we require kubectl to be configured ) as well as ssh already configured.

Dockerfile for the custom image :

No alt text provided for this image

Build custom docker image and push this image to the Docker Hub (Docker Hub is a service provided by Docker for finding and sharing container images with your team).

$ docker build -t dynamicslave:latest 
$ docker tag dynamicslave:latest abhi123/dynamicslave:latest
$ docker push abhi123/dynamicslave:latest

Now we setup the environment for dynamic jenkins cluster.Here we use two vm one is used for docker server and another is used for docker client.We have to change something in the service of docker in vm2.

$ systemctl status docker 
$ vim /usr/lib/systemd/system/docker.service
No alt text provided for this image

After adding this we have to restart the docker services.

$ systemctl daemon-reload
$ systemctl restart docker

Now we have to stop the docker services of another vm1 using systemctl stop docker. After this you need to export the DOCKER_HOST, so that you can use this VM(vm1) as a docker client.

$ export DOCKER_HOST=IP of VM2:port

Here we give the port number same as the port number which we change in docker service of vm2.

Now we have to configure the cloud environment

First you have to install the Docker plugin from manage plugins. Now go to

Manage jenkins–> Manage Node and Clouds –> configure cloud.\

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

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

No alt text provided for this image

Dockerfile for the web code:

FROM centos:latest

RUN yum install httpd -y

COPY web.html /var/www/html

CMD /usr/sbin/httpd -DFOREGROUND


EXPOSE 80
No alt text provided for this image

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.

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

?

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

Abhilash Sharma的更多文章

社区洞察

其他会员也浏览了