How to Setup Jenkins Build Agents on Kubernetes Pods

Reading Time: 5 minutes
Deploying and Scaling Jenkins on Kubernetes | by Anup Dubey | FAUN  Publication

Jenkins

Jenkins is an open-source automation tool written in Java with plugins built for Continuous Integration purposes.Therefore, It is used to build and test your software projects continuously making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build.

Kubernetes

Kubernetes (“K8s” for short) is an open source solution for automating the deployment and dynamic scaling of containerised online applications. 

Setting Up Jenkins Build Pods On Kubernetes

To work on this setup we need the following.

A working Kubernetes cluster.

A running Jenkins master

 Jenkins Kubernetes Plugin is required to set up Kubernetes-based build agents. Let’s configure the plugin.

Step 1: Install Jenkins Kubernetes Plugin

Go to Manage Jenkins –> Manage Plugins, search for Kubernetes Plugin in the available tab and install it.

and after that install the kubernetes plugin.

Step 2: Create a Kubernete Cloud Configuration

Once installed, go to Manage Jenkins –> Manage Node & Clouds

Click Configure Clouds

“Add a new Cloud” select Kubernetes.

Select Kubernetes Cloud Details

Here we have two scenarios.

  • Jenkins server running inside the same Kubernetes cluster
  • It’s server running out the Kubernetes cluster.

I’m considering one scenarios here

Jenkins server running inside the same Kubernetes cluster

Since we have inside the Kubernetes cluster with a service account to deploy the agent pods, we don’t have to mention the Kubernetes URL or certificate key.

However, to validate the connection using the service account, use the Test connection button as shown below. It should show a connected message if the pod can connect to the Kubernetes master API.

Step 3: we have to need to create one namespace

I will create namespace using name of capstone,follow this code

apiVersion: v1
kind: Namespace
metadata:
  name: capstone

and after that we need to create is a service account here is the command to create service account

kubectl create serviceaccount jenkins --namespace=capstone<namespace-name>

We have already created the service account in the capstone namespace. We need to get the token from the service account.

Execute the following commands to retrieve the secret name from the service account.

kubectl describe secret $(kubectl describe serviceaccount jenkins --namespace=capstone | grep Token | awk '{print $2}') --namespace=capstone

this is the token that we need so within this token i’m going to copy this and we are over to go back over to our controller and we are going to add a credentials.

Now click the Add button under credentials and create a credential type “Secret text“. Enter the service account token in the secret box and add other details as shown below. Finally, save the credential.

The kubernetes cloud configuration would look like the following.

After filling in all the details, you can test the connection to validate the Kubernetes cluster connectivity.

Step 4: Configure the Jenkins URL Details

For master running inside the cluster, you can use the Service endpoint of the Kubernetes cluster as the URL because agents pods can connect to the cluster via internal service DNS.

After filling in all the details, you can test the connection to validate the Kubernetes cluster connectivity.

The URL is derived using the following syntax.

http://<service-name>.<namespace>.svc.cluster.local:8080

In our case, the service DNS will be,

http://jenkins-service.devops-tools.svc.cluster.local:8080

Also, add the POD label that can be used for grouping the containers if required in terms of bulling or custom build dashboards.

Step 5: Create POD and Container Template

Next, you need to add the POD template with the details, as shown in the image below. The label kubeagent will be used in the job as an identifier to pick this pod as the build agent. Next, we need to add a container template with the Docker image details.

The next configuration is the container template. If you don’t add a container template, the Kubernetes plugin will use the default JNLP image from the Docker hub to spin up the agents.

Step 6: Go to Jenkins home –> New Item and create a freestyle project.

In the job description, add the label kubeagent as shown below. It is the label we assigned to the pod template. This way, it knows which pod template to use for the agent container.

Add a shell build step with an echo command to validate the job as shown below.

Now, save the job configuration and click “Build Now”

You should see a pending agent in the job build history as shown below.

In a couple of minutes, you will see a successful build. If you check the logs, it will show you the executed shell.

conclusion:

Hope this blog will help you out.you will see a successful build. If you check the logs, it will show you the executed shell.

Written by 

I am an enthusiastic , hard-working and determine girl with strong attention to detail and eager to learn about new technologies.