#90DaysOfDevops | Day 28

Rajendra PatilRajendra Patil
4 min read

Understanding Jenkins Agents

At the heart of Jenkins lies the master server, orchestrating the execution of pipelines and managing job workflows. However, as projects grow in complexity and scale, a single master may become overwhelmed. Enter Jenkins agents – the distributed workforce that executes tasks delegated by the master.

Jenkins Master (Server): The Jenkins master serves as the central control hub, housing configurations and coordinating pipeline workflows, job scheduling, and monitoring.

Jenkins Agent: An agent, whether a physical machine or container, connects to the Jenkins master to execute tasks defined in jobs. Each agent is assigned a unique label, allowing for targeted job execution.

Preparing for Deployment

Before diving into the agent setup, ensure you have the following prerequisites in place:

  • A fresh Ubuntu 22.04 Linux installation for the Jenkins agent.

  • Java, matching the version on the Jenkins master server, and Docker installed on the agent.

  • Access to the Jenkins master server for SSH key pair generation and Jenkins installation.

Task-01: Setting Up a Jenkins Agent

Before we begin, let's establish that we already have a Jenkins master server where we've practiced older blogs on Day 26 and Day 27. Today, our focus will be on creating a Jenkins agent server. However, keep in mind that you need to create two instances – one for the Jenkins master server (existing) and one for the Jenkins agent server.

Now, let's proceed with setting up the Jenkins agent server:

Create a New AWS EC2 Instance for Jenkins Agent:

  1. Go to the AWS Console and start a new EC2 instance named jenkins-server.

  2. Ensure that the instance meets the requirements for Java and Jenkins installation.

Install Java and Jenkins on Jenkins Agent:

  1. Log in to the Jenkins agent server.

  2. Install Java and Jenkins following the official installation instructions for Ubuntu.

      sudo apt update
      sudo apt install fontconfig openjdk-17-jre
    

      sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
        https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
      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
    

  3. Start Jenkins

      sudo systemctl enable jenkins
      sudo systemctl start jenkins
      sudo systemctl status jenkins
    

Generate SSH Key Pair on Jenkins Master:

  1. Open your terminal on the Jenkins master server.

  2. Use the command:

      ssh-keygen -t rsa
    

  3. Follow the prompts to specify the file location and passphrase (optional).

Copy Public Key from Jenkins Master to Jenkins Agent:

  1. Copy the public key (id_rsa.pub) generated in Step 2 from the Jenkins master server.

    • Open the id_rsa.pub file using a text editor:

          cat ~/.ssh/id_rsa.pub
      

    • Copy the entire content of the id_rsa.pub file.

  2. Paste the public key into the authorized_keys file on the Jenkins agent server.

    • Connect to your Jenkins agent server.

    • Navigate to the .ssh directory (create it if it doesn't exist):

          mkdir -p ~/.ssh
      
    • Open the authorized_keys file using a text editor:

          nano ~/.ssh/authorized_keys
      
    • Paste the content of the id_rsa.pub file into this authorized_keys file and save the changes.

Establish SSH Connection between Jenkins Master and Agent:

Use the private key (id_rsa) generated in Step 3 on the Jenkins master server to establish an SSH connection with the Jenkins agent server.

ssh -i /path/to/private_key.pem user@agent_ip_address

Verify Node Status on Jenkins Master:

  1. Log in to the Jenkins master server.

  2. Navigate to "Manage Jenkins" > "Manage Nodes and Clouds" to ensure that the agent node appears under the "Nodes" section.

  3. Now, our agent is connected

Task-02: Configure Jenkins Jobs to Run on the New Agent:

Navigate to the configurations of previous jobs built on Day 26 and Day 27. Configure the jobs to run on the newly created agent using labels. Save the job configurations.

Trigger Job Builds to Run on the Agent:

Initiate job builds to trigger execution on the agent. Monitor the job status to ensure successful execution on the agent.

Happy Learning....

0
Subscribe to my newsletter

Read articles from Rajendra Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rajendra Patil
Rajendra Patil