📖 Scenario: You are setting up a Jenkins pipeline that runs jobs on Kubernetes agents. This allows Jenkins to create fresh, isolated environments for each job automatically.
🎯 Goal: Build a Jenkins pipeline script that defines a Kubernetes agent with a specific container image, runs a simple shell command inside the container, and prints the output.
📋 What You'll Learn
Create a Jenkins pipeline with a Kubernetes agent block
Specify the container image alpine:3.14 for the agent
Run a shell command echo Hello from Kubernetes agent inside the container
Print the command output in the Jenkins console
💡 Why This Matters
🌍 Real World
Many companies use Jenkins with Kubernetes agents to run CI/CD jobs in isolated, scalable environments.
💼 Career
Understanding Kubernetes agents in Jenkins is valuable for DevOps engineers managing cloud-native build pipelines.
Progress0 / 4 steps
1
Define a basic Jenkins pipeline
Create a Jenkins pipeline script with a pipeline block and an empty agent section.
Jenkins
Hint
Start with the pipeline block and add an empty agent block inside it.
2
Configure the Kubernetes agent with container image
Modify the agent block to use kubernetes with a containerTemplate specifying the container image alpine:3.14 and container name alpine.
Jenkins
Hint
Use containerTemplate inside kubernetes agent to specify the container image and name.
3
Add a stage to run a shell command inside the container
Add a stage named Run Command with a steps block that uses container('alpine') to run sh 'echo Hello from Kubernetes agent'.
Jenkins
Hint
Inside stages, add a stage with steps that run the shell command inside the alpine container.
4
Print the output of the shell command
Add a script block inside container('alpine') to capture the output of sh(script: 'echo Hello from Kubernetes agent', returnStdout: true) into a variable output and then print it using echo output.
Jenkins
Hint
Use sh with returnStdout: true inside a script block to capture and print the command output.
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
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 A
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?
C. The node block is outside the podTemplate block
D. The node block is missing a label matching the podTemplate
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:
The node block is missing a label matching the podTemplate -> Option D
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?