Kubernetes agents in Jenkins - Time & Space Complexity
We want to understand how the time it takes to run Jenkins jobs on Kubernetes agents changes as we add more jobs or agents.
How does the system handle more work and what costs grow with more agents?
Analyze the time complexity of the following Jenkins pipeline code that runs jobs on Kubernetes agents.
pipeline {
agent {
kubernetes {
label 'k8s-agent'
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: jnlp
image: jenkins/inbound-agent
'''
}
}
stages {
stage('Build') {
steps {
sh 'make build'
}
}
}
}
This pipeline runs a build stage on a Kubernetes pod agent created for the job.
Look for repeated actions that affect time.
- Primary operation: Creating a Kubernetes pod agent for each job run.
- How many times: Once per job execution, repeated for each job in the queue.
As the number of jobs (n) increases, the system creates more pod agents, each taking time to start.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 pod creations and job runs |
| 100 | 100 pod creations and job runs |
| 1000 | 1000 pod creations and job runs |
Pattern observation: The time grows roughly in direct proportion to the number of jobs because each job needs its own pod agent.
Time Complexity: O(n)
This means the total time grows linearly as you add more jobs, since each job requires creating and running on a separate Kubernetes agent pod.
[X] Wrong: "Adding more jobs will not increase total time because pods run in parallel."
[OK] Correct: While pods can run in parallel, the cluster resources and Jenkins controller limits mean pods may queue or delay, so total time still grows with more jobs.
Understanding how Jenkins uses Kubernetes agents helps you explain how cloud resources scale with workload, a key skill in DevOps roles.
What if we changed from creating a new pod agent per job to reusing a fixed number of pod agents? How would the time complexity change?
Practice
Solution
Step 1: Understand Kubernetes agents role
Kubernetes agents run Jenkins jobs inside pods, providing isolation and scalability.Step 2: Compare options
Only To run Jenkins jobs inside isolated pods on a Kubernetes cluster correctly describes running jobs inside pods on Kubernetes clusters.Final Answer:
To run Jenkins jobs inside isolated pods on a Kubernetes cluster -> Option AQuick Check:
Kubernetes agents = isolated pods for Jenkins jobs [OK]
- Thinking agents run jobs on master node
- Confusing Kubernetes agents with storage solutions
- Assuming Kubernetes replaces Jenkins pipelines
podTemplate?Solution
Step 1: Identify correct podTemplate usage
ThepodTemplateblock wraps thenodeblock and defines containers properly.Step 2: Validate syntax correctness
podTemplate(containers: [containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent')]) { node { /* steps */ } } correctly placespodTemplateoutsidenodeand defines containerTemplate properly.Final Answer:
podTemplate(containers: [containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent')]) { node { /* steps */ } } -> Option CQuick Check:
podTemplate wraps node with containerTemplate [OK]
- Placing podTemplate inside node block
- Using containerTemplate outside podTemplate
- Incorrect nesting of blocks
podTemplate(containers: [containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent')]) {
node {
stage('Test') {
echo 'Running inside Kubernetes agent'
}
}
}Solution
Step 1: Understand podTemplate and node behavior
The podTemplate defines the Kubernetes pod agent, and node runs the steps inside it.Step 2: Analyze echo command inside stage
The echo command prints the message to the Jenkins console log during the 'Test' stage.Final Answer:
The message 'Running inside Kubernetes agent' will be printed in the Jenkins console log -> Option BQuick Check:
echo inside podTemplate prints message [OK]
- Assuming echo won't work inside podTemplate
- Thinking podTemplate requires label always
- Confusing podTemplate with running on master node
podTemplate(containers: [containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent')]) {
stage('Build') {
node {
echo 'Building...'
}
}
}What is the likely cause?
Solution
Step 1: Check node block label requirement
When using podTemplate, the node block must specify the label matching the podTemplate to allocate the pod.Step 2: Identify missing label causes no node available
Without label, Jenkins cannot find the Kubernetes agent pod, causing the error.Final Answer:
Thenodeblock is missing a label matching the podTemplate -> Option DQuick Check:
node label missing = no Kubernetes pod allocated [OK]
- Not specifying node label
- Misplacing node and stage blocks
- Assuming containerTemplate name causes error
Solution
Step 1: Define multiple containers in podTemplate
podTemplate(containers: [containerTemplate(name: 'maven', image: 'maven:3.8.1'), containerTemplate(name: 'nodejs', image: 'node:16')]) { node { stage('Build') { container('maven') { sh 'mvn clean install' } } stage('Test') { container('nodejs') { sh 'npm test' } } } } correctly defines two containers: 'maven' and 'nodejs' with proper images.Step 2: Use container block to run commands in specific containers
podTemplate(containers: [containerTemplate(name: 'maven', image: 'maven:3.8.1'), containerTemplate(name: 'nodejs', image: 'node:16')]) { node { stage('Build') { container('maven') { sh 'mvn clean install' } } stage('Test') { container('nodejs') { sh 'npm test' } } } } usescontainer('name')to run build and test commands inside respective containers.Final Answer:
Option A correctly defines two containers and uses container blocks for commands -> Option AQuick Check:
Multiple containers + container blocks = podTemplate(containers: [containerTemplate(name: 'maven', image: 'maven:3.8.1'), containerTemplate(name: 'nodejs', image: 'node:16')]) { node { stage('Build') { container('maven') { sh 'mvn clean install' } } stage('Test') { container('nodejs') { sh 'npm test' } } } } [OK]
- Not defining all containers in podTemplate
- Running commands outside container blocks
- Mixing container names or missing container blocks
Ready to prove it?
Pass the test and this pattern moves unknown
