Add Jenkins Build Agent

You want Jenkins to use a remote agent (server) to do the builds. As such, create two more servers to process tasks.

I want to process remote tasks on server 2 so add a label for ‘remote’

I want to process local tasks on server 3 so add a label for ‘local’

Basic set up of a server first off.

Create a jenkins user:

# useradd -d /var/lib/jenkins -c "Jenkins Remote Agent" -m jenkins 

Then create a public/private key as the jenkins user

$ ssh-keygen -t rsa 

The Jenkins master will have the Private Key so you’ll need to copy the id_rsa.pub key into the remote server’s authorized_keys file

$ cp .ssh/id_rsa.pub .ssh/authorized_keys

You’ll also need to copy the id_rsa.pub key to any remote server in order for uploading to succeed. We’ll get there in a few minutes.

Next, install the java application as that’s what Jenkins uses. You’ll need to check the master to see what’s installed.

Linux:cschelin@lnmt1cuomjkns1$ rpm -qa | grep jdk
java-1.8.0-openjdk-headless-1.8.0.242.b08-1.el7.x86_64 
copy-jdk-configs-3.3-10.el7_5.noarch 
java-1.8.0-openjdk-1.8.0.242.b08-1.el7.x86_64

And on the nodes, install the same version.

Installed:
  java-1.8.0-openjdk-headless.x86_64 1:1.8.0.272.b10-1.el7_9

Dependency Installed:
  copy-jdk-configs.noarch 0:3.3-10.el7_5             javapackages-tools.noarch 0:3.4.1-11.el7              lksctp-tools.x86_64 0:1.0.17-2.el7
  pcsc-lite-libs.x86_64 0:1.8.8-8.el7                python-javapackages.noarch 0:3.4.1-11.el7             python-lxml.x86_64 0:3.2.1-4.el7
  tzdata-java.noarch 0:2020d-2.el7

Complete!

Now we need the Private key for the agents.

In Jenkins, click Configure, Manage Nodes, and here, New Node.

Name the node, provide a description. Then the Number of Executors needs to be set. Rule of thumb here is the number of CPUs on the node is a good starting place. For a small site like mine, 2 is perfectly fine. A bigger system/site might have more assigned.

Remote root directory is /var/lib/jenkins

For the remote server, server 2, create the ‘remote’ label. For the local server, create the ‘local’ label.

Then select, “Only build jobs with label expressions matching this node”

Under Launch Method, select ‘Launch agents via ssh’

You’ll need to add a host. I always use IP in case DNS is slow or it fails to resolve.

Under Credentials, we’ll need to add the agent’s credentials. Remote and Local

You’ll give it a name so you can identify the credentials. Then a description. For the description, that’s what’s in the drop down listing so be clear with it. Then enter the name of the account (jenkins) and then add the id_rsa Private Key in the blank and save it.

Back at the Node screen, select the appropriate credentials and save the Node information.

It will initially fail mainly due to the known_hosts file not having the host key for the target servers. Log in to the master, sudo to jenkins, and ssh to the two nodes. Remember to use the name or IP you used when creating the agent configuration.

It should automatically try to connect to verify everything’s working. It took a bit for me to get it working and I had to delete the remote credentials and recreate to get it working but eventually it all worked.

Once done, in the various jobs, I have remote and local, in the configuration, add a “restrict where this project can be run” and select one of the two labels depending on the target location. I created the two servers so jkns2 goes to the remote server. So all the remote sites go here. And same with the local sites.

And since I’m using labels, should the remote one get busier for some reason, I can add a third node and give it the same label and jobs will run on both servers as needed.

And the last thing is to edit the main jenkins node. If it’s a busy site, you don’t necessarily need to change things but since we’re trying to replicate, edit the main node’s configuration and you can either change the number of Executor’s to 0 (zero) so no jobs run on the main node, or create a label such as ‘master’ which lets you run jobs on the master node. This also prevents a default run in case you forget to make a change to a project.

Don’t forget. It’s a shell script. Most anything you can do in a shell script, can be done in Jenkins in the shell option.

This entry was posted in Computers, Jenkins and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *