Bird
Raised Fist0
Jenkinsdevops~10 mins

Kubernetes agents in Jenkins - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Kubernetes agents
Jenkins Master
↓
Request Agent Pod
↓
Kubernetes API
↓
Create Agent Pod
↓
Agent Pod Running
↓
Agent Executes Job
↓
Job Complete
↓
Agent Pod Deleted
Jenkins master requests a Kubernetes agent pod, which is created and runs the job, then is deleted after completion.
Execution Sample
Jenkins
podTemplate(label: 'k8s-agent') {
  node('k8s-agent') {
    stage('Build') {
      echo 'Running build on Kubernetes agent'
    }
  }
}
Defines a Kubernetes agent pod template and runs a build stage on that agent.
Process Table
StepActionEvaluationResult
1Jenkins master requests agent pod with label 'k8s-agent'Request sent to Kubernetes APIAgent pod creation started
2Kubernetes API creates pod based on templatePod spec validAgent pod is running
3Jenkins master schedules job on agent podAgent pod readyJob starts executing
4Job runs 'echo' commandCommand executesOutput: 'Running build on Kubernetes agent'
5Job completesSuccessAgent pod marked for deletion
6Kubernetes deletes agent podPod terminatedAgent pod removed
💡 Agent pod deleted after job completion, freeing resources
Status Tracker
VariableStartAfter Step 2After Step 4Final
agentPodStatusNot createdRunningRunningDeleted
jobStatusNot startedNot startedRunningCompleted
jobOutputRunning build on Kubernetes agentRunning build on Kubernetes agent
Key Moments - 2 Insights
Why does Jenkins create a new pod for each agent instead of reusing one?
Each agent pod is created fresh to ensure a clean environment for the job, as shown in execution_table steps 1 and 2 where a new pod is created and started.
What happens if the agent pod fails to start?
If the pod fails to start, Jenkins cannot run the job on it, so the job will fail or retry. This is implied in step 3 where the job starts only if the pod is ready.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the agentPodStatus after step 2?
ANot created
BDeleted
CRunning
DPending
💡 Hint
Check the variable_tracker table under agentPodStatus after Step 2
At which step does the job output 'Running build on Kubernetes agent' appear?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the execution_table row describing the job running the echo command
If the agent pod was not deleted after job completion, what would change in the variable_tracker?
AagentPodStatus would remain 'Running' or 'Terminated' instead of 'Deleted'
BjobStatus would be 'Failed'
CjobOutput would be empty
DNo change in any variable
💡 Hint
Refer to the final value of agentPodStatus in variable_tracker
Concept Snapshot
Kubernetes agents in Jenkins:
- Jenkins master requests a pod from Kubernetes
- Pod is created fresh per job using podTemplate
- Job runs inside the pod as an agent
- Pod is deleted after job completes
- Ensures clean, isolated build environments
Full Transcript
This visual execution shows how Jenkins uses Kubernetes agents. First, Jenkins master requests an agent pod labeled 'k8s-agent'. Kubernetes API creates the pod based on the podTemplate. Once the pod is running, Jenkins schedules the job on it. The job runs the build stage, printing 'Running build on Kubernetes agent'. After the job finishes successfully, the agent pod is deleted to free resources. Variables like agentPodStatus and jobStatus change accordingly during these steps. This process ensures each job runs in a clean, isolated environment.

Practice

(1/5)
1. What is the main purpose of using Kubernetes agents in Jenkins?
easy
A. To run Jenkins jobs inside isolated pods on a Kubernetes cluster
B. To store Jenkins job logs permanently on the master node
C. To replace Jenkins pipelines with Kubernetes scripts
D. To run Jenkins jobs only on physical machines

Solution

  1. Step 1: Understand Kubernetes agents role

    Kubernetes agents run Jenkins jobs inside pods, providing isolation and scalability.
  2. 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.
  3. Final Answer:

    To run Jenkins jobs inside isolated pods on a Kubernetes cluster -> Option A
  4. Quick Check:

    Kubernetes agents = isolated pods for Jenkins jobs [OK]
Hint: Kubernetes agents run jobs inside pods, not on master node [OK]
Common Mistakes:
  • Thinking agents run jobs on master node
  • Confusing Kubernetes agents with storage solutions
  • Assuming Kubernetes replaces Jenkins pipelines
2. Which Jenkins pipeline syntax correctly defines a Kubernetes agent using podTemplate?
easy
A. containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent') { podTemplate { node { /* steps */ } } }
B. node { podTemplate(containers: [containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent')]) { /* steps */ } }
C. podTemplate(containers: [containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent')]) { node { /* steps */ } }
D. podTemplate { node(containers: ['jnlp']) { /* steps */ } }

Solution

  1. Step 1: Identify correct podTemplate usage

    The podTemplate block wraps the node block and defines containers properly.
  2. Step 2: Validate syntax correctness

    podTemplate(containers: [containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent')]) { node { /* steps */ } } correctly places podTemplate outside node and defines containerTemplate properly.
  3. Final Answer:

    podTemplate(containers: [containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent')]) { node { /* steps */ } } -> Option C
  4. Quick Check:

    podTemplate wraps node with containerTemplate [OK]
Hint: podTemplate wraps node, containerTemplate inside podTemplate [OK]
Common Mistakes:
  • Placing podTemplate inside node block
  • Using containerTemplate outside podTemplate
  • Incorrect nesting of blocks
3. Given this Jenkins pipeline snippet, what will be the output if the pod agent is correctly configured?
podTemplate(containers: [containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent')]) {
  node {
    stage('Test') {
      echo 'Running inside Kubernetes agent'
    }
  }
}
medium
A. The pipeline will fail because podTemplate is missing a label
B. The message 'Running inside Kubernetes agent' will be printed in the Jenkins console log
C. Nothing will print because echo is not supported inside podTemplate
D. The pipeline will run on the Jenkins master node ignoring podTemplate

Solution

  1. Step 1: Understand podTemplate and node behavior

    The podTemplate defines the Kubernetes pod agent, and node runs the steps inside it.
  2. Step 2: Analyze echo command inside stage

    The echo command prints the message to the Jenkins console log during the 'Test' stage.
  3. Final Answer:

    The message 'Running inside Kubernetes agent' will be printed in the Jenkins console log -> Option B
  4. Quick Check:

    echo inside podTemplate prints message [OK]
Hint: echo prints messages in Jenkins console inside podTemplate [OK]
Common Mistakes:
  • Assuming echo won't work inside podTemplate
  • Thinking podTemplate requires label always
  • Confusing podTemplate with running on master node
4. You wrote this Jenkins pipeline but it fails with "No nodes available" error:
podTemplate(containers: [containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent')]) {
  stage('Build') {
    node {
      echo 'Building...'
    }
  }
}

What is the likely cause?
medium
A. The containerTemplate name 'jnlp' is invalid
B. The stage block is outside the node block
C. The node block is outside the podTemplate block
D. The node block is missing a label matching the podTemplate

Solution

  1. Step 1: Check node block label requirement

    When using podTemplate, the node block must specify the label matching the podTemplate to allocate the pod.
  2. Step 2: Identify missing label causes no node available

    Without label, Jenkins cannot find the Kubernetes agent pod, causing the error.
  3. Final Answer:

    The node block is missing a label matching the podTemplate -> Option D
  4. Quick Check:

    node label missing = no Kubernetes pod allocated [OK]
Hint: Always specify node label matching podTemplate to avoid no nodes error [OK]
Common Mistakes:
  • Not specifying node label
  • Misplacing node and stage blocks
  • Assuming containerTemplate name causes error
5. You want to run a Jenkins job on a Kubernetes agent with two containers: one for building with Maven and one for running tests with Node.js. Which pipeline snippet correctly defines this setup?
hard
A. 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' } } } }
B. podTemplate(containers: [containerTemplate(name: 'maven', image: 'maven:3.8.1')]) { node { stage('Build') { sh 'mvn clean install' } stage('Test') { container('nodejs') { sh 'npm test' } } } }
C. podTemplate(containers: [containerTemplate(name: 'nodejs', image: 'node:16')]) { node { stage('Build') { container('maven') { sh 'mvn clean install' } } stage('Test') { sh 'npm test' } } }
D. podTemplate(containers: [containerTemplate(name: 'maven', image: 'maven:3.8.1'), containerTemplate(name: 'nodejs', image: 'node:16')]) { node { stage('Build') { sh 'mvn clean install' } stage('Test') { sh 'npm test' } } }

Solution

  1. 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.
  2. 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' } } } } uses container('name') to run build and test commands inside respective containers.
  3. Final Answer:

    Option A correctly defines two containers and uses container blocks for commands -> Option A
  4. Quick 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]
Hint: Use container('name') block to run commands in specific pod containers [OK]
Common Mistakes:
  • Not defining all containers in podTemplate
  • Running commands outside container blocks
  • Mixing container names or missing container blocks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard