# Day 28 Task: Jenkins Agents

#   
📌 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 actually 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.

An agent is typically a machine or container that connects to a Jenkins master and this agent actually 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.

[![](https://user-images.githubusercontent.com/115981550/215276859-fa140ab7-e905-41c9-8ae2-1eef577c5e72.png align="center")](https://user-images.githubusercontent.com/115981550/215276859-fa140ab7-e905-41c9-8ae2-1eef577c5e72.png)

## 📌 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.

# 📌 Task-01 Setting up Jenkins agent

Create an agent by setting up a node on Jenkins

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693904653965/377d1c4f-4079-41da-966d-497a77fd0d6f.png align="center")

Create an AWS EC2 instance of the agent node. Create a new AWS EC2 Instance and connect it to the master(Where Jenkins is installed)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693904433280/2f0ee020-dac1-4b22-b2bc-4e94b04059aa.png align="center")

Installed Java on agent node confirm Jenkins master and agent node have same Java --version

```basic
sudo apt update

sudo apt install docker.io docker-compose

sudo apt  install openjdk-11-jre

usermod -aG docker $USER
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693910162560/d88f2c90-8506-47a8-b1b2-1ce8f98fedab.png align="center")

The connection of the master and agent requires SSH and the public-private key pair exchange.

Create the SSH key pair on the Jenkins master node

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693906419920/7dd62fc8-eae9-4ddb-8aaf-2690c33780ea.png align="center")

Then go to the .ssh folder and cat id\_rsa.pub key and copy this key

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693906474320/20b77ee7-ea74-4c8a-9447-7961963125b9.png align="center")

Add this key to the Jenkins agent server in .ssh/authorized\_keys

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693906669201/6d2f8580-a3e0-436c-81f5-c6f740f7e53e.png align="center")

Then go to the Jenkins dashboard in that go to manage Jenkins/Nodes and click on Create the new node.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693906820586/eaa629aa-761e-42af-a8f3-4bb4b3cb4b59.png align="center")

Add the new agent

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693906907567/3a38a369-cc3a-4b2a-8202-a304d058fe8a.png align="center")

Add the below details

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693907074327/9e76c680-172c-496e-abf2-da2e7d691986.png align="center")

Add the public IP in the host and create credentials

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693908064438/ee04aa0a-14ee-430f-850b-3d24b4c5e0af.png align="center")

Create the credentials

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693907293408/de6e2c7f-fffb-4a89-a630-6bd0dbea55ac.png align="center")

cat the id\_rsa in Jenkins -master and copy this key

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693907467840/8b6a1256-a5df-45c2-8ab3-400f162127d0.png align="center")

Add this key private key section in the credentials

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693907553684/d9071d96-1397-46fb-b308-2afb6c8c2f59.png align="center")

Save the changes

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693908089291/5bb68824-eeb4-40fa-9a72-123d685844ab.png align="center")

Successfully connect agent node to Jenkins master

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693908959297/4a6483e7-44a8-4a02-a124-39f723550281.png align="center")

## 📌 Task-02 Run Jenkins job on the agent node

Run your previous Jobs (which were built on Day 26, and Day 27) on the new agent

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693909694009/9d345bdc-86fa-48ef-93f9-259631da9889.png align="center")

Successfully build the pipeline

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693909854808/04fb7946-5665-47b9-9644-bf6d64d5dd34.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693909821841/7bff6ae4-477f-44cc-9978-f12353daf4e3.png align="center")

# Thank You
