DevOps & Cloud Solutions, Managed Services

For High WorkLoads : Jenkins Master-Slave Setup Step by Step Guide

In this blog we are going to explain master slave configurations of Jenkins. This will useful to manage the distributed builds.

Steps:

Updating the System

First update your system before we start installing the Jenkins. Use below commands for updating the system
#yum update(for RHEL)

Install Java

Install java if java is not installed yet n your server and verify the version of java. Below are the commands to install java.
#yum install java-1.8.0-openjdk.x86_64

Now, check Java Version using below command
#java -version

JAVA Home Path setup:

#sudo cp /etc/profile /etc/profile_backup

#echo ‘export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk’ | sudo tee -a /etc/profile

#echo ‘export JRE_HOME=/usr/lib/jvm/jre’ | sudo tee -a /etc/profile
#source /etc/profile

Check Java Home path using below command
#echo $JAVA_HOME

Installation of Jenkins

Use below commands to install Jenkins on your server

#sudo rpm –import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
#yum install Jenkins

Once you are done with the installation you can follow the initial steps like starting the Jenkins, updating initial password for admin and installing the basic plugins.

Create SSH Keys and enable login for Jenkins

In this step we are going to create the SSH Keys for the jenkins user. Edit the passwd file and change the Jenkins user details as below.

#vi /etc/passwd

Change Jenkins User Details as below

From : jenkins:x:997:994:Jenkins Automation Server:/var/lib/jenkins:/bin/false
To : jenkins:x:997:994:Jenkins Automation Server:/var/lib/jenkins:/bin/bash

Create password for jenkins user using below command and then switch to it.

#passwd jenkins
#su -jenkins

Now, create SSH keys.
# ssh-keygen -t rsa

Press Enter for all the lines. Don’t enter password.

Jenkins Slave side configurations

Create a user called Jenkins in slave. And create password for that user.
Install Java as mentioned above and git client. For GIT
# yum install git

Master-Slave configurations

Next go to the master server do the following

# su – jenkins
# cd .ssh

Copy the Public Key to Jenkins Slave
# ssh-copy-id jenkins@<Jenkins Slave IP>

Output as follows

-bash-4.2$ ssh-copy-id jenkins@<Slave IP>
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/var/lib/jenkins/.ssh/id_rsa.pub”
The authenticity of host ‘ip-170-00-00-186 (170.00.00.186)’ can’t be established.
ECDSA key fingerprint is SHA256:2Y8D6nN2dIe6opWGqIESWMQzLULbbucAkjP4Ar7LCB4.
ECDSA key fingerprint is MD5:04:0c:98:aa:a6:b2:77:46:d0:b9:ba:2c:16:a1:b5:f9.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed — if you are prompted now it is to install the new keys
jenkins@ip-170-00-00-186’s password:

Number of key(s) added: 1

 Now try logging into the machine, with:   “ssh ‘jenkins@<Slave IP>” and check to make sure that only the key(s) you wanted were added.

Now, Login to the jenkins Master server with Admin Credentials and go to Manage Jenkins -> Manage node:

Click on New Node in Left panel
then,  give slave name and click on Permanent Agent click on Ok

In the ‘remote root directory’, add the path Jenkins User path (/home/Jenkins or any directory where Jenkins user have access).
In launch method select option ‘launch slave agents via ssh’ and add the slave machine’s ip address for credentials copy the id_rsa (Private key) from Master Server and update as showed in screenshot.

Click on Save once you are done with the above process.

For Credential Configuration select Kind as  “SSH Username with private key “  Enter User Name as “Jenkins” on Private key  select  Enter directly Copy the id_rsa (Private key from Jenkins Master) and paste in the text area click on Add and select Add. Give label name as well

Note: If you have Any problem with SSH Keys you can use User Name password option and give Jenkins slave server username and password

If configuration success it will show you in sync. As showed in the screenshot.


Master will copy Slave.jar file in Remote root directory in above-mentioned screenshot. And checks the java process.

Run Jobs in Jenkins Slave

To run jobs in slave go to job in Jenkins and select the “Restrict where this job can be run” and enter slave Label and save the job. Whenever you click on Build that will trigger in Jenkins slave. you can  confirm this in Console Output.

 

About The Author

Leave a Reply

*