As an automation server that supports a list of Social Control Management (SCM) software systems such as SVN, Git, and Mercurial, Jenkins features many plugins to help automate any project. It is considered a great choice for a ‘master/slave’ architecture and it’s a perfect host for a number of large projects.
Jenkins ‘master/slave’: How it Works?
The master and slave architecture for Jenkins is used in a build environment where there is a distribution of workload for different projects to a number of agent nodes. Different environments can be used for each build.
The master in this setting deals with tasks related to the build system. The master node is used in various tasks such as job schedules, slave nodes monitors, dispatch of builds to slave nodes, build jobs execution, and keeping build result records.
Jenkins slave nodes are tasked with offloading all the build projects from the master. This requires an established connection that should be available between the master and slaves.
In this article, we will look at the setup process for Jenkins-master/slave architecture using Ubuntu 18.04 LTS.
Before you start, you need the following:
- Ubuntu 18.04 LTS
- Jenkins Automation server installed on Ubuntu 18.04 If you have not installed Jenkins Automation server follow Our guide on “How to install Jenkins Automation server on Ubuntu 18.04” to install the application.
- The latest version of master (10.0.15.10)
- Slave 01 version 10.0.15.21
- Slave 02 Version 10.0.15.22
- Root privileges via sudo
Let’s get started!
Step 1- Installing Jenkins Master
In this article, we will not go into details on the basic Jenkins installation.
If you have already installed the Jenkins application on your Ubuntu 18.04, you should have an interface like the one below.
We can now proceed and set up the Jenkins master.
Step 2 - Configuring Credentials for Jenkins Master
Once you install Jenkins master server, go ahead and configure it. Jenkins default setting allows us to start the agent nodes in different ways. They can either be launched through the windows administrative account, SSH, or through Java Web Start (JNLP).
You should choose one that fits your need based on the environment and your operating system (OS).
In this tutorial, we will use SSH to launch these agent nodes, then set up the credentials for Jenkins on the master server.
Generating SSH Key
We need the SSH authentication key to launch the agent nodes. Now generate the key for Jenkins user then use ‘ssh-copy-id’ to upload this key manually to each server node. Run the commands below in the Jenkins master server to generate the keys:
su - jenkins ssh-keygen
The command above will create a private key ‘id_rsa’ and a public key ‘Id_rsa.pub’ in the ‘.ssh’ directory.
Setting up Credentials on Jenkins
Go to Jenkins dashboard then open the Credentials menu..
Select the “global domain link” then click Add Credentials.
Next, select the authentication method you want to use. You need to provide the following information:
Kind: SSH Username plus the private key
Private key: Enter and paste the ‘id_rsa’ private key for Jenkins user
Now press OK.
Now, the details for Jenkins with ssh authentication key technique has been created.
Step 3: Setting up Slave Nodes
The next step is to set up the slave nodes server. To do so, you need to install java on your server then create a Jenkins user.
Start by installing the software packages then include the PPA repository for java. This will be accomplished using the apt command on your Ubuntu 18.04 command line.
$ sudo apt install software-properties-common apt-transport-https -y $ sudo add-apt-repository ppa:openjdk-r/ppa -y
Use the following apt to install java OpenJDK.
$ sudo apt install openjdk-8-jdk -y
Once the installation is done, run the command below to confirm the java version you have installed.
You should see the current version of Java OpenJDK installed on your system.
Adding a New User for Jenkins
Next, ensure each of the agent nodes has a Jenkins user using the following command:
useradd -m -s /bin/bash Jenkins passwd Jenkins
At this point, Jenkins user has been created and you can proceed to upload the key from the master to slave server nodes.
Uploading the Public SSH Key
The next step is uploading the ‘id_rsa.pub’ key from the master to slave server nodes. Use the ‘ssh-copy-id’ to upload the key to each node:
ssh-copy-id firstname.lastname@example.org ssh-copy-id email@example.com
Now, enter the password for the Jenkins user.
After that, the ‘id_rsa.pub’ key should be uploaded successfully to each of the agent nodes.
Step 4 - Adding Slave Nodes
Go to the Jenkins dashboard and select Manage Jenkins, then hit the Manage Nodes.
Now, press the New Node icon.
Enter the name of the node ‘slave01,’ then select the permanent agent , and press OK.
Now, enter the following information for this node.
Description: slave01 node agent server
Remote root directory: /home/Jenkins
Labels: slave 01
Launch method: Launch slave agent using SSH, then enter the IP address (10.0.15.21) for the host and select the auth using “Jenkins” details.
Save the changes and make sure the master server is connected to all the agent nodes before launching the agent services.
Once the master level has connected successfully to the agent nodes, you will see the screen below:
Now, the slave nodes have been added successfully to the Jenkins master server.
Step 5 - Preparing Slave Nodes to Perform Build
To configure Jenkins master to perform build on the slave agent nodes, select Manage Jenkins then Configure System.
In the Slave Setups area and set everything as follows:
You can find more information about Slave Setups on its official page.
Press Save to complete the process.
Step 6 - Testing
The next step is to create your own Jenkins build. In this case, we shall perform the build on agent nodes ‘slave01’ and ‘slave02.’
Go to Jenkins dashboard, and select the New Item menu.
Enter the name of the item you want, select the freestyle project, then press OK.
Next, on the Jenkins general section, define the job details and make sure you have checked the option ‘Restrict where this project can be run.’
Enter your node such as ‘slave01’ on the Label Expression section.
Execute Shell and run the following command:
top -b -n 1 | head -n 10 && hostname
Save the changes. You should see the following page:
Now, select Build Now to initiate the project, then select Item the Build History section.
The build on the ‘slave01’ should give you the following result:
Build on the ‘slave02’ should look like this:
Now everything is done.
At this point, you have successfully configured Jenkins master and slave architecture on Ubuntu 18.04. We hope this tutorial will help you execute the process with ease.
Check out these top 3 VPS services: