Jenkins Master Slave on K8s
2 min readMay 16, 2019
Introduction
Jenkins Master-Slave architecture has been designed for distributed builds. So, the workload will be distributed to slaves and master act as a managing and scheduling node. Here we’ll discuss implementing Jenkins Master-Slave setup on K8s.
Prerequisites
- An up and running K8s cluster.
- Running and accessible Jenkins service on K8s cluster.
- Jenkins should have Kubernetes plugin installed.
How ToDo
- Navigate to Jenkins -> Configure System and configure your K8s cluster as shown below in Kubernetes plugin. Here I have deployed Jenkins in jenkins namespace.
- Click on Test Connection and confirm cluster is accessible to Jenkins.
- Scroll down to Kubernetes Pod Template and configure it with jnlp pod template as shown below.
- jnlp.yml
apiVersion: v1
kind: Pod
metadata:
labels:
app: jnlp
name: jnlp
spec:
containers:
- image: jenkins/jnlp-slave:3.27-1-alpine
imagePullPolicy: IfNotPresent
name: jnlp
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
- Finally, click on Save and navigate to Jenkins Home page
- Create a Pipeline job.
New Item -> Enter an item name -> Pipeline -> OK - Fill Pipeline script box in Pipeline section with below script.
pipeline {
agent {
kubernetes {
cloud "kubernetes" // Cloud Kubernetes Name
label "jnlp" // jnlp pod template label
}
}
stages {
stage("Print Env") {
steps {
container("jnlp") {
sh "printenv"
sh "sleep 60"
}
}
}
}
}
- Run the job and monitor the Kubernetes. You could see jnlp slave running pods. Meanwhile Jenkins UI as well.
$ kubectl get po -n jenkinsNAME READY STATUS RESTARTS AGEjenkins-85544876-p4mfz 1/1 Running 0 57mjnlp-6cfdt 1/1 Running 0 11s
Writing feedback is Welcome.