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.
Table of Contents
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.
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.
- Go to the Jenkins Home Page -> Credentials ->System -> Global credentials
- Click Add Credentials -> Select username with password or SSH Username with the private key then save
- If username with password option is chosen, Simply, Username and password of “Jenkins” are enough.
- If SSH Username with Private Key is an option, then follow the steps
- Create ssh key pair by entering “
ssh-keygen -t rsa -C "Get Key”
“ - Add the key to authorized keys by entering
cat id_rsa.pub > ~/.ssh/authorized_keys
- Then Copy the private key from “
id_rsa
” file from the .ssh directory. - Then in creating new credentials page select “SSH Username with private key” kind and enter directly the copied private key and save.
- Create ssh key pair by entering “
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.
- Go to Manage Jenkins -> Manage Nodes -> New Node
- Enter the Node Name (Here, Jenkins_slave_1) there and select Permanent Agent then click OK
- Then a new form will open and enter the details one by one as follows.
- Edit the Description (Optional)
- Enter the # of Executor (One or more, In our case we are going to set up 2 Executors)
- 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)
- Select the launch Method. (In Our case, we are selecting Launch Agent via SSH)
- Enter the Host address (In our case, 172.28.128.20)
- Select the Credentials Which is created in the “Launching Agent with SSH from Master” Section of this article.
- Select “Non verifying Verification Strategy” for Host key Verification Strategy.
- Others Keep default. (Click help icon and know everything from the page itself)
- Then click Save button.
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)
- 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
). - Log in and go to Manage Jenkins -> Manage Nodes, then click the created Node.
- It will show you the Popup window which will have “Launch” Button.
- 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).
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.
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.
Certified Cloud Automation Architect and DevSecOps expert, skilled in optimizing IT workflows with Six Sigma and Value Stream Management. Proficient in both technical and leadership roles, I deliver robust solutions and lead teams to success.
Pingback: Dynamically Scalable Jenkins Slaves with Docker and Kubernetes - Digital Varys
Pingback: Scale Up Jenkins with slave – Tech Annotation