Jenkins Master/Slave Setup
6 min readFeb 16, 2024
Jenkins’ master/slave architecture allows you to distribute the workload of executing Jenkins jobs across multiple nodes. The master node is responsible for managing the Jenkins system, storing configurations, and handling user interactions through the web interface. The slave nodes, also known as agent nodes, are responsible for executing the jobs assigned to them by the master.
Jenkins Master
Your main Jenkins server is the Master. The Master’s job is to handle:
- Scheduling build jobs.
- Dispatching builds to the slaves for the actual execution.
- Monitor the slaves (possibly taking them online and offline as required).
- Recording and presenting the build results.
- A Master instance of Jenkins can also execute build jobs directly.
Jenkins Slave
A Slave is a Java executable that runs on a remote machine. Following are the characteristics of Jenkins Slaves:
- It hears requests from the Jenkins Master instance.
- Slaves can run on a variety of operating systems.
- The job of a Slave is to do as they are told to, which involves executing build jobs dispatched by the Master.
- You can configure a project to always run on a particular Slave machine or a particular type of Slave machine, or simply let Jenkins pick the next available Slave.
How Jenkins Master and Slave Architecture works?
Now let us look at an example in which we use Jenkins for testing in different environments like Ubuntu, MAC, Windows, etc.
The diagram below represents the same:
- Jenkins checks the Git repository at periodic intervals for any changes made in the source code.
- Each builds requires a different testing environment which is not possible for a single Jenkins server. In order to perform testing in different environments, Jenkins uses various Slaves as shown in the diagram.
- Jenkins Master requests these Slaves to perform testing and to generate test reports.
Now, let’s start this setup by installing Jenkins and configuring further.
Step 1: Install Jenkins on Master Node
- Jenkins is built on Java so, to install Jenkins we first need to install Java on the linux server.
- Run the below command on the Jenkins master node.
sudo apt install openjdk-11-jdk -y
- The above commands will install Java on the Jenkins master node.
- Run the below commands on the Jenkins master node.
sudo apt update
sudo apt install openjdk-17-jre
java -version
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins
sudo systemctl start jenkins.service
sudo systemctl status jenkins
- The above commands will install Jenkins on the Jenkins master node.
Step 2: Install Java on the Agent Node
- On the agent node we don’t actually need to install the Jenkins however, jenkins only works when java is installed so we just need to install java on the agent node.
- Run the below command to install java on the agent node
sudo apt install openjdk-11-jdk -y
Now, our setup is ready. We will configure the master-agent architecture now.
Step 3: Configuring the Master-Agent setup
- Go the Jenkins UI and click on Set up and agent
- Add the node name as per your choice
- Add the below details
- Add the description as per your need
- Number of executors: This indicates that how many parallel jobes this node will execute. As of now I have set it to 2
- Labels: Add some label so that we can select node based on the label for executing the job
- custom workdir path: Whenever we create any job in the jenkins it will create workspace in the backend, however we have not installed Jenkins in the agent node so, we need to define any directory for the workspace. As of now I have set it to /home/ubuntu
- Usage: Set this parameter to Use the node as much as possible so that we can fully utilise the node
- Launch method: Set this parameter to Launch agent by connecting the controller
- Click on Disable WorkDir
- Availability: Set this parameter to Keep the agent online as much as possible. So, it will make sure to keep the node online as much as possible
- Click on save.
- After that you will see the below page.
- Run the Run from agent command line: (Unix) on the master node
- After running the command you can see the below page and you can see agent is connected over there.
- Now, our master slave setup is ready. We can execute the job on the agent node
- Create a new job and configure as per the below image
- Click on the Restrict where the project can be run and under the label expression select the label of the agent node
- Under the build steps select execute shell and add the below command
uptime
echo $WORKSPACE
- Click on save and apply.
- Before running the job you can verify the uptime of your agent node.
- Click on build now and execute the job
- In the above image you can see the uptime is 33 min and workspace is also /home/ubuntu/workspace/urara
- That’s it. Our setup is ready. Now, you can create different jobs and execute on the agent node as per your need.
thank you for reading !!!!