Configuring a Kubernetes Pod as a Jenkins Agent
This guide explains how to set up a Kubernetes pod to act as a Jenkins agent, covering prerequisites, deployment YAML, commands to launch and verify the pod and service, and the Jenkins UI configuration needed to connect the pod as a scalable CI/CD worker.
Jenkins is an automation server that plays a crucial role in coordinating CI/CD pipelines, but traditional Jenkins agents have limitations in scalability and flexibility. Kubernetes, a container orchestration platform, can be used to run Jenkins agents as pods, enabling dynamic scaling, optimized resource usage, and simplified CI/CD workflows.
Prerequisites
Access to a Jenkins instance
A running Kubernetes cluster
Kubernetes services started
Create a simple deployment file that launches 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:
name: demo-service
spec:
ports:
- port: 2222
targetPort: 22
nodePort: 32222
selector:
app: demo
sessionAffinity: None
type: NodePortThe above file creates a pod named "demo" from the base image ubuntu18 , runs /usr/sbin/sshd -D to start an SSH daemon in debug mode, mounts the host's /home/demo directory into the pod, opens port 22 for SSH, and creates a NodePort service demo-service exposing port 32222 to the outside of the Kubernetes cluster.
Run the following command to launch the pod and service:
kubectl apply -f <yaml_file_name>Verify that the pod and service started successfully with:
kubectl get deploymentsConfigure Jenkins Agent
In the Jenkins console, navigate to Manage Jenkins → Manage Nodes and Clouds → New Node , provide a node name, select Permanent Agent , and click Create . Then fill in the following details and save:
Remote root directory: /home/demo/ (dedicated to this node)
Launch method: Launch agents via SSH
Host: Public IP address of the Kubernetes host
Credentials: Credentials of the user used to start the container
Host key verification strategy: Non-verifying strategy
Port: 32222 (the NodePort opened for external access)
JavaPath: /usr/lib/jvm/java-11-openjdk-amd64/bin/java (path to OpenJDK)
After creating the node, start it so that the Kubernetes pod functions as a Jenkins agent. Screenshots in the original article illustrate the node details and the final configuration.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.