How to set up Jenkins Slave node on windows for linux aws ec2 instance – Part 2

Part 2: Setting up Jenkins Slave node on host machine

Part 1: Setup AWS EC2 instance to run jenkins slave agent

Why Slave Node

As our Jenkins server is installed on a Windows machine, we would require a different agent to run a job on different OS which is linux in our case and on a remote machine i.e. aws ec2 instance in our case. This agent or node has to be created in addition to the existing master node and is called a slave node.
In this post, we will setup a new slave node and then create a job in Jenkins which uses this slave node so that the job can be run on ec2 instance. Let’s get started.
Go to Jenkins -> Manage Jenkins -> Manage Nodes

OR

Go to Jenkins -> BuildExecutorStatus


Both these options will take you to the Manage Nodes screen


Then select “New Node” option from left bar as shown below


Provide “Node Name” and select “Permanent Agent” option. We will provide name as “TestSlaveNode” for our example. Clicking on OK button will take you to the screen where you can configure this node.

Description: Provide the scenario for using the slave node
# of executors: Set it to the default value of 1. This value signifies the maximum number of concurrent builds that Jenkins may perform on this node.
Remote root directory: This is the Jenkins directory on our aws machine. You can go to the remote machine and create a folder “Jenkins” in that machine. You can copy the path from there and then mention it here.
Labels: This could be more useful if you have multiple slaves with some similarity. If you want to run a job on multiple Linux machines using 2 slave nodes, you can keep the same label for both the nodes. The node which will be available out of these will be used to run the job. We will leave it blank for our example.
Usage: Here you need to select the option to control the build schedule on this node. You can select option “Only build files with label expressions matching this node” if labels are being used to run a job on multiple machines. We will use the option “Use this node as much as possible” and will select this node while configuring the job to be run on this node.
Launch method: We are going to ssh on aws machine from this node, hence we will select “Launch agent agents via ssh”
Host: Here we need to provide the host name or ip of the host machine.
Credentials: Select the authentication method that you find suitable as per your project requirement. We will use “Key based authentication in host machine”. For this go to  Add-> Jenkins. This will open the screen where you can add credentials. Select the value for “Kind” as “SSH username with private key” and add the details including the private key. You can also use username/password or aws credentials as per your need.
Host Key Verification Strategy: You can select one of the verification strategy based on your project requirement. For this project we will select “Non Verification Strategy”
Advanced:  In this section, you can select the SSH port if you are not using the default port or set the paths if required. We will not need to change anything here, hence we will leave all values as default.
Availability: For our example we will select “Keep this agent online as much as possible”. You can select any of the other options if you have a requirement to bring up the agent only at scheduled time or when there are queued jobs to be run on this node.
Node Properties: Here you can set environment variables or location of tools used in your project. For this example we will not require them.

Setting up a job to run on this agent

In order to create a new job,

Go to Jenkins -> New Item

In the screen that opens, enter a unique name for the Job and select the project type. We will select freestyle project here and provide the job name as “ftestslave”.

In the next screen that opens, you can configure this job. 

Provide description for the job under “Description” section.
Then select the checkbox which says “Restrict where this project can be run”.
Here you can mention the label  for the node if provided,else  you can mention the name of the node.

Keep the other values as default. Under Build section, go to “Add Build Step” and then select “Execute Shell” from the list.

Write following command:

echo this job is running on TestSlaveNode
Click on “Apply”. If there are no errors, then click on “Save” and that will take you to Jenkins home screen.

Starting the Slave Node

From the Jenkins home screen and from BuildExecutorStatus section, select the slave node TestSlaveNode. Before starting the node make sure the aws machine on which the slave node will run has been started and it’s host name is updated on the slave. Click on “Launch Agent”. To check the status of slave machine, go to “Log” from left side menu bar. The connection should be successfully established. The last message on slave node will look something like below

Starting the Job

Once he node is launched, go to Jenkins job by selecting Jenkins-> ftestslave. Select “Build Now” from left side bar. The new job should get started on the ftestslave node.


!screenshot
Go to the job screen by clicking on job number link. Then select “Console Output” from side bar. Here you can see the detailed status and if the job  has run successfully, it’ll echo the message we had written in SSH command.

Let us know if you found this post useful.   

Leave a Reply

Your email address will not be published. Required fields are marked *