Ad Code

Ticker

6/recent/ticker-posts

DevOps - How to Scale Jenkins Master Slave Job Build on Kubernetes PODs Dynamically

Jenkins And Scalability 

In this post we are going to learn how to Scale Jenkins on Kubernetes. So let us talk brief about the Problem Statement here, with Standalone Jenkins server the issue is that the server can become overloaded with numerous jobs running at the same time. 

Anyway there is way to solve this problem by increasing the number of executors, but you soon end up hitting a performance limit. And to overcome this problem, you can offload some of the jobs to different machines called Jenkins agents.

Jenkins scaling is based on the controller/agents model, where a number of agent instances and one main Jenkins instance called the controller that is responsible mainly for distributing jobs across the agents. Jenkins agents run a small program that communicates with the Jenkins controller to check if there’s any job it can run. When Jenkins finds a job scheduled, it transfers the build to the agent.

Scaling Jenkins on Kubernetes

Advantages of scaling Jenkins on Kubernetes

Auto Healing is possible

If your build or your agent gets corrupted, you no longer need to worry — Jenkins will remove the unhealthy instance and spin up a new one.

Run builds in parallel

You no longer have to plan the executors and limit them; instead, Jenkins will spin up an agent instance and run your build in it.

Even load distribution

Kubernetes manages loads well, and it’ll make sure your Jenkins agents spin up in the best available server, which makes your builds faster and more efficient.

For this demo example we are going to use Docker Desktop Engine which runs the Kubernetes 



So lets Start with our implementation Steps here:
Step 1: Create a Dockerfile to install a Jenkins controller Agent with Jenkins base image, 

Step 2: Create the namespace  as "jenkins" and set it as current context using the below commands:

Step 3: Create the Deployment to run the Jenkins Controller Agent in jenkins namespace within the Cluster.

Step 4: Create the Service to expose the Deployment outside cluster and make it accessible.


Step 5: Now its time to apply the Deployment and Service using kubectl, We can see both Deployment and Service got created
PS C:\Users\rajek\OneDrive\Documents\teachlea\JenkinsOnK8s> kubectl apply -f .
deployment.apps/jenkins-master-deployment created
service/jenkins-master-service created
PS C:\Users\rajek\OneDrive\Documents\teachlea\JenkinsOnK8s>
Step 6: Validate the creation Deployment and Service, here we can see the POD got created successfully and Running with Jenkins Controller Agent Container.

Step 7: Now our Jenkins Controller Agent Dashboard should be up and accessible at port 30325:
http://localhost:30325/ 

Here is the Jenkins Dashboard , It will ask Administrator Password:


Step 8: You can get the Administrator Password in 2 ways 

    By just checking the deployment logs using command :kubectl logs deployment/jenkins-master-deployment  -n jenkins
It will give the Administrator Password as shown below:

    Next you can get Administrator Password  from /var/jenkins_home/secrets/initialAdminPassword inside the POD using command: kubectl exec -it jenkins-master-deployment-5d8558b578-hjdjr -- /bin/bash

Use the same Administrator Password and click on Continue. Next it will ask you to install Jenkins plugins according to how you will use them. You can choose either option, here I am using the Default one.

Next it will start the Plugin Installation for Jenkins Controller Agent, it may take some time to install all required plugins. So, Please wait until completed.

Next It will ask to set the User Credentials for Jenkins Controller Agent Dashboard


So with this we have able to Launch Successfully the Jenkins Controller Agent Dashboard which Running as container inside POD of K8s cluster.




Jenkins Agents Configuration

Now it’s time to configure Jenkins agents. But before that we need to installed the Kubernetes plugin, There are different ways to download and install the Kubernetes plugin for Jenkins Agents.

Here we are going to manually download it from Kubernetes plugin, And Install manully using and for this go to Path : http://localhost:30325/manage/pluginManager/advanced and upload the .hpi k8s plugin file 


 Kubernetes Plugin Configuration

Now, we are ready to fill in the Kubernetes plugin configuration. In order to do that, open the Jenkins UI and navigate to “Manage JenkinsManage Nodes and CloudsConfigure CloudsAdd a new cloudKubernetes and fill in the Kubernetes URL and Jenkins URL appropriately, by using the values which we have just collected in the previous step:




Here First we will fill and configure Kubernetes Cloud Details
PS C:\Users\rajek> kubectl cluster-info
Kubernetes control plane is running at https://kubernetes.docker.internal:6443
CoreDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

 Enter the namespace as "jenkins" and click on Test Connection. Here you will get the below Error:

To Solve this Issue we need to create as Service Account with clusterrolebinding using below command: kubectl create clusterrolebinding permissive-binding --clusterrole=cluster-admin --user=admin --user=kubelet --group=system:serviceaccounts

Test Connection again, this time it should get connect , Next do the below Configuration for Jenkins-URL, Pod Labels, Pod Templates and Container Details as shown below and Save the Configuration.



Now go to http://localhost:30325/manage/configure and do below Configuration and Save.


Now with the above configurations we have completed the Jenkins Cloud Kubernetes configurations, Next lets create a simple Slave Job using Freestyle Project

Inside SlaveJob Configuration we will just provide the build Step and try to print the Hello world from Host Name and Save it, 
Now go to the Dashboard you will able to see one SlaveJob is created
Now build the SlaveJob it should create the slave pod 

Summary:

Congratulations! We have have seen how to scale Jenkins Slave Job with on Kubernetes PODs Dynamically. To make it simpler in this example we have not created Persistent Volume and Persistent Volume Claim.

Creating a persistence volume is essential since all of your Jenkins jobs, plugins, configurations should be persisted. If one pod dies, then another new pod can continue with persistent data from your volume. “PersistentVolumeClaim” request for storage with a specific size and access mode.

The Steps will same even with PV and PVC just we need to create these 2 manifest. All the files can be found at the below Github link.

GitHub Link : rajivksingh13/teachlea
Please feel free to provide your valuable comments, Thanks.

Ref:

https://www.jenkins.io/doc/book/scaling/scaling-jenkins-on-kubernetes/




Post a Comment

0 Comments

Ad Code