Jenkins Agent Setup Step-by-Step

Jenkins Agent Setup Step-by-Step

ยท

3 min read

Jenkins agent Setup in AWS Instance

  1. I'm going to set up a Jenkins agent in AWS using any cloud services of your choice. We will be using one master and one agent, but you can add as many agents as you need.

  2. Let's create two Linux instances, one for the master and one for the agent. I will guide you through setting up the agent since I have already created a blog post on how to set up the master.

  3. Now connect to the Jenkins agent instance and We have to install the following:-

  • Java:-

      sudo apt update
      sudo apt install openjdk-11-jreJenkins
    
  • Docker:-

      sudo apt-get update
      sudo apt-get install ca-certificates curl gnupg
    
      sudo install -m 0755 -d /etc/apt/keyrings
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
      sudo chmod a+r /etc/apt/keyrings/docker.gpg
    
      echo \
        "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
        "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
        sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    
    Note on Jenkins Agent
    The agent node is responsible for executing the Jenkins jobs that are distributed by the Jenkins master. It does not require a full installation of Jenkins.

    Jenkins Web Interface Configuration

  1. Now Access Jenkins Web Interface: Open a web browser and enter the public IP address or DNS name of your EC2 instance followed by the Jenkins port (e.g. http://<master_public_ip>:8080).

  2. To create a new node in Jenkins, follow these steps:

    1. Go to the Jenkins web interface and log in as an administrator.

    2. Navigate to the "Build Executor Status" page.

    3. Click on the "+ New Node" button to create a new node.

    4. Provide a name for the node in the "Node name" field.

    5. Select the "Permanent Agent" option to make it a persistent agent.

      Configure other settings for the node, such as the number of executors, labels, usage, and availability options.

  1. Configure other settings for the node, such as the number of executors, labels, usage, and availability options.

    1. Remote root directory:- We need to enter our root directory here for this type "pwd" in your agent terminal.

  1. Launch method:- Launch Agent via SSH

  2. Host:- Here we have to enter <agent_node_public_ip>

  3. Credentials:- Add Jenkins Credentials

  4. In Credentials:-

    1. Kind - SSH Username with a private key

    2. ID - An internal unique ID by which these credentials are identified from jobs and other configurations.

    3. Description - Add a description to help tell similar credentials apart.

    4. Username - Enter the username of your agent node, In my Case it's 'Ubuntu'.

    5. Private Key - Enter Directly then add a private key and save it.

      *Note - Here we have to enter the private ssh key of our Master node.

      After adding all Credentials it will look like this,

  1. Save the configuration to create the new node.

Jenkins Master and Agent Node Setup

I have already created a blog post on how to set up the master. Now we are going to connect the master node and agent using ssh.

  1. Set up SSH keys: Generate SSH keys on Jenkins master. This will allow them to authenticate each other without passwords. You can use the

    ssh-keygen -t ed25519 command to generate the keys.

  2. Add Master SSH key to the agent: On the agent master machine, add the public key of the master to the ~/.ssh/authorized_keys file of the user account running the Jenkins agent. This will allow the master to authenticate the agent.

  3. From the Jenkins Web Interface go to the agent page and click on the "Launch agent" button to test the SSH connection between the master and the agent.

If all the steps are followed correctly, Jenkins should be able to establish an SSH connection with the agent machine. The agent will then be ready to execute jobs and tasks assigned by the Jenkins master.

~Puneet ๐Ÿ™ƒ

ย