Setup build agent/slave VM and Integrate with Jenkins Master
Jenkins has powerful feature of master slave architecture which enables distributed builds. This article we will learn how to establish slave nodes on Ubuntu machines and integrate with Jenkins Master.
Jenkins Master
Jenkins Master uses SSH keys to communicate with slave. You need to create ssh keys in Jenkins master by executing below command.
Pre-requisites:
Your main Jenkins server is the Master. The Master’s job is to handle:
- Scheduling build jobs.
- Dispatching builds to the slaves for the actual execution.
- Monitor the slaves (possibly taking them online and offline as required).
- Recording and presenting the build results.
- A Master instance of Jenkins can also execute build jobs directly.
Jenkins Slave
A Slave is a Java executable that runs on a remote machine. Following are the characteristics of Jenkins Slaves:
- It hears requests from the Jenkins Master instance.
- Slaves can run on a variety of operating systems.
- The job of a Slave is to do as they are told to, which involves executing build jobs dispatched by the Master.
- You can configure a project to always run on a particular Slave machine, or a particular type of Slave machine, or simply let Jenkins pick the next available Slave.
Jenkins Master uses SSH keys to communicate with slave. You need to create ssh keys in Jenkins master by executing below command.
Pre-requisites:
- Jenkins Master is already setup and running
- Create a new EC2 instance for Slave
Steps involved:
1. Setup new EC2 instance for slave
2. Create jenkins user and Install Java, Maven in Slave node
3. upload public keys from master to slave node.
4. verify ssh connection from master to slave
5. Register slave node in Jenkins master
6. Run build jobs in Jenkins slave
Slave node configuration
(You need to create at least t2.small Ubuntu 22.0.4 instance for this slave)
only port 22 needs to be open
only port 22 needs to be open
Change Host Name to Slave
sudo hostnamectl set-hostname Slave
sudo hostnamectl set-hostname Slave
Install Java
sudo apt-get update
sudo apt-get install default-jdk -y
Install Maven
sudo apt-get install maven -y
Create User as Jenkins
sudo useradd -m jenkins
Add SSH Keys from Master to Slave
Execute the below command in Jenkins master EC2.
sudo cat ~/.ssh/id_rsa.pub
Copy the output of the above command:
Now Login to Slave node and execute the below command
sudo -u jenkins vi /home/jenkins/.ssh/authorized_keys
Once you pasted the public keys in the above file in Slave, come out of the file by entering :wq!
Now go into master node
ssh jenkins@slave_node_ipthis is to make sure master is able to connect slave node. once you are successfully logged into slave, type exit to come out of slave.
Register slave node in Jenkins:
Now to go Jenkins Master, manage jenkins, manage nodes.Click on new node. give name and check permanent agent.
give name and no of executors as 1. enter /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. Make jenkins as lowercase as it is shown.
Kind as SSH username with private key. enter private key of master node directly by executing below command:
sudo cat ~/.ssh/id_rsa
(Make sure you copy the whole key including the below without missing anything)
-----BEGIN RSA PRIVATE KEY-----
-----
-----END RSA PRIVATE KEY-----
click Save.
select Host key verification strategy as "manually trusted key verification strategy".
Click Save.
Click on launch agent..make sure it connects to agent node.
Now you can kick start building the jobs, you will see Jenkins master runs jobs in slave nodes.
Comments
Post a Comment