# Day 28 : Jenkins Agents

### Jenkins Master (Server)

Jenkins’s server or master node holds all key configurations. 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 that actually execute 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 master(Where Jenkins is installed)
    
* The connection of master and agent requires SSH and the public-private key pair exchange.
    
* Verify its status under "Nodes" section.
    

1. For creating a node login into Jenkins. Go to Dashboard—&gt; manage Jenkins—→ new node
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743916790133/3d7cf4a8-6bf0-49ff-945d-b93c7083b96c.png align="center")
    
    2. Fill up the mentioned details for your new node
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743916868459/f41ad583-671c-4cd3-8d14-dcbc59c6d51c.png align="center")
        
    3. The no of executors will depend on the no of vcpu’s of your agent server. I have created server with only 1 vcpu therefore kept it as 1
        
    4. Mention the remote root directory as the path you defined in agent server. Here I have created a directory in agent server and mentioned it’s path. Select the launch method as launch agent via ssh.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743920942856/e97509e3-61c9-4f2e-8702-9f36e44c23fc.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743920981700/687f194a-1bd5-4a77-84b7-8dad0032ca35.png align="center")
        
    5. Mention the host as ip address as of second ec2 server and we will use another set of credentials by adding new credentials
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743921138964/d8b5324e-c58c-409a-bbad-1d0124cfd57e.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743921185140/23efb548-25ca-4c97-a7bf-c21ae3c7c1b5.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743921195389/4ad846ba-c8c0-41c1-89c6-b232ea77602e.png align="center")
        
        Add the private key created in master server before we start working on pipeline
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743921254027/a5959ac5-0fa9-4648-8469-5fc0a3db9737.png align="center")
        
    6. Click on save and see if the agent is created in node or not
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743921317172/3d1917a9-8b4f-42b1-9e68-892cdb22a752.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743921357726/51713ed8-b679-4781-b481-04260861147b.png align="center")
        
    7. Click on deploy agent and see if it is able to deploy without any issue or not
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743921417475/d95f0dd1-b692-48ce-8419-17ca01e17369.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743921429063/168eb697-6787-4caf-9dc6-ea54f849b456.png align="center")
        
        Now our agent is deployed successfully
        

**How we established connectivity between master Jenkins server and Jenkins agent server**

1. We created new ec2 server for Jenkins agent and installed java and docker into it.
    
2. We then logged into master server and generated a new key pair using command “ssh-keygen -t rsa” in .ssh folder
    
3. We then copied the public key from master server to agent server and tried to login into agent server from master server using command “ssh ubuntu@agentserverip”
    

### Task-02

* 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.
    

1. We will create a new pipeline and this time we will run our job on the new agent
    
2. This time we will use pipeline script from github i.e. pipeline script from scm
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743922532137/766c2c18-5c8c-4ae5-82e9-cc8137dd8fc5.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743922550955/f5561385-a529-4d40-8ed2-ddf66751efc2.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743922694215/7f9d724a-a851-423f-9c19-b0b276739835.png align="center")
    

3. Click on apply and build this pipeline
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743922766018/771d1387-1387-41ab-b052-24c29566af45.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743922781020/310b3c0d-d3c7-4e3d-b3c0-506d7d80c9a6.png align="center")
