Skip to main content

Managing Agent Provisioning

1. Configuring Static Agents

Property Value Description
name static-agent-01 Name of the static agent node
remoteFS /home/jenkins Remote filesystem root directory on the agent
executors 2 Number of executors to run concurrently
launchMethod ssh Launch method for connecting to the agent (e.g., ssh, jnlp)
host 192.168.1.100 IP address or hostname of the agent machine
port 22 SSH port to connect to the agent
credentialsId ssh-credentials-id Credentials ID for SSH authentication

Example: Static Agent Configuration in Jenkinsfile

pipeline {
    agent {
        label 'static-agent-01'
    }
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
            }
        }
    }
}
Note: Ensure the Jenkins master can reach the agent via SSH on the specified port.

2. Using Dynamic Agent Provisioning

Dynamic agent provisioning allows Jenkins to automatically create and manage agents as needed, rather than relying on static agents. This is particularly useful for handling varying workloads or reducing resource consumption during idle periods.

Configuration Description Default Value
Jenkins URL URL to the Jenkins master server http://localhost:8080
Agent Name Name of the agent to be created dynamically dynamic-agent-${BUILD_NUMBER}
Remote Root Directory Directory where agent will run on the node /home/jenkins
Launch Method How the agent will be launched (e.g., SSH, JNLP) JNLP

Example: Configure Dynamic Agent via Jenkins UI

1. Go to Jenkins Dashboard
2. Navigate to "Manage Jenkins" > "Configure System"
3. Under "Cloud", click "Add a new cloud"
4. Select "Kubernetes" or "Docker"
5. Configure the agent template with desired labels and resource limits
6. Save configuration
Note: Ensure that the Jenkins master has sufficient permissions to launch agents in the target environment (e.g., Docker daemon access, Kubernetes cluster permissions).
Warning: Misconfigured dynamic provisioning can lead to resource exhaustion or security vulnerabilities. Always validate configurations and set appropriate limits on agent resources.

Key Benefits of Dynamic Agent Provisioning

  • Scalability: Automatically scales agents based on job demand
  • Resource Efficiency: Reduces idle resource usage by spinning up agents only when needed
  • Flexibility: Supports various agent types (Docker, Kubernetes, etc.)

3. Implementing AWS EC2 Agents

Property Value Description
cloud.name aws-ec2 Name of the cloud provider plugin
region us-east-1 AWS region to launch instances in
ami ami-0c55b159cbfafe1d0 Amazon Machine Image ID for the agent
instanceType t3.medium EC2 instance type to use for the agent
keyPairName jenkins-keypair Name of the EC2 key pair to use for SSH access
securityGroup jenkins-agent-sg Security group to assign to the EC2 instance

Example: Jenkins Configuration as Code (JCasC)

jenkins:
  clouds:
    - ec2:
        name: "aws-ec2"
        region: "us-east-1"
        ami: "ami-0c55b159cbfafe1d0"
        instanceType: "t3.medium"
        keyPairName: "jenkins-keypair"
        securityGroup: "jenkins-agent-sg"
Note: Ensure that the Jenkins user has appropriate IAM permissions to launch EC2 instances and manage security groups.
Warning: The AMI ID must be valid for the selected region. Using an incorrect AMI can cause provisioning failures.

4. Implementing Azure VM Agents

Property Value Description
cloud.name azure-vm-agents Name of the cloud configuration for Azure VM agents
cloud.subscription.id your-subscription-id Azure subscription ID to use for VM creation
cloud.resource.group jenkins-rg Azure resource group where VMs will be created
cloud.location East US Azure region to deploy VMs in
cloud.vm.image UbuntuLTS Base image for the Azure VMs (e.g., UbuntuLTS, WindowsServer)
cloud.vm.size Standard_D2s_v3 VM size to use for agent instances

Example: Azure Cloud Configuration in Jenkins

import jenkins.model.Jenkins
import hudson.plugins.azurevmagents.AzureVMCloud

def cloud = new AzureVMCloud(
    "azure-vm-agents",
    "your-subscription-id",
    "jenkins-rg",
    "East US",
    "UbuntuLTS",
    "Standard_D2s_v3"
)

Jenkins.instance.addCloud(cloud)
Jenkins.instance.save()
Note: Ensure the Jenkins user has sufficient Azure permissions to create and manage VMs in the specified subscription and resource group.
Warning: Azure VM agents will incur costs based on the VM size and runtime. Ensure proper cleanup or scheduling to avoid unnecessary charges.

5. Implementing GCP Compute Agents

Property Value Description
gcp.project.id my-gcp-project GCP project ID where the agent will be provisioned
gcp.zone us-central1-a GCP zone for agent instance placement
gcp.machine.type n1-standard-2 Instance machine type for compute agent
gcp.image.family jenkins-agent Image family used for agent instances
gcp.service.account.email jenkins-agent@my-gcp-project.iam.gserviceaccount.com Service account email with required permissions

Example: GCP Compute Agent Configuration

agent {
    gcpCompute {
        project = 'my-gcp-project'
        zone = 'us-central1-a'
        machineType = 'n1-standard-2'
        imageFamily = 'jenkins-agent'
        serviceAccountEmail = 'jenkins-agent@my-gcp-project.iam.gserviceaccount.com'
    }
}
Note: Ensure the service account has compute.instances.create and compute.instances.delete permissions.
Warning: GCP Compute Agents are NEW in Jenkins 2.450+ and require the google-compute-engine plugin.
Feature Status Notes
Dynamic Provisioning NEW Automatically creates agents on demand
Instance Termination NEW Agents are terminated after job completion
Image Customization NEW Supports custom GCP images via image family

6. Configuring Docker Cloud Agents

Property Value Description
name docker-cloud Name of the Docker cloud configuration
serverUrl https://docker-host:2376 Docker daemon URL (e.g., with TLS)
dockerApiVersion 1.40 Docker API version to use (latest stable)
credentialsId docker-credentials Credentials ID for Docker daemon access (e.g., TLS certs or token)
containerCap 10 Maximum number of containers to run simultaneously
retentionTimeoutMinutes 5 Time (in minutes) to keep idle containers before termination

Example: Docker Cloud Configuration in Jenkins

import jenkins.model.Jenkins
import org.jenkinsci.plugins.docker.workflow.client.DockerClient

def cloud = new DockerCloud(
    name: "docker-cloud",
    serverUrl: "https://docker-host:2376",
    dockerApiVersion: "1.40",
    credentialsId: "docker-credentials",
    containerCap: 10,
    retentionTimeoutMinutes: 5
)

Jenkins.instance.clouds.add(cloud)
Jenkins.instance.save()
Note: Ensure Docker daemon is configured with TLS to securely connect from Jenkins.
Warning: Setting containerCap too high can lead to resource exhaustion on the Docker host.
Agent Template Property Value Description
label docker-agent Label to match jobs to this agent template
image jenkins/inbound-agent:latest Docker image to use for the agent container
remoteFs /home/jenkins Remote filesystem root directory for the agent
tunnel none Tunneling method (e.g., none, ssh)
instanceCap 5 Maximum number of instances to run for this template
idleMinutes 10 Idle minutes before terminating the container

Example: Agent Template Configuration

import com.nirima.jenkins.plugins.docker.DockerTemplate
import com.nirima.jenkins.plugins.docker.DockerTemplateContainer

def template = new DockerTemplate(
    image: "jenkins/inbound-agent:latest",
    label: "docker-agent",
    remoteFs: "/home/jenkins",
    tunnel: "none",
    instanceCap: 5,
    idleMinutes: 10
)

cloud.templates.add(template)

Key Configuration Points

  • Use TLS-enabled Docker daemon for secure communication.
  • Set containerCap to prevent overloading the Docker host.
  • Define remoteFs to ensure proper file system access for agents.
  • Use idleMinutes to clean up unused containers efficiently.

7. Using Kubernetes Pod Templates

Kubernetes Pod Templates in Jenkins allow you to define the structure and configuration of pods that will be used as agents in your CI/CD pipelines. These templates are essential for dynamically provisioning agents on Kubernetes clusters.

Property Description Example Value
name The name of the pod template jenkins-agent-pod
namespace The Kubernetes namespace where the pod will be created jenkins
image The Docker image to use for the agent container jenkins/inbound-agent:4.13-1
command The command to run in the agent container /usr/local/bin/jenkins-agent
args Arguments to pass to the command -url http://jenkins:8080
workingDir The working directory inside the container /home/jenkins
volumes Volume mounts to be attached to the pod [{name: "workspace", persistentVolumeClaim: {claimName: "jenkins-home"}}]
envVars Environment variables to set in the pod [{name: "JAVA_OPTS", value: "-Xmx1g"}]
nodeSelector Node selector labels to restrict where the pod can be scheduled {"kubernetes.io/os": "linux"}
tolerations Tolerations to allow scheduling on nodes with taints [{key: "dedicated", operator: "Equal", value: "jenkins", effect: "NoSchedule"}]

Example: Define a Pod Template in Jenkins

// Groovy script to define a Kubernetes pod template
podTemplate(name: 'jenkins-agent-pod', namespace: 'jenkins', image: 'jenkins/inbound-agent:4.13-1') {
    node('jenkins-agent-pod') {
        stage('Build') {
            sh 'echo "Building..."'
        }
    }
}
Note: Ensure that the Jenkins Kubernetes plugin is installed and configured to use the pod templates effectively.
Warning: Make sure that the image specified in the pod template is accessible from your Kubernetes cluster. If it's a private image, ensure that the necessary secrets are configured.

8. Setting Agent Idle Termination

Property Default Value Description
jenkins.agent.idleTerminationMinutes 5 Number of minutes an agent can be idle before it is terminated.

Example: Configure Idle Termination in Jenkinsfile

pipeline {
    agent {
        label 'my-agent'
        idleTerminationMinutes 10
    }
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
            }
        }
    }
}
Note: Setting this value too low may cause agents to terminate prematurely during long-running builds.

9. Managing Agent Resource Allocation

Resource Type Configuration Property Description Default Value
Memory jenkins.agent.memory Maximum memory allocated to the agent process 1GB
CPU Cores jenkins.agent.cpu Number of CPU cores assigned to the agent 1
Disk Space jenkins.agent.disk Amount of disk space reserved for agent operations 5GB

Example: Configure Agent Resource Limits

import jenkins.model.Jenkins
import hudson.slaves.SlaveComputer

def jenkins = Jenkins.getInstance()
def agent = jenkins.getComputer('agent-name')

// Set resource limits
agent.setMemoryLimit(2 * 1024 * 1024 * 1024) // 2GB
agent.setCpuLimit(2) // 2 cores
agent.setDiskSpaceLimit(10 * 1024 * 1024 * 1024) // 10GB

println "Resource limits updated for agent: ${agent.getName()}"
Note: Resource allocation settings are applied at the agent level and can be overridden per job using agent directives in pipeline scripts.
Warning: Setting resource limits too low may cause build failures or performance degradation. Ensure sufficient resources are allocated to avoid system instability.

10. Using Custom Agent Labels

Label Description Usage
custom.agent.label Custom label to identify a specific agent or group of agents agent { label 'custom.agent.label' }
agent.type Specifies the type of agent to use (e.g., docker, kubernetes) agent { label 'custom.agent.label' type 'docker' }

Example: Using Custom Agent Labels in Pipeline

pipeline {
    agent {
        label 'custom.agent.label'
    }
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
            }
        }
    }
}
Note: Custom labels must be defined in Jenkins configuration or agent provisioning system to be recognized.

Was this article helpful?