Jenkins Master (Server)
Jenkins’s server or master node holds all key configurations. The Jenkins master server is like a control server that orchestrates all the workflow defined in the pipelines. For example, scheduling a job, monitoring the jobs, etc.
Jenkins Agent
An agent is typically a machine or container that connects to a Jenkins master and this agent executes all the steps mentioned in a Job. When you create a Jenkins job, you have to assign an agent to it. Every agent has a label as a unique identifier.
When you trigger a Jenkins job from the master, the actual execution happens on the agent node that is configured in the job.
A single, monolithic Jenkins installation can work great for a small team with a relatively small number of projects. As your needs grow, however, it often becomes necessary to scale up. Jenkins provides a way to do this called “master to agent connection.” Instead of serving the Jenkins UI and running build jobs all on a single system, you can provide Jenkins with agents to handle the execution of jobs while the master serves the Jenkins UI and acts as a control node.
Pre-requisites
Let’s say we’re starting with a fresh Ubuntu 22.04 Linux installation. To get an agent working make sure you install Java ( same version as Jenkins master server ) and Docker on it.
Note:- While creating an agent, be sure to separate rights, permissions, and ownership for Jenkins users.
Task-01
Create an agent by setting up a node on Jenkins
Create a new AWS EC2 Instance and connect it to the master(Where Jenkins is installed)
The connection of the master and agent requires SSH and the public-private key pair exchange.
Verify its status under the "Nodes" section.
Steps:
Create an EC2 Instance:
Log in to your AWS Management Console.
Navigate to the EC2 Dashboard.
Launch a new EC2 instance. Choose an Amazon Machine Image (AMI), and instance type, and configure other details. In this case, we're using Ubuntu AMI
In the "Configure Security Group" step, ensure that port 22 (SSH) is open.
Launch the instance and select an existing or create a new key pair.
Connect to the EC2 Instance: Use the private key associated with your key pair to connect to the EC2 instance via SSH.
Example:
ssh -i /path/to/private-key.pem ubuntu@your-ec2-instance-public-ip
Install Java and Jenkins on the Jenkins Master EC2 Instance:
sudo apt update
sudo apt install openjdk-11-jre
#Install Jenkins
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins
#Start jenkins
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
- Install Java and Docker on the Jenkins Agent EC2 instance:
##Java
$ sudo apt update
$ sudo apt install openjdk-11-jre
$ java -version
##Docker
$ sudo apt-get update
$ sudo apt-get install docker.io
- Verify the services are running on the two instances
Set up SSH Key Pair Exchange: Generate SSH key pair on your Jenkins Master EC2 Instance:
ssh-keygen
cd .ssh
ls
Copy the public key(id_rsa.pub) from the master to the Agent EC2 instance
In the Agent Instance:
cd .ssh
vim authorized_keys
- Edit authorized keys and paste the public key from the master instance.
Set up a new node:
Go to your Jenkins master web interface.
Navigate to "Manage Jenkins" -> "Manage Nodes and Clouds."
- In the "Launch method" select launch agent via SSH and in the "Host" section, input the public IP of the Agent instance
- "Add Credential" is where you will add your credentials if you don't have them saved already.Select "SSH username with private key". Fill all required details, and in the private key area add Private key from the master instance (id_rsa).
- Enter "Save"
Task 2
Run your previous Jobs (which you built on Day 26, and Day 27) on the new agent
Use labels for the agent, your master server should trigger builds for the agent server.