How to add Jenkins remote build agent based on Ubuntu 18.04

How to add Jenkins remote build agent based on Ubuntu 18.04

Overview:

  1. Jenkins server based on CentOS 7 machine
  2. Jenkins remote build-agent based on Ubuntu 18.04 machine

On remote Ubuntu system:

  1. SSH into your system
  2. Create Jenkins home directory:
    sudo mkdir /var/lib/jenkins
    
  3. Check if jenkins folder was created:
    ls -l /var/lib/ | grep jenkins
    
  4. Create jenkins username for build agent with home directory /var/lib/jenkins. Master node will use this directory to write data into. jenkins user will be used to access Jenkins server:
    sudo useradd -d /var/lib/jenkins jenkins
    
  5. Make jenkins user to have permissions to operate on /var/lib/jenkins directory:
    sudo chown jenkins:jenkins /var/lib/jenkins
    
  6. Check if user jenkins has permissions on the directory:
    ls -l /var/lib/ | grep jenkins
    
  7. Generate a SSH key pair:
    ssh-keygen
    
  8. Create .ssh folder to store the SSH key pairs:
    sudo mkdir /var/lib/jenkins/.ssh
    
  9. Copy the contents of the generated public key (e.g. id_rsa.pub):
    cat ./.ssh/id_rsa.pub
    
  10. Create authorized_keys file under jenkins home folder and store the generated public key there:
    sudo vi /var/lib/jenkins/.ssh/authorized_keys
    
  11. Install Java environment on the build agent machine:
    sudo apt install -y openjdk-8-jre-headless
    
  12. Check environment setup:
    java -version
    
  13. Copy the contents of the private key:
    cat ./.ssh/id_rsa
    

On Jenkins master node UI dashboard:

  1. Go to Manage Jenkins --> Manage nodes and clouds --> New Node
  2. Specify the worker node name and select Permanent Agent (physical server): agent
  3. In remote home directory box specify the /var/lib/jenkins folder created on build agent machine. For launching method select Launch via SSH. In the Host specify the IP or DNS address of the build agent machine.
  4. Under credentials add build agent credentials for access. Select SSH username with private key: ssh
  5. Under Host verification strategy select Known hosts file Verification Strategy
  6. After saving configuration you will see your worker node: worker

On Jenkins master node machine:

  1. SSH into Jenkins master node machine
  2. In order to make your build agent connect with Jenkins master node we need to add build agent entry into known_hosts file. After logging into your master node, SSH into your build agent machine to add entry into known_hosts file:
    ssh [username]@[ip_address]
    
  3. Back on your master node machine create .ssh directory on Jenkins home library if it doesn't exist:
    sudo mkdir /var/lib/jenkins/.ssh
    
  4. If no configuration is presented currently then (otherwise it will override the configuration, you can manually add it if needed) copy the known_hosts file into Jenkins .ssh folder:
    sudo cp ./.ssh/known_hosts /var/lib/jenkins/.ssh
    
  5. In the UI dashboard in the agent logs section you will find a successful connection message: message

Making builds running only on remote agent:

  1. In order to make default builds in Jenkins to run on remote agent, we need to globally configure master build runs by using labels. To do this go to Manage Jenkins --> Configure system --> Choose label for master (e.g master) --> In Usage form select "Only build jobs with label expressions matching this node" --> Save changes: master
  2. This change will make sure that if you want to run build on master node, you need to explicitly define this in job configuration by choosing "master" label: label
  3. All the rest of jobs by default will run on remote build agent

Notes:

  1. The authorized_keys file in SSH specifies the SSH keys that can be used for logging into the user account for which the file is configured. It is a highly important configuration file, as it configures permanent access using SSH keys and needs proper management. It stores public keys. Link
  2. The known_hosts File is a client file containing all remotely connected known hosts, and the ssh client uses this file. This file authenticates for the client to the server they are connecting to. The known_hosts file contains the host public key for all known hosts. The use of this file is optional, but, if used then it is prepared by the system administrator. Link
  3. What is actually in known_hosts?
  4. Linux : SSH known_hosts