
Table of Contents
By Khimananda Oli | Last reviewed: August 2026
Running every pipeline on a single controller creates bottlenecks, security risks, and single points of failure that stall delivery. Jenkins distributed builds with agents solve this by offloading execution to dedicated worker nodes while the controller handles only scheduling and orchestration. This guide covers the practical configuration patterns I use to scale teams safely, from static SSH agents to ephemeral cloud-native executors.
Before configuring remote workers, ensure your foundation is solid. A stable controller is non-negotiable when managing multiple nodes. If you are starting fresh or hardening an existing instance, review my tutorial on how to build a CI/CD pipeline with Jenkins to establish proper credential management and baseline security. Skipping these fundamentals often leads to permission errors and unstable agent connections later.
How do you configure Jenkins distributed builds with agents using SSH?
SSH agents remain the most reliable choice for fixed infrastructure, legacy systems, or environments where containerization is restricted. In my work with Nepali government projects and air-gapped financial systems, SSH is often the only viable protocol due to compliance mandates prohibiting container runtimes.
Prerequisites and Security Hardening
Never use password authentication. Generate a dedicated ED25519 key pair for Jenkins and deploy the public key to the agent's ~/.ssh/authorized_keys. Restrict the Jenkins user on the agent to only the commands it needs via sudoers or capability-limited accounts. For detailed OS-level hardening before connecting, see my guide on securing a fresh Ubuntu VPS.
# On the Jenkins controller (as jenkins user)
ssh-keygen -t ed25519 -C "jenkins-agent-prod" -f ~/.ssh/jenkins_agent_ed25519 -N ""
# Copy to agent (manual verification required first time)
ssh-copy-id -i ~/.ssh/jenkins_agent_ed25519.pub [email protected]
# Verify connection without interactive prompt
ssh -o BatchMode=yes -o ConnectTimeout=5 [email protected] echo "Connection OK" Registering the Node in Jenkins
- Navigate to Manage Jenkins → Nodes → New Node.
- Select Permanent Agent and name it descriptively (e.g.,
linux-build-static-01). - Set Remote root directory to an absolute path like
/var/lib/jenkins/workspace. Never use relative paths. - Under Launch method, choose "Launch agents via SSH".
- Enter the hostname/IP, credentials (select the SSH private key stored in Jenkins Credentials), and set Host Key Verification Strategy to "Known hosts file" after manually verifying the fingerprint once.
- Add labels like
linux,maven, orsecurity-scannedto enable label-based pipeline targeting.
A common mistake is leaving "Non-verifying Verification Strategy" selected permanently. This opens man-in-the-middle attack vectors. Always switch to known-hosts verification after initial trust establishment.
When should you use Docker agents versus Kubernetes agents?
Choosing between Docker and Kubernetes agents depends on your infrastructure maturity, team size, and elasticity requirements. Both provide ephemeral environments, but their operational profiles differ significantly.
| Criteria | Docker Agent (docker-plugin) | Kubernetes Agent (kubernetes-plugin) |
|---|---|---|
| Infrastructure Requirement | Single Docker host or Swarm | Functional K8s cluster (EKS/GKE/AKS/self-managed) |
| Scaling Speed | Seconds (container start) | 10–60 seconds (pod scheduling + image pull) |
| Resource Isolation | cgroups/namespaces on shared kernel | Pod-level isolation, network policies, RBAC |
| Multi-container Builds | Limited (sidecar support basic) | Native sidecars, init containers, service meshes |
| Cost Model | Fixed host cost; idle containers waste resources | Pay-per-pod; scales to zero when idle |
| Best For | Small teams, simple pipelines, quick migration | Large teams, microservices, variable load, multi-cloud |
In practice, I recommend Docker agents for teams with fewer than 20 concurrent builds and stable workloads. Move to Kubernetes when you need auto-scaling, multi-stage testing with service dependencies, or when operating across regions. If you're evaluating cloud platforms for hosting these agents, compare options in my AWS vs Azure vs GCP analysis.
How do you define dynamic Kubernetes agents in Jenkinsfile?
The Kubernetes plugin allows you to declare agent specifications directly in your pipeline code. This treats build infrastructure as code, versioned alongside your application. Below is a production-grade template I use for Java microservices.
pipeline {
agent none
stages {
stage('Build & Test') {
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: maven
image: maven:3.9-eclipse-temurin-21-alpine
command: ['sleep', '3600']
resources:
requests:
memory: "2Gi"
cpu: "1000m"
limits:
memory: "4Gi"
cpu: "2000m"
securityContext:
runAsNonRoot: true
readOnlyRootFilesystem: true
- name: docker
image: docker:27-dind
securityContext:
privileged: true
'''
}
}
steps {
container('maven') {
sh 'mvn clean verify -B'
}
}
}
}
} Critical notes from production experience:
- Always set resource requests AND limits. Without limits, a runaway build can starve other pods or crash nodes. I've seen entire EKS clusters destabilized by missing memory limits.
- Use
runAsNonRoot: trueunless absolutely necessary. Many official images now support non-root execution. - Avoid
privileged: truefor Docker-in-Docker when possible. Consider Kaniko or Buildah for rootless container builds inside Kubernetes. - Pin image tags to digests in production pipelines. Mutable tags like
latestbreak reproducibility and audit trails.
What security controls are mandatory for distributed Jenkins agents?
Distributed builds expand your attack surface. Each agent is a potential entry point. Apply these controls systematically:
- Network Segmentation: Agents should not have unrestricted outbound internet access. Use egress firewalls or VPC endpoints to allow only required repositories, artifact stores, and API endpoints. In AWS, this means restrictive Security Groups and NAT Gateway rules.
- Credential Isolation: Never store secrets in agent filesystems. Use Jenkins Credentials Binding plugin to inject secrets at runtime as environment variables or files, scoped to specific folders or jobs.
- Immutable Infrastructure: Rebuild agent images regularly. Patch base OS packages weekly. Automate this with tools like Packer or Docker multi-stage builds. See my multi-stage Docker build guide for lean, secure agent images.
- Audit Logging: Enable Jenkins Audit Trail plugin. Forward agent syslog/auth.log to centralized logging. Every command executed on an agent must be traceable back to a specific job and user.
- Least Privilege IAM: If agents interact with cloud APIs, assign minimal IAM roles. An agent building frontend assets should never have S3 write access to production buckets.
For teams handling sensitive data or operating under SOC 2 / ISO 27001, document these controls explicitly. Auditors will ask for evidence of agent isolation and credential handling. Automated evidence collection saves hours during assessment periods.
How do you troubleshoot agent connectivity and performance issues?
Even well-configured agents fail. Systematic debugging prevents hours of guesswork.
Connectivity Failures
If an SSH agent shows "Offline" or "Connection refused":
- Test SSH manually from the controller host as the jenkins user:
ssh -vvv jenkins@agent-host. Verbose output reveals auth failures, host key mismatches, or timeout issues. - Check agent-side logs:
journalctl -u sshd -for/var/log/auth.log. Look for "Failed password", "Invalid user", or PAM denials. - Verify firewall rules. Port 22 must be open bidirectionally for inbound SSH launch mode. For JNLP agents, ensure TCP port for inbound agents (default 50000) is accessible.
- Confirm Java version compatibility. Agent JVM major version must match or exceed controller JVM. Mismatched versions cause silent disconnects.
Performance Degradation
When builds slow down unexpectedly:
- Check disk I/O: Run
iostat -xz 1on the agent during a build. High %util indicates storage bottleneck. Switch to NVMe or tmpfs for workspace. - Monitor memory pressure:
free -handdmesg | grep -i oom. OOM kills manifest as mysterious build failures without clear error messages. - Validate network throughput: Large artifact transfers saturate bandwidth. Use
iperf3between controller and agent. Consider local artifact caching proxies (Nexus/Artifactory) on the agent subnet. - Review garbage collection: Add
-XX:+PrintGCDetails -Xloggc:/tmp/gc.logto agent JVM options. Long GC pauses indicate heap misconfiguration.
Document recurring issues in your team's runbook. Pattern recognition accelerates future resolution.
Scaling Jenkins Distributed Builds with Agents for Production
Implementing Jenkins distributed builds with agents transforms your CI/CD from a fragile monolith into a resilient, scalable platform. Start with SSH agents for immediate wins, graduate to Docker for isolation, and adopt Kubernetes when elasticity becomes critical. Prioritize security controls from day one — retrofitting is costly and error-prone.
Your next step: audit your current pipeline queue times and failure rates. Identify the top three bottlenecks. Map each to an agent type discussed here. Prototype one change this sprint. Measure the impact. Iterate.
If your team needs hands-on guidance designing agent architectures, hardening configurations for compliance, or migrating legacy setups to cloud-native executors, reach out through my contact page. I help engineering teams build CI/CD systems that scale securely and pass audits confidently.