Jenkins Distributed Builds and Agents: Scale CI/CD Without Losing Your Sanity
Master Jenkins distributed builds: set up agents, avoid master overload, and handle production incidents with real debug commands and fixes..
20+ years shipping production infrastructure and CI/CD at scale. Lessons pulled from things that broke in production.
- ✓Production DevOps experience
- ✓Deep understanding of the tool's internals
- ✓Experience debugging distributed systems
- Jenkins distributed builds offload jobs from master to agent nodes, preventing master overload and enabling horizontal scaling.
- Agents can be permanent (e.g., EC2 instances) or ephemeral (e.g., Kubernetes pods), each with unique configuration needs.
- Master-agent communication uses JNLP or SSH; JNLP is simpler for cloud agents, SSH is more secure for on-prem.
- Common production issues: agent disconnects, credential mismatches, and workspace conflicts on shared agents.
- Key debugging commands:
journalctl -u jenkinson master,java -jar agent.jar -jnlpUrl ...for agent logs. - Use labels to route jobs to specific agents (e.g.,
label == 'linux' && label == 'gpu'). - Cloud-native agents (Kubernetes, Docker) scale faster but require careful resource limits and cleanup.
- Always set executors count per agent to match CPU cores; oversubscription causes thrashing.
Imagine a restaurant kitchen. The master is the head chef who plans the menu and assigns tasks. The agents are the line cooks who actually prepare the dishes. If the head chef tries to cook everything himself, the kitchen slows down and orders pile up. By hiring more line cooks (agents), the head chef can focus on coordination and complex tasks, while the cooks handle the routine work in parallel. In the same way, Jenkins agents execute builds, tests, and deployments, freeing the master to orchestrate the pipeline.
| Chrome | Firefox | Safari | Edge |
|---|---|---|---|
| ✓ | ✓ | ✓ | ✓ |
I remember the day our Jenkins master crashed during a critical release. The build queue was 200 deep, and every developer was panicking. We had a single master running everything—compilation, tests, packaging, deployment. It was a ticking time bomb. That day, we learned the hard way that scaling CI/CD requires distributing the load. Since then, I've set up dozens of Jenkins clusters, from small teams to enterprise pipelines. This article shares the real-world knowledge I wish I had back then.
1. Architecture Overview: Master and Agents
Jenkins master is the brain: it stores job configurations, manages the build queue, and serves the web UI. Agents are the muscle: they run the actual build steps. The master delegates job execution to agents based on labels and availability. Communication happens over TCP port 50000 (JNLP) or SSH (port 22). In production, never run build steps on the master itself—it's a single point of failure. Use agents for everything. The master should only orchestrate. For high availability, consider a multi-master setup with shared storage (NFS, S3) but that's advanced. Start with one master and multiple agents.
Production insight: Always use a dedicated master with minimum 4 CPU cores and 8GB RAM. Monitor /tmp disk usage—agent logs fill it fast. Set up logrotate.
Key takeaway: Master orchestrates, agents execute. Never build on master.
2. Setting Up Permanent Agents via SSH
SSH agents are ideal for on-premises machines with static IPs. Steps: 1) Install Java on agent. 2) Create a Jenkins user on agent. 3) Generate SSH key on master (no passphrase). 4) Copy public key to agent's authorized_keys. 5) In Jenkins UI: Manage Jenkins > Manage Nodes > New Node. Choose 'Permanent Agent'. Set remote root directory (e.g., /home/jenkins/workspace). Set labels (e.g., linux, docker). Under Launch method, select 'Launch agent via SSH'. Provide host, credentials (the SSH key), and Java path. Test connection. Ensure agent can reach master on port 50000 if using JNLP fallback.
Production insight: Use a dedicated service account for SSH. Rotate keys regularly. Set executors equal to CPU cores. Oversubscription causes thrashing.
Key takeaway: SSH agents are reliable for static environments; use dedicated service accounts and key rotation.
3. Setting Up JNLP Agents (Java Web Start)
JNLP agents are simpler for cloud environments where agents come and go. Steps: 1) In Jenkins UI, create a new node as 'Permanent Agent' but select 'Launch agent via Java Web Start'. 2) Download agent.jar from master: wget https://master:8080/jnlpJars/agent.jar. 3) On agent, run: java -jar agent.jar -jnlpUrl https://master:8080/computer/agent-name/slave-agent.jnlp -secret <secret>. 4) For production, wrap in a systemd service. Example unit file: [Service] ExecStart=/usr/bin/java -jar /opt/jenkins/agent.jar -jnlpUrl ... -secret ... Restart=always. User=jenkins. 5) Use -noReconnect to prevent retry storms.
Production insight: JNLP agents are ephemeral; use -noReconnect and let orchestration tool (Kubernetes, Docker) restart them. Monitor agent.log for connection errors.
Key takeaway: JNLP is great for cloud; wrap in systemd and use -noReconnect.