Kubernetes Pod as a Jenkins Agent

Famidha Thurab
Make Android
Published in
3 min readFeb 10, 2024

This article walks you through configuring Kubernetes pod as a Jenkins agent

Photo by Afif Ramdhasuma on Unsplash

Jenkins, an automation server, plays a vital role in orchestrating CI/CD pipelines in the constantly evolving DevOps landscape. However, traditional Jenkins agents have limitations in scalability and flexibility. This is where Kubernetes comes in. Kubernetes is a container orchestration platform that is changing the way deployment and management are done. This article delves into how Jenkins and Kubernetes work together by using Kubernetes pods as Jenkins agents. This empowers teams to scale dynamically, optimize resource utilization, and streamline their CI/CD workflows.

Yes, you read it right, we can configure the Kubernetes pod as a Jenkins agent. Let us dive into the steps.

Prerequisites

Access to Jenkins

Running Kubernetes cluster

Launching Kubernetes service

Let uscreate a simple deployment file which will launch a pod and a service.

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo
labels:
app: demo
color: green
spec:
replicas: 1
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
color: green
spec:
containers:
- name: demo
image: ubuntu18:latest
command: ["/usr/sbin/sshd","-D"]
imagePullPolicy: IfNotPresent
volumeMounts:
- name: home
mountPath: /home/
ports:
- containerPort: 22
volumes:
- name: home
hostPath:
path: /home/demo/
---
apiVersion: v1
kind: Service
metadata:
annotations:
name: demo-service
spec:
ports:
- port: 2222
targetPort: 22
nodePort: 32222
selector:
app: demo
sessionAffinity: None
type: NodePort

The above file

creates a pod called “demo”

from a base image “ubuntu18” which is available on the host server with details of the user which will be used inside the container

executes command “/usr/bin/sshd -D” when the pod launched to launch the SSH daemon in debug mode

mounts home directory “/home/demo” of the host server with the pod home

opens port “22” for SSH connectivity

creates a service “demo-service”

opens nodePort “32222” making the service accessible from outside the Kubernetes cluster

opens port “2222”, an internal port of the service within the Kubernetes cluster

opens targetPort “22” in which the pod’s container is listening

maps nodePort, targetPort and port

Running the below command will launch the pod and the service

kubectl apply -f <yaml_file_name>

Check if the pod and the service have been launched successfully using the below command

kubectl get deployments

Configuring Jenkins agent

On the Jenkins console, go to “Manage Jenkins” -> “Manage Nodes and Clouds” -> “New Node”

Give the “Node name”, select “Permanent Agent” and click on “Create”.

Creating a new Jenkins node

Add the details such as below and save.

  1. Remote root directory: /home/demo/ (A dedicated directory for this node)
  2. Launch method: Launch agents via SSH
  3. Host: Public IP address of the host
  4. Credentials: Credentials of the user with which we are launching the container.
  5. Host Key Verification Strategy: Non-verifying Verification Strategy
  6. Port: 32222 (the nodePort which we opened for the service to be accessed outside the Kubernetes cluster)
  7. JavaPath: /usr/lib/jvm/java-11-openjdk-amd64/bin/java (path to the openJDK)
Node details
Node details

After creating the node, launch the node to have Kubernetes pod as Jenkins Agent.

Thanks for reading!!

--

--

Famidha Thurab
Make Android

DevOps Engineer| Editor Make Android | Writes on AIOps | Android IVI | AOSP | Docker | Kubernetes | AWS | Jenkins | Python | Shell | Git