Jenkins Master-Slave Agent Installation and Configuration on AWS EC2

Jenkins Master-Slave Agent Installation and Configuration on AWS EC2


Author: Multi Cloud Architect (+91-9036367291)


Jenkins Overview

What is Jenkins?

Jenkins is an open-source automation server widely used for Continuous Integration (CI) and Continuous Delivery (CD). It helps automate software development tasks such as building applications, testing code, and deploying applications to various environments.

Jenkins Architecture

Jenkins follows a Controller-Agent (Master-Slave) architecture.

Jenkins Controller (Master):

  • Manages Jenkins configuration and jobs.
  • Schedules build tasks.
  • Monitors connected agents.
  • Provides the Jenkins web interface.

Jenkins Agent (Slave):

  • Executes build and deployment jobs assigned by the controller.
  • Can be installed on Linux, Windows, or Docker-based environments.
  • Helps distribute workloads and improve scalability.

 

Prerequisites

Launch an EC2 instance with the following configuration:

  • Instance Name: Jenkins-Master
  • Operating System: Amazon Linux
  • EBS Volume Size: 30 GB
  • Security Group Name: Jenkins-NSG

Configure the following inbound rules:

Port

Protocol

Purpose

22

TCP

SSH Access

443

TCP

HTTPS Access

8080

TCP

Jenkins Web Access

40933

TCP

Jenkins Agent Communication

 

 

 

Step 1: Install Jenkins on the Master Server

Connect to the Jenkins-Master EC2 instance using SSH.

Import the Jenkins repository key:

sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

Install Jenkins:

sudo dnf install jenkins -y

 

 

Understanding systemctl

What is systemctl?

systemctl is a Linux command used to manage system services.

Common commands include:

Check service status:

systemctl status jenkins

Enable Jenkins service during system startup:

systemctl enable jenkins

Start Jenkins service:

systemctl start jenkins

Verify Jenkins service status:

systemctl status jenkins

 

 

Step 2: Access the Jenkins Web Interface

Copy the public IP address of the Jenkins-Master EC2 instance.

Open a browser and access:

http://<Public-IP>:8080

Example:

http://30.24.23.23:8080

If the page is not accessible:

  • Open the AWS Security Group (Jenkins-NSG).
  • Add an inbound rule for port 8080.
  • Save the changes and refresh the browser.

 

 

Step 3: Unlock Jenkins

Jenkins will display an Administrator Password page.

Connect to the EC2 instance using SSH and run:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the password and paste it into the Jenkins web page.

Click Continue.

 

 

Step 4: Create the First Jenkins Administrator

  • Create the first administrator account.
  • Enter username, password, and email address.
  • Click Save and Continue.
  • Verify the Jenkins URL.
  • Click Save and Finish.
  • Click Start Using Jenkins.

The Jenkins dashboard will open successfully.

 

 

Step 5: Configure Jenkins Agent Port

From the Jenkins dashboard:

  • Click Manage Jenkins.
  • Click Security.
  • Scroll down to the Agents section.
  • Select Fixed.
  • Enter port number 40933.
  • Click Save.

Go to the AWS Security Group and add port 40933 as an inbound rule.

 

 

Step 6: Launch the Jenkins Agent Server

Launch a second EC2 instance with the following configuration:

  • Instance Name: Slave-1
  • Operating System: Amazon Linux
  • EBS Volume Size: 30 GB
  • Security Group: Jenkins-NSG

Connect to the instance using SSH.

Switch to the root user:

sudo su

Update the operating system:

dnf update -y

 

 

Why is Java Required for Jenkins?

Jenkins is a Java-based application.

Java is required because:

  • Jenkins itself runs on Java.
  • Jenkins agents communicate using Java processes.
  • Build jobs often require Java runtime support.

Without Java, Jenkins services and agents cannot run.

 

 

Step 7: Install Java on the Agent

Install Java:

sudo dnf install java-17-amazon-corretto -y

Verify installation:

java --version

 

 

Step 8: Install Docker

Install Docker:

sudo dnf install docker -y

Verify installation:

docker --version

Enable Docker service:

systemctl enable docker

Start Docker service:

systemctl start docker

Verify Docker service:

systemctl status docker

 

 

Step 9: Install Git

Install Git:

sudo dnf install git -y

Verify installation:

git --version

 

 

Step 10: Create a Jenkins Agent

From the Jenkins dashboard:

  • Click Manage Jenkins.
  • Click Nodes.
  • Click New Node.

Configure the following:

Node Name:

slave-1

Node Type:

Permanent Agent

Configure:

  • Number of Executors: 1
  • Remote Root Directory: /home/jenkins
  • Usage: Use this node as much as possible
  • Launch Method: Launch agent by connecting it to the controller
  • Availability: Keep this agent online as much as possible

Click Save.

Initially, the node will appear offline.

 

 

Step 11: Connect the Jenkins Agent

On the Slave-1 server:

Create the Jenkins directory:

mkdir /home/jenkins

Navigate to the directory:

cd /home/jenkins

Verify the directory contents:

ls -lrt

Download the Jenkins agent:

curl -sO http://<Jenkins-IP>:8080/jnlpJars/agent.jar

Verify the download:

ls -lrt

Copy the agent connection command from the Jenkins node page and execute it.

Example:

java -jar agent.jar \

-url http://<Jenkins-IP>:8080/ \

-secret <secret-key> \

-name "Slave-1" \

-workDir "/home/jenkins"

After successful execution, the agent status will change to Connected.

 

 

Step 12: Verify Node Connectivity

Navigate to:

Manage Jenkins → Nodes

The Slave-1 node should now display a healthy and connected status.

The Jenkins Controller-Agent architecture is now fully configured.

 

 

 

Step 13: Disable Builds on the Controller

From the Jenkins dashboard:

  • Click Built-In Node.
  • Change the Number of Executors to 0.
  • Click Save.

This ensures all jobs execute only on the agent node.

 

 

Step 14: Create a Test Job

From the Jenkins dashboard:

  • Click New Item.
  • Enter Job Name:

Shell_Job

  • Select Freestyle Project.
  • Click OK.

Scroll to the Build Steps section.

Select:

Execute Shell

Add the following command:

echo "The job is ready to execute."

Click Save.

 

 

Step 15: Execute the Job

Open the Shell_Job project.

Click:

Build Now

After the build completes:

  • Click the build number.
  • Click Console Output.

You should see:

The job is ready to execute.

This confirms that the job has been executed successfully on the Jenkins Agent node.

  

Conclusion

You have successfully configured Jenkins Controller-Agent Architecture on AWS EC2 using Amazon Linux. This setup allows Jenkins to distribute workloads across multiple agents, improving performance, scalability, and build execution efficiency.

Multi Cloud Architect (+91-9036367291)


Comments

Popular posts from this blog

Application Load Balancer - Config Steps by Girish

VPC Peering Connection by Cloud Architect (i.e Girish Babu)