Kubernetes Pod as a Jenkins Agent
This article walks you through configuring Kubernetes pod as a Jenkins agent
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”.
Add the details such as below and save.
- Remote root directory: /home/demo/ (A dedicated directory for this node)
- Launch method: Launch agents via SSH
- Host: Public IP address of the host
- Credentials: Credentials of the user with which we are launching the container.
- Host Key Verification Strategy: Non-verifying Verification Strategy
- Port: 32222 (the nodePort which we opened for the service to be accessed outside the Kubernetes cluster)
- JavaPath: /usr/lib/jvm/java-11-openjdk-amd64/bin/java (path to the openJDK)
After creating the node, launch the node to have Kubernetes pod as Jenkins Agent.
Thanks for reading!!