Jenkins Master Slave

How to Configure Jenkins Master Slave Setup.

Jenkins is an opensource tool which will automate some of the software development lifecycle tasks like build, test, deploy and more. Well, we have talked enough about Jenkins. In this article, we will discuss how to configure Jenkins master Slave Setup also called Master-Agent setup.

Before that, we will see why we need master-slave set up in Jenkins. When we build the Jenkins job, Workload of the single node Jenkins will take the master’s resource and wait for the queued process to finish. This is time-consuming and sometimes, bigger build task may break the Jenkins server. Even in some cases, a single build job needs a different environment for different tasks. To overcome this issue, Jenkins has a feature called distributed Build and followed by The Jenkins Master-Slave Setup.

Distributed Builds

So, let us see how the Distributed Build is working. Ideally, the machine where we install complete Jenkins will be Jenkins master. Which will open in “http://YOUR_IP:8080”. On the slave machine, we will install a runtime program called Agent. Installing Agent will not install complete Jenkins, but this Agent will run on JVM. It is capable enough to run the subtask or main task of the Jenkins in a dedicated executor.

Jenkins Master Slave Architecture
Jenkins Master Slave Architecture

We can have N number of Agent node or slave node and we can configure Master node to decide which task or job should run which agent and how many executor agents can have. The communication between Master and Jenkins slave nodes is bi-directional and it is happening with TCP/IP connection protocol.

Invoking Slaves are happening in many ways and we need to pick up the right method for our need. Let’s see all the method one by one.

Launching Agent with SSH from Master

Jenkins will talk with the Agent node with SSH and this is the better method for the Unix based Agent node as Unix is having inbuilt SSH setup. But, note that we need to Configure Jenkins Credentials beforehand. Let’s see how these Credentials need to be configured quickly.

  1. Go to the Jenkins Home Page -> Credentials ->System -> Global credentials
  2. Click Add Credentials -> Select username with password or SSH Username with the private key then save
    1. If username with password option is chosen, Simply, Username and password of “Jenkins” are enough.
    2. If SSH Username with Private Key is an option, then follow the steps
      1. Create ssh key pair by entering “ssh-keygen -t rsa -C "Get Key”
      2.  Add the key to authorized keys by entering cat id_rsa.pub > ~/.ssh/authorized_keys
      3. Then Copy the private key from “id_rsa” file from the .ssh directory.
      4. Then in creating new credentials page select “SSH Username with private key” kind and enter directly the copied private key and save.

Launch Agent via Java Web Start

In this case, a JNLP file will be opened on the agent machine, this will establish a TCP connection to the Jenkins master. By doing this, Jenkins agent will directly communicate to master but master need not communicate with the agent. By default, the JNLP agent will launch a GUI, but it’s also possible to run a JNLP agent without a GUI, e.g. as a Window service.

Launch agent via execution of command on the master

Start the Agent by running the custom command from the master node. When the master can directly assess and execute the command for an agent from the master, this method can be used.

Jenkins Master-Slave Setup.

As we discussed earlier, we are going to have a master Jenkins and a Slave Jenkins for this article. So, assuming the following prerequisites and configurations are made already.

  • Jenkins Server installed and up and Ready
  • Another server for configuring slave (In this article, we are taking Ubuntu 16.04 with 1GB memory)
  • Both the Jenkins server and slave server are in the same network.

Let’s Configure the Master.

Master Configuration

Log in to the Jenkins server and follow the following steps to configure the Master server.

  1. Go to Manage Jenkins -> Manage Nodes -> New Node
  2. Enter the Node Name (Here, Jenkins_slave_1) there and select Permanent Agent then click OK
  3. Then a new form will open and enter the details one by one as follows.
    1. Edit the Description (Optional)
    2. Enter the # of Executor (One or more, In our case we are going to set up 2 Executors)
    3. Then enter the Remote FS Root which is going to be the root directory of the Jenkins slave from the master. (In our case, /var/lib/jenkins)
    4. Select the launch Method. (In Our case, we are selecting Launch Agent via SSH)
      1. Enter the Host address (In our case, 172.28.128.20)
      2. Select the Credentials Which is created in the “Launching Agent with SSH from Master” Section of this article.
      3. Select “Non verifying Verification Strategy” for Host key Verification Strategy.
  4. Others Keep default. (Click help icon and know everything from the page itself)
  5. Then click Save button.
Jenkins Slave Node Configuration (Launch Agent via SSH)
Jenkins Slave Node Configuration (Launch Agent via SSH)

Slave Configuration (If “Launch agent via Java Web Start” option is selected).

If “Launch Agent via Java Web Start” is select in Jenkins Slave creation page (Like in figure)

Jenkins Slave Node Configuration (Launch Agent via Java Web Start)
Jenkins Slave Node Configuration (Launch Agent via Java Web Start)
  1. From the windows machine (Windows slave), open any web browser and open the Jenkins Master server (In our case, http://172.28.128.10:8080).
  2. Log in and go to Manage Jenkins -> Manage Nodes, then click the created Node.
  3. It will show you the Popup window which will have “Launch” Button.
jenkins slave JNLP file download
JNLP file download
  1. It will download JNLP file. Launch it and it will start running as the slave for the master node. If you want to run this agent as windows service, Open File -> Install as Windows service from the running JNLP program.

Testing the Master Slave Configuration

To test the master Slave configuration, Create a simple freestyle project. In the configuration page of the project, in the general-section, check the “Restrict where this project can be run”. Then the input box will open for specifying the Slave node label. Enter the created node’s label name (Created Node’s label will auto-populate).

Sample Jenkins Job with Slave Agent
Sample Jenkins Job with Slave Agent

Then put some command in build’s Execute shell section to test whether the commands are executed in the shell or not. In my case I have entered “uname -a” and “ifconfig” just to see uname and network configuration of the slave machine to test.

Test Jenkins Projects's Log file
Test Jenkins Projects’s Log file

If you get the above logs, then your configuration is correct. Else, comment here, we will discuss.

Conclusion

In this article we have discussed, what is Distributed Build in Jenkins, How Jenkins slaves are Invoked from the master, then importantly, we have seen How Jenkins Master-Slave setup is been done in different ways. Finally, we have tested the Slave Node configuration. But this is just an introduction to distributed Build in Jenkins, we will see Detailed Use case in our coming article.

Learn and master in Jenkins by reading Jenkins 2: Up and Running- Evolve Your Deployment Pipeline for Next Generation Automation Book

Stay tuned and Subscribe DigitalVarys to get immediate update on the new article and offers on learning materials on DevOps, DevSecOps, Agile and App Development.

2 thoughts on “How to Configure Jenkins Master Slave Setup.”

  1. Pingback: Dynamically Scalable Jenkins Slaves with Docker and Kubernetes - Digital Varys

  2. Pingback: Scale Up Jenkins with slave – Tech Annotation

Leave a Reply