3. Jenkins Agents (Multi-Node) – Setting Up a Slave Node on AWS EC2

Introduction

Jenkins operates in a master-agent architecture, where agents (slave nodes) execute build jobs while the controller (master node) manages them. In this guide, we will set up a Jenkins slave node on an AWS EC2 instance and connect it to the Jenkins master.


1. Launching a Jenkins Slave Node on AWS EC2

First, create an additional EC2 instance to act as a Jenkins agent (slave node).

Instance Details:

  • Instance Type: t2.micro

  • Storage: 8 GB

  • OS: Ubuntu


2. Installing Dependencies on the Slave Node

Once the instance is up, connect to it via SSH:

ssh -i your-key.pem ubuntu@slave-instance-ip

Install Java and Docker

sudo apt update && sudo apt install openjdk-17-jdk docker.io -y

Add Jenkins User to Docker Group

sudo usermod -aG docker $USER

Install Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose


3. Connecting the Slave Node to Jenkins Master

Step 1: Add the Slave Node in Jenkins

  1. Open Jenkins DashboardManage JenkinsManage Nodes and CloudsNew Node.

  2. Name the node as jenkins-slave and select Permanent Agent.

  3. Set the Remote Root Directory (e.g., /home/ubuntu).

  4. Under Launch Method, select "Launch agents via SSH".

  5. Enter the Public IP Address of the slave instance.


Step 2: Set Up SSH Authentication

Now, we need to set up passwordless SSH authentication between the master and slave nodes.

Generate SSH Keys on the Jenkins Master

On the Jenkins Master, run the following command to create SSH keys:

ssh-keygen -t rsa -b 2048

Press Enter for default locations and leave the passphrase empty.

Select credential that we created and setup slave node

Copy the Public Key to the Slave Node

Copy the public key (~/.ssh/id_rsa.pub) from the master to the slave:

Alternatively, manually add the key to the ~/.ssh/authorized_keys file on the slave:

cat ~/.ssh/id_rsa.pub

go to slave machine and paste it

Check Slave connection


Conclusion

Setting up a Jenkins slave node on AWS EC2 helps scale CI/CD workloads efficiently. Using SSH authentication, we securely connect the slave node, allowing Jenkins to distribute jobs seamlessly.

Let me know if you need any modifications! 😊