Skip to main content
In this lesson you will configure a Kubernetes cloud in Jenkins so Jenkins can dynamically spin up agent pods. This guide covers plugin installation, credential setup (least-privilege), RBAC, and common troubleshooting tips. Open the Jenkins UI and navigate to Manage Jenkins to begin configuring a cloud.
A dark-themed Jenkins web dashboard showing a table of CI jobs with status icons, last success/failure times, and durations. The left sidebar contains navigation links and the top bar shows a search field and user account controls.

1. Install the Kubernetes plugin

Go to Manage Jenkins -> Manage Plugins and install the plugin that provides Kubernetes cloud support.
A screenshot of the Jenkins "Manage Plugins" -> "Available plugins" page showing a search for "Cloud Providers" and a list of plugins (Docker, Kubernetes, Amazon EC2, vSphere, Azure VM Agents) with descriptions and release timestamps. The left sidebar shows navigation items like Updates, Available plugins, Installed plugins, and Advanced settings.
I selected the Kubernetes plugin. If you need a specific version, download the .hpi or use the jenkins-plugin-cli tool. Example local files from my machine:
Install a specific version using jenkins-plugin-cli:
Or download directly from the update site:
If installation fails, check the plugin install logs for dependency errors (for example, an out-of-date Credentials Plugin). Update any required plugins and restart Jenkins. After restart, confirm the Kubernetes plugin appears under Manage Plugins -> Installed. Refer to the plugin documentation for full details and examples:
A screenshot of the Jenkins plugins website showing the "Kubernetes" plugin page with its documentation, description, and a Table of Contents. The right column shows version info, release date, install percentage and related links.

2. Common pipeline primitives (quick reference)

These keywords appear frequently when using Kubernetes agents in Jenkins pipelines. When adding a Kubernetes cloud you can either upload an entire kubeconfig (not recommended if it contains cluster-admin credentials) or provide the Kubernetes API server URL and a restricted credential (recommended). The secure approach:
  1. Create a namespace for Jenkins (e.g., jenkins).
  2. Create a service account in that namespace.
  3. Generate a long-duration token for the service account.
  4. Add the token to Jenkins as a Secret Text credential and use it in the cloud configuration.
Show Kubernetes cluster info and kubeconfig examples:
Get the raw kubeconfig:
Example (trimmed) kubeconfig:
Do NOT upload a kubeconfig containing cluster-admin credentials to Jenkins unless you fully understand the security implications.
Use least-privilege credentials: create a dedicated service account in a single namespace for Jenkins instead of using an admin kubeconfig.

Create a namespace, service account, token, and bind privileges

Commands to set up a restricted service account for Jenkins:
If your cluster version doesn’t create legacy secrets, use kubectl create token as shown above. Add the token to Jenkins as a credential:
  • Kind: Secret text
  • Secret: paste the token value
  • ID: e.g. k8s-jenkins-agent-token
  • Description: optional

4. Configure the Kubernetes cloud in Jenkins

After the plugin is installed, go to Manage Jenkins -> Configure System -> Clouds (or Manage Jenkins -> Clouds depending on Jenkins version). Add a new cloud and select “Kubernetes”. In the cloud configuration provide: If Jenkins cannot validate the cluster certificate, either upload the CA certificate in the cloud options or temporarily disable TLS verification (not recommended for production). If you set everything correctly, click “Test Connection” to validate Jenkins can talk to the Kubernetes API.
Disabling TLS verification is insecure. Only use it for short-term debugging in a trusted environment. For production, upload the cluster CA certificate or ensure certificates are valid.

5. RBAC: Grant the service account the needed permissions

Without proper RBAC, the token will fail with 403 Forbidden responses. For example:
To enable Jenkins to create and manage pods in the jenkins namespace, bind an appropriate role. A simple binding to the admin ClusterRole scoped to the namespace:
If you truly need only specific permissions, craft a Role with minimal verbs (e.g., get, list, watch, create, delete) for resources such as pods, pods/exec, services, configmaps, and bind it to the service account. Note: If you scope the service account to only the jenkins namespace, testing another namespace (e.g., jenkins-123) will return 403 — this is expected behavior for least-privilege credentials.

6. Connectivity options and agent lifecycle

  • By default, agent pods connect back to Jenkins over the JNLP (TCP) port. If your Jenkins instance disables the TCP agent port, configure WebSocket or Direct Connection.
  • WebSocket agents use HTTP(S), which is useful where TCP is blocked.
  • You can set a custom Jenkins URL in global settings if Jenkins is reachable behind a different endpoint.
  • Pod labels allow easy filtering and organization for created agent pods.
  • Pod retention:
    • Never (default): delete pods after build completes.
    • On Failure: keep pods if the build fails for debugging.
    • Always: retain pods regardless of outcome.
After saving the cloud configuration, Jenkins will be able to spin up agent pods in the configured namespace when a job requests a matching agent.
A screenshot of the Jenkins "Clouds" settings page showing one cloud entry named "dasher-prod-k8s-us-east" and a "New cloud" button in the top-right. The page uses a dark theme and shows navigation/header elements for Jenkins.

7. Test by running jobs

Create or run a pipeline that requests a Kubernetes agent (via label or podTemplate) and confirm Jenkins creates a pod in the jenkins namespace. Verify pod creation using:
Watch logs for failures and adjust RBAC, credentials, or TLS settings as needed. You have now configured Jenkins to connect to Kubernetes and create ephemeral agent pods. Run pipeline jobs that use this cloud to observe pod lifecycle and logs.

Watch Video