Jenkins plugin to run dynamic agents in a Kubernetes cluster.
Based on the Scaling Docker with Kubernetes article, automates the scaling of Jenkins agents running in Kubernetes.
The plugin creates a Kubernetes Pod for each agent started, and stops it after each build.
Agents are launched as inbound agents, so it is expected that the container connects automatically to the Jenkins controller. For that some environment variables are automatically injected:
JENKINS_URL : Jenkins web interface url
JENKINS_SECRET : the secret key for authentication
JENKINS_AGENT_NAME : the name of the Jenkins agent
JENKINS_NAME : the name of the Jenkins agent (Deprecated. Only here for backwards compatibility)
Tested with jenkins/inbound-agent, see the Docker image source code.
It is not required to run the Jenkins controller inside Kubernetes.
How to Setup Jenkins Build Agents on Kubernetes Pods
Before getting into the implementation, let’s understand how this setup works.
Whenever you trigger a Jenkins job, the Jenkins Kubernetes plugin will make an API call to create a Kubernetes agent pod. Then, the Jenkins agent pod gets deployed in the kubernetes with few environment variables containing the Jenkins server details and secrets.
When the agent pod comes up, it used the details in its environment variables and talks back to Jenkins using the JNLP method. The following images show the environment variables of the agent pod.
All the build steps from the Jenkinsfile run on that pod. Once the build is complete, the pod will get terminated automatically. There are also options to retain the build pod.
The Jenkins Kubernetes plugin takes care of all the communication from Jenkins to the Kubernetes cluster.
Also, as long as your Kubernetes cluster scales, you can scale your Jenkins build agents without any issues.