Author: Chandu Bodduluri
Jenkins master-slave configuration, also known as Jenkins distributed builds, is a setup in which a Jenkins master server delegates tasks to one or more Jenkins slave nodes to distribute the workload and improve the efficiency of continuous integration and continuous delivery (CI/CD) processes. Here’s how it works:
Jenkins Master: The Jenkins master server is the central control point. It manages the scheduling and monitoring of jobs, user authentication, and the web-based user interface. It also stores the configuration and metadata of all jobs.
Jenkins Slave Nodes: The Jenkins slave nodes are additional machines that perform the actual build and test jobs. These nodes can be either physical machines or virtual machines, and they are typically connected to the master via the network. Each slave node can have its own set of tools and configurations.
Setting up a Jenkins master-slave configuration involves configuring the master to recognize and manage the slave nodes, usually through SSH or JNLP (Java Network Launch Protocol) connections. In this blog post we will understand the process of establishing the Jenkins master-slave relationship between two machines on Windows using SSH.
On your master and slave agent, make sure that both OpenSSH server and OpenSSH client are installed.
To do this, open Settings, navigate to Apps-> Optional features. Check if OpenSSH Server is already present in installed features. OpenSSH Client will be installed by default. If not present, click on view features in Add an optional feature and install.
Next, open Control Panel->System and Security->Windows Defender Firewall->Allow an app or feature through windows defender firewall. Scroll down and make sure that the OpenSSH server option has been enabled.
Make sure that the master and agent are on the same network. On the master system, open PowerShell as an admin and type Start-Service sshd
Open command prompt as an admin, type ssh-keygen. Leave the options as default and press enter. Once done, the private key(id_rsa), public key(id_rsa.pub) will be saved in the default path C:\Users\<username>\.ssh
Repeat the same process on the agent system to generate public and private keys!
On the master, open the command prompt and enter ssh <agent username>@< agent IP address>. Enter the password and you should be able to take remote control of the agent system.
Repeat the same process on the agent system and make sure it is able to ssh the master system.
SSH Configuration on Jenkins Web UI
Once done, open the Jenkins web UI on the master system, click on Manage Jenkins-> Credentials under Security.
Click on Add Credentials, select the option username with password. Enter the username and password of the agent system. All the other fields are optional and can be left empty. Click on Create.
- Go back to the dashboard, click on Manage Jenkins, click on Nodes under System Configuration.
- Click on New node, enter the node name, select the kind as permanent agent and click on Create.
- Enter any path on the agent system in the field Remote root directory( Ex: C:\Jenkins).
- Under Usage select ‘Launch agent via SSH’. In the host section, enter the IP address of the agent.
- Under the credentials section, select the credential that was previously added as shown in the above picture. Click on Save
Once done, click on the Relaunch agent, wait for some time and the node should be successfully launched and connected to the master.
Running jobs on slave agents from the master
Go back to the dashboard, click on the required project, click on Configure and choose the option ‘Restrict where this project can be run’ and select the label on the node which you want to run. Multiple nodes can be mapped to the same label and run simultaneously to perform parallel execution.
Save the changes and click on build now. Once the build is successful, the logs can be checked in console output.
In this way, Jenkins master can allocate slave nodes, schedule jobs, and distribute builds to multiple slaves for execution. Additionally, it can monitor the slave node’s state, get build results from slave nodes, and report them to the console.
Key benefits of using a Jenkins master-slave configuration
Parallel Execution: Multiple build or test jobs can run simultaneously on different slave nodes, allowing for faster job execution.
Distributed Resources: You can allocate specific types of slave nodes for particular tasks, optimizing resource utilization. For example, you could have Windows slave nodes for Windows builds and Linux slave nodes for Linux builds.
Scalability: You can easily add or remove slave nodes to accommodate changing workloads and resource requirements.
Isolation: Jobs running on slave nodes are isolated from each other, reducing potential conflicts and resource contention.
High Availability: If one slave node fails, the master can reroute jobs to other available nodes, ensuring high availability.
Jenkins’ master-slave configuration using SSH is a game-changer for organizations aiming to streamline their CI/CD processes. By distributing tasks efficiently, isolating resources, and enabling scalability, it empowers development teams to deliver high-quality software faster and more reliably. If you haven’t used this powerful feature yet, it’s time to consider implementing it in your Jenkins setup. Happy exploring!