How to configure Jenkins master and slave
In today's tutorial we will learn how to configure Jenkins master and slave. Jenkins is a continuous integration tool. It is used to build and deploy applications not only that it has powerful feature of master slave architecture which enables distributed builds.
Prerequisites
- Scheduling build jobs
- Distribute builds to the slaves for the actual execution
- Monitor the build progress
- Execute the build jobs
Install Java
SSH in to to your slave server and you need to install Java on it. To install Java, you can use following commands
1
2
3
sudo apt-get update
sudo apt-get install default-jdk -y
1 | sudo apt-get update |
Create Jenkins User
Now we need to create a user as jenkins in our slave node. You can use following commands to create a user for Jenkins
1
2
3
sudo useradd -m jenkins
sudo -u jenkins mkdir /home/jenkins/.ssh
1 | sudo useradd -m jenkins |
Copy SSH Keys from Master node
To establish our connection between Master and Slave node, we need to add SSH key of our Master node to Slave node. To copy Master node's SSH key(If you do not have keys, create using ssh-keygen command), execute the below command in Jenkins master
1
sudo cat ~/.ssh/id_rsa.pub
1 | sudo cat ~/.ssh/id_rsa.pub |
Now copy the output of the above command
Add Master node SSH key to Slave node
Now go to Slave node and execute the below command
1
sudo -u jenkins vi /home/jenkins/.ssh/authorized_keys
1 | sudo -u jenkins vi /home/jenkins/.ssh/authorized_keys |
This will be empty file, now paste the key which we were copied from our Master node into above file.
Once you pasted the public key in the above file, come out of the file by entering wq!
Test Connection between Master and Slave
To check whether we can access from our Master node to Slave node. You can go to your Master node and execute below command
1
ssh jenkins@slave_node_ip
this is to make sure master node able to connect slave node. once you are successfully logged into slave, type exit to come out of slave. Now copy the SSH keys into /var/lib/jenkins/.ssh by executing below command in Master node.
1
sudo cp ~/.ssh/known_hosts /var/lib/jenkins/.ssh
1 | ssh jenkins@slave_node_ip |
1 | sudo cp ~/.ssh/known_hosts /var/lib/jenkins/.ssh |
Register Slave node in Master node
Now to go Jenkins Master, manage jenkins->manage nodes
Click on new node
Then give name and check permanent agent
Click Ok to create the slave node
In the next screen give name and no of executors as 1
Add /home/jenkins as remote directory
Select launch method as Launch slaves nodes via SSH
Enter Slave node ip address as Host
Click on credentials
Enter user name as jenkins
Select Kind as SSH username with private key
Enter private key of master node directly by executing below command:
1 | sudo cat ~/.ssh/id_rsa |
Click Save
Select Host key verification strategy as manually trusted key verification strategy
Click Save