0
0
Jenkinsdevops~10 mins

Kubernetes agents in Jenkins - Step-by-Step Execution

Choose your learning style9 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.