Introduction:
In the world of CI/CD, Jenkins stands out as a versatile tool for automating your software development workflows. While Jenkins' master node is powerful on its own, scaling your builds and deployments often requires setting up Jenkins agents. Agents allow you to offload tasks, manage diverse environments, and optimize performance by distributing workloads. In this guide, I will walk you through the process of setting up a Jenkins agent, covering everything from understanding the agent's role to configuring it on your Jenkins master.
Prerequisites:
Jenkins Installed: Jenkins and Java installed on one EC2 instance (Master Node)
EC2 Instance : Additional EC2 instances created to act as Jenkins agents.
๐ Tip: To understand how to setup Jenkins on AWS EC2 instance read my previous blog . [Here is the link to the blog]
Steps To Setup Jenkins Agent:
Step 1: Create Two AWS EC2 Instances;
Launch two AWS EC2 instances using a suitable AMI (e.g., Amazon Linux 2 or Ubuntu).
Ensure instances are running and note their IP addresses for agent setup.
As you can see in the image I have created two AWS EC2 instances named Linux_Server1 and jenkins_agent. By following the steps given on above blog link setup Jenkins on Linux_Server1.
Step 2: Establish SSH Connectivity Between Linux_Server1 and Jenkins Agent
- Connect to Linux_Server1 (Master) and Jenkins Agent Server via SSH.
On Linux_Server1, generate an SSH key pair using
ssh-keygen
.
Copy the public key to the
authorized_keys
file on the Jenkins Agent server.Test the connection by running:
ssh -i id_ed25519 username@<Jenkins_Agent_Public_DNS>
As you can see we have successfully connected Linux_Server1 to jenkins_agent server via ssh .
Step 3: Install Java on Jenkins Agent Server
In order to run Jenkins on our agent server the minimum requirement is Java should be installed. To install Java on jenkins_agent server run the following command .
sudo apt update sudo apt install fontconfig openjdk-17-jre
Configure the following settings:
Remote root directory: Path on the agent server where Jenkins will run.
Labels: Optional labels for the agent.
When configuring a Jenkins job, you can specify which label the job should run on. Jenkins will then schedule the job on an agent that matches the label, ensuring it runs in the appropriate environment. Labels help distribute tasks efficiently across multiple agents.
Launch method: Choose Launch agent by SSH.
Host: Enter the public IP or DNS of the Jenkins Agent server.
Credentials: Add the SSH credentials (private key) to connect to the agent.
Click Save to add the agent.
- Jenkins will attempt to connect to the agent using SSH. If successful, the agent will be listed as online
Conclusion
Setting up Jenkins agents on separate servers is a crucial step in scaling your CI/CD pipeline. By configuring agents, you can distribute build and deployment tasks across multiple machines, optimizing performance and reducing the load on the Jenkins master node. With this setup, you can build a more robust, scalable Jenkins infrastructure for your development workflows.