Jenkins is a powerful open-source automation tool that facilitates Continuous Integration (CI) and Continuous Delivery (CD). One of the key features that makes Jenkins highly scalable is its Master-Slave architecture. This setup allows Jenkins to delegate tasks to other machines (Slaves) while maintaining centralized control through the Master server.
In this article, we’ll walk through the entire process of setting up a Jenkins Master-Slave architecture from scratch. By the end of this guide, you'll have a fully functional Jenkins setup capable of distributing build and test tasks across multiple servers.
What is Jenkins Master-Slave Architecture?
In Jenkins:
Master: The Jenkins Master server is the main server that manages the Jenkins environment. It handles job scheduling, configuration management, and provides the user interface to configure Jenkins jobs, plugins, etc.
Slave: A Jenkins Slave is a remote machine that performs the actual work (such as building, testing, or deploying) that the Jenkins Master schedules. By adding Slaves, you can scale your Jenkins environment, improve performance, and avoid overloading the Master server.
The Master-Slave architecture allows you to distribute workloads across multiple machines, improving the overall speed and efficiency of your Jenkins pipelines.
Prerequisites
Before you begin, make sure you have:
Two servers: One for Jenkins Master and one for Jenkins Slave. These can be virtual machines, physical machines, or even cloud-based instances.
Linux knowledge: Basic knowledge of Linux commands for managing servers.
SSH access: SSH access to both Master and Slave servers.
Root or sudo access: Required for installation and configuration tasks.
A working network connection between the Master and Slave.
Step 1: Install Jenkins on the Master Server
Choose a server for the Jenkins Master:
This will be the central point of control for your Jenkins setup.Install Jenkins on the Master:
Depending on your Linux distribution, you can install Jenkins as follows:On Ubuntu/Debian:
sudo apt update sudo apt install openjdk-11-jdk sudo apt install wget wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee -a /etc/apt/trusted.gpg.d/jenkins.asc sudo sh -c 'echo deb http://pkg.jenkins.io/debian/ stable main > /etc/apt/sources.list.d/jenkins.list' sudo apt update sudo apt install jenkins
On CentOS/RHEL:
sudo yum install java-11-openjdk-devel sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key sudo yum install jenkins
Start Jenkins:
After installation, start Jenkins and enable it to start on boot:sudo systemctl start jenkins sudo systemctl enable jenkins
Access Jenkins Dashboard:
In your browser, navigate tohttp://<master-server-ip>:8080
to access the Jenkins setup wizard.
Step 2: Install Jenkins on the Slave Server
Choose a server for the Jenkins Slave:
This machine will perform the actual work, such as running builds, tests, and other tasks.Install Jenkins on the Slave:
You can install Jenkins on the Slave server following the same steps as for the Master server.
Step 3: Set Up SSH Between Jenkins Master and Slave
Set Up SSH Key Authentication:
To allow Jenkins to communicate with the Slave, you need to set up SSH key-based authentication.On the Master server, generate SSH keys:
su - jenkins ssh-keygen -t rsa
Copy the public key to the Slave server:
ssh-copy-id jenkins@<slave-server-ip>
- Verify SSH Access:
Test the SSH connection to make sure it’s working:
ssh jenkins@<slave-server-ip>
Step 4: Set Up Password Authentication on Both Master and Slave
Allow Password Authentication (if needed): On both the Master and Slave servers, edit the SSH configuration to allow password-based authentication (especially for environments that prefer password auth over SSH keys).
Open the SSH configuration file:
sudo vi /etc/ssh/sshd_config
Find and modify the following lines:
PasswordAuthentication yes PubkeyAuthentication yes
Restart SSH to apply the changes:
sudo systemctl restart sshd
- Set the Jenkins User Password: On both Master and Slave, set the password for the Jenkins user:
sudo passwd jenkins
This allows SSH access to the Jenkins user using a password if SSH keys aren’t used.
Step 5: Configure Jenkins Master to Recognize the Slave
Login to Jenkins Dashboard:
On the Jenkins Master, open your browser and go tohttp://<master-server-ip>:8080
.Manage Jenkins:
From the Jenkins dashboard, click on Manage Jenkins.Add a New Node (Slave):
Click on Manage Nodes.
Click New Node.
Enter a name for the Slave node and select Permanent Agent.
Configure the following:
Number of Executors: How many jobs the Slave can handle concurrently.
Remote Root Directory: The directory on the Slave where Jenkins will store files (e.g.,
/home/jenkins
).Labels: You can use labels to specify which jobs should run on the Slave.
Launch Method: Choose Launch agent via SSH.
Host: Enter the IP address of the Slave.
Credentials: Add SSH credentials:
If using Password Authentication, use the username and password.
If using SSH Key Authentication, add SSH credentials using the private key generated earlier.
Host Key Verification Strategy: Choose Non-verifying verification strategy.
Availability: Select Keep this agent online as much as possible.
Save and Test Connection:
After saving, Jenkins will try to connect to the Slave via SSH. If everything is configured correctly, the Slave node will appear as online in the Jenkins Dashboard.
Step 6: Verify the Setup
To verify that your Master-Slave setup is working:
Create a Test Job:
Create a simple Jenkins job and assign it to the Slave node.Run the Job:
Execute the job, and Jenkins should run it on the Slave node. You should be able to monitor the job’s progress through the Jenkins dashboard.
Conclusion
Congratulations! You’ve successfully set up Jenkins Master-Slave architecture. This setup allows Jenkins to distribute build and test tasks across multiple servers, improving efficiency and scalability.