Day 23 of #90DaysOfDevOps

Day 23 of #90DaysOfDevOps

Today we will deep dive into some Jenkins' terminologies such as node, agent, controller and executor. Then, we will see how to add an agent to Jenkins.

Node

A machine which is part of the Jenkins environment and capable of executing Pipelines or jobs. Both the Controller and Agents are considered to be Nodes.

Nodes are the "machines" on which build agents run. Agents manage the task execution on behalf of the Jenkins controller by using executors. An agent can use any operating system that supports Java. Tools required for builds and tests are installed on the node where the agent runs; they can be installed directly or in a container (Docker or Kubernetes).

To conclude:

In practice, nodes and agents are essentially the same but it is good to remember that they are conceptually distinct.

Controller

The central, coordinating process which stores configuration, loads plugins, and renders the various user interfaces for Jenkins.

Agent

An agent is typically a machine, or container, which connects to a Jenkins controller and executes tasks when directed by the controller.

Executor

A slot for execution of work defined by a Pipeline or job on a Node. A Node may have zero or more Executors configured which corresponds to how many concurrent Jobs or Pipelines are able to execute on that Node.

Node creation for setting up agent in Jenkins

  • Create a linux instance (Am choosing Ubuntu EC2 instance)

  • Install Java on agent

    sudo apt-get update

    sudo apt install -y --no-install-recommends openjdk-17-jdk-headless

  • Let’s now verify if java works for us:

    java -version

  • Create a jenkins user

    sudo adduser --home /home/jenkins --shell /bin/bash jenkins

    Setting up Jenkins Agent Using SSH keys

  • Step 1: Log in to the agent server as a jenkins user.

  • Step 2: Create a .ssh directory and cd into the directory.

    mkdir ~/.ssh && cd ~/.ssh

  • Step 3: Create an ssh key pair using the following command. Press enter for all the defaults when prompted.

    ssh-keygen -t rsa -C "The access key for Jenkins slaves"

  • Step 4: Add the public to authorized_keys file using the following command.

    cat id_rsa.pub >> ~/.ssh/authorized_keys

  • Step 5: Now, copy the contents of the private key to the clipboard.

    cat id_rsa

  • Now, from jenkins master copy the public key(id_rsa.pub) from jenkins user and paste in authorized_keys file in agent node, to enable connectivity from Jenkins master to agent node via SSH.

    cat /home/jenkins/.ssh/id_rsa.pub -- Jenkins Master

    vi /home/jenkins/.ssh/authorized_keys -- Jenkins Agent

Set-up Agent in the Jenkins UI

  • Before we begin, make sure we have Publish Over SSH plugin installed in Jenkins.

  • Go to your Jenkins dashboard

  • Go to Manage Jenkins option in the main menu

  • Go to Nodes option under System Configuration

    • Go to New Node option in the side menu

    • Fill in the Node name ("Agent") and type ("Permanent Agent")

    • Click on the Create button

    • In the Description field, enter if you want a human-readable description of the node ("Node with Java and Docker installed" )

    • Let 1 as the number of executors for the time being. A good value to start with would be the number of CPU cores on the machine (unfortunately for me, it’s 1)

    • As Remote root directory, enter the directory where you want to install the agent (/home/jenkins/jenkins-agent for me)

    • Regarding the Labels field, enter the labels you want to assign to the node (Ex: Agent).

    • For the Usage, choose Use this node as much as possible for the time being, you will be able to restrict later on the kind of jobs that can be run on this node.

    • For the Launch Method, I will choose launch agents via SSH. So you need to set SSH connection between your Jenkins host and the node first and Add the credentials for node in credentials manager in Jenkins host.

    • Use public IP of your server for Host and choose "Non verifying Verification Strategy" for Host Key Verification Strategy?

    • For Availability choose Keep this agent online as much as possible.

How to add credentials in Jenkins UI

  • Go to Jenkins dashboard

  • Go to Manage Jenkins

  • Go to Credentials under Security

  • Then go to System > Global credentials(Unrestricted) > Add credentials

  • Choose Kind as SSH username with private key.

  • Add username and private key selecting enter directly. Add private key(id_rsa) from agent server.