Skip to content

Kubernetes pod templates for Jenkins agents

In a Kubernetes-based Jenkins deployment, agents are spawned as Pods. The configuration defining these agents is handled by Pod Templates, which specify the container images, resources, and labels required for Jenkins builds.^[400-devops-06-kubernetes-devops-helm-helm-jenkins-readme.md]

Configuration methods

The Jenkins Helm chart supports multiple ways to define Pod templates. You can add custom templates directly under agent.podTemplates, or use additionalAgents to create new agents based on the default agent's configuration.^[400-devops-06-kubernetes-devops-helm-helm-jenkins-readme.md]

Using agent.podTemplates

This method allows you to define detailed Pod Templates via the values.yaml file.^[400-devops-06-kubernetes-devops-helm-helm-jenkins-readme.md]

  • Key Definition: Each key under agent.podTemplates acts as a label for the template and must conform to RFC 1123 DNS label standards (lowercase letters, numbers, and hyphens).^[400-devops-06-kubernetes-devops-helm-helm-jenkins-readme.md]
  • JNLP Container: The Kubernetes plugin automatically injects the JNLP (Java Network Launch Protocol) container into the Pod, so it does not need to be explicitly defined in the template.^[400-devops-06-kubernetes-devops-helm-helm-jenkins-readme.md]
  • Prerequisites: For these templates to be loaded, controller.JCasC.defaultConfig must be set to true.^[400-devops-06-kubernetes-devops-helm-helm-jenkins-readme.md]

The following example creates a Python Pod template:^[400-devops-06-kubernetes-devops-helm-helm-jenkins-readme.md]

agent:
  podTemplates:
    python: |
      - name: python
        label: jenkins-python
        serviceAccount: jenkins
        containers:
          - name: python
            image: python:3
            command: "/bin/sh -c"
            args: "cat"
            ttyEnabled: true
            privileged: true
            resourceRequestCpu: "400m"
            resourceRequestMemory: "512Mi"
            resourceLimitCpu: "1"
            resourceLimitMemory: "1024Mi"

Using additionalAgents

The additionalAgents configuration is useful for creating new agent definitions that inherit values from the default agent configuration, while allowing specific overrides.^[400-devops-06-kubernetes-devops-helm-helm-jenkins-readme.md]

  • Inheritance: These agents inherit all default values (such as resources) from the main agent block, so you only need to specify values that differ.^[400-devops-06-kubernetes-devops-helm-helm-jenkins-readme.md]
  • Container Overrides: You can override the default JNLP container settings using the sideContainerName property if needed.^[400-devops-06-kubernetes-devops-helm-helm-jenkins-readme.md]

Example configuration for Maven and Python agents:^[400-devops-06-kubernetes-devops-helm-helm-jenkins-readme.md]

agent:
  podName: default
  customJenkinsLabels: default
  # set resources for additional agents to inherit
  resources:
    limits:
      cpu: "1"
      memory: "2048Mi"

additionalAgents:
  [Maven](<./maven.md>):
    podName: [Maven](<./maven.md>)
    customJenkinsLabels: [Maven](<./maven.md>)
    # An example of overriding the jnlp container
    # sideContainerName: jnlp
    image: jenkins/jnlp-agent-maven
    tag: latest
  python:
    podName: python
    customJenkinsLabels: python
    sideContainerName: python
    image: python
    tag: "3"
    command: "/bin/sh -c"
    args: "cat"
    TTYEnabled: true

Configuration as Code

Pod templates are managed via Jenkins Configuration as Code (JCasC). The official documentation for the specific syntax available in these YAML blocks can be found at the configuration-as-code reference path for the Kubernetes cloud.^[400-devops-06-kubernetes-devops-helm-helm-jenkins-readme.md]

Sources

  • 400-devops-06-kubernetes-devops-helm-helm-jenkins-readme.md