In this guide, you’ll learn how to run Jenkins build agents inside Kubernetes Pods using a Declarative Pipeline. We assume you have already installed and configured the Kubernetes plugin for Jenkins.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
- A running Jenkins instance with the Kubernetes plugin installed
- Access to a Kubernetes cluster and proper credentials configured under Manage Jenkins → Configure System → Cloud → Kubernetes
Make sure your Jenkins service account has permissions to create and delete Pods in the target namespace.
1. Create a New Pipeline Job
- Open the Jenkins Dashboard and click New Item.
- Enter a name, for example
k8s-cloud-agent-demo, and select Pipeline. - Scroll to the Pipeline section, choose Pipeline script, then pick the Declarative Kubernetes sample.

2. Define a Basic Kubernetes Agent
The default Declarative example includes anagent block with containerTemplate. To get started quickly, simplify it to print the Pod’s hostname:
containerTemplate and let Jenkins use defaults:
podRetention: Never).

With the default retention policy (
Never), Pods are removed immediately after each build. Disable or adjust this if you need to debug post-build.3. Inline Pod Template with YAML
For more control, define your Pod in YAML directly within the pipeline:
4. Run the Pipeline
- Save the Pipeline script.
- Click Build Now.

5. Default Cloud & Advanced Configuration
If you don’t specify acloud in the agent block, Jenkins selects the first available Kubernetes cloud. You can view options in the Declarative Directive Generator:

Select a Specific Cloud and Namespace

6. Example: Multiple Containers
Define multiple containers in the same Pod to run different tools per stage:container('node-container'), the Node.js commands default to ubuntu-container and will fail.

7. Inspecting Pods and Events
During a build, you can inspect Pods and events withkubectl:
Conclusion
Leveraging Kubernetes Pods as Jenkins agents in Declarative Pipelines offers:- Custom environments per stage (e.g., Ubuntu, Node.js)
- Ephemeral build agents that spin up and down automatically
- Inline YAML definitions or external templates for reusable configurations