jenkins-slave-poster

Setup Jenkins Slave on Amazon Linux EC2 instance (AWS)

Jenkins is one of the best tool used in IT industry to automate tasks such as building, testing, and deploying software. On cloud majority of startups and well established companies prefer to have separated builds, this means they separate the software process in terms of different environments/stages.

If a company runs its software development process in a distributed environment, than it needs different/separate machines for build, test and deployment tasks. An ideal fit for this type of architecture is Jenkins Master Slave deployment. Where you have a single master node under which there are n number of slave nodes performing different operations.


Jenkins slave are generally required to provide desired environment, it works on basis of request recieved from Jenkins master. This article is a step by step guide to set up Jenkins Slave on Amazon Linux EC2 instance.

Before we start ensure that you’ve have all pre-requistes in place.

Pre-requistes : 

  • Amazon linux instance with Jenkins installation. This instance will be your Jenkins master
  • Another amazon linux instance which will be your slave.
  • Make sure you’ve port 8080 opened on both master and slave instance.
  • On slave instance create a user named jenkins with sudo priviledges

Step 1. Create a Jenkins Slave node

Since you would have already instance Jenkins on Master instance you can directly move to this setup. If your haven’t you can refer to this document for installation. http://bhargavamin.com/how-to-do/install-jenkins-on-amazon-linux-aws/

On your master machine go to Manage Jenkins > Manage Nodes > New Node

manage-node-slave-jenkins

Next enter Node name and click ok

setup-jenkins-slave-node

 

Step 2. Configure Node

Here you will fill all the details about the node as follows : 

configure-jenkins-slave-node

 

Note :  if you are not able to see Launch Method : Launch Agent via Java Web Start then you need to enable TCP port for JNLP agents.

Go to Manage Jenkins > Configure Global Security

There enable the TCP port for JNLP agents.

setup-jnlp-agent

 

Step 3. Setup Jenkins Slave

Once you’ve saved the node configuration in above setup it will show you a page with a link and command which you will have to execute it on Slave EC2 instance.

connecting-agent-final

Below steps to be performed on Slave EC2 instance :

Download slave.jar on the /opt/jenkins path.

If you dont have jenkins directory in /opt than create one.

You can get the link to slave.jar by right click on it

save-jenkins-slave-jar

 

You can download slave.jar on EC2 instance by following command :

sudo wget <copied link>

install-slave-jar

Now that you’ve have download slave.jar on /opt/jenkins directory. It time to connect same EC2 instance to Master Jenkins.

Copy the command you found on Master Jenkins > Slave node Page

 copy-slave-jenkins-command

Copy the command shown in above image and execute it on Slave instance at /opt/jenkins location.

Note : Before executing the command switch the user to jenkins and then execute it on Slave instance.

jenkins-slave-connection-succeeded

The output should be connected. This means Slave instance has successfully connected to Master Jenkins via JNLP agent.

jenkins-slave-connected

If the connection had failed then you should check your security group and open port 8080 with Slave instance ip on Master Jenkins. Also make sure the IP of Master Jenkins is correct in the command.

Inorder to ensure that the agent remains connected then you need to run above mentioned command via jenkins user and with nohup utility which will run this process in backgroud.

nohup java -jar slave.jar -jnlpUrl http://52.1.11.111:8080/computer/Jenkins-slave/slave-agent.jnlp -secret 4a36971c88afff8c142077b74f6797ab7b812816831293020c04cb275ed06dae &

Now you’ve successfully setup a Jenkins Slave which will be up all the time no matter what and where all the task will be executed which were triggered on Master Jenkins server.

jenkins-slave-connection-status

This concludes this article, if you’ve any doubts or queries feel free to comment below.

-Bhargav



  • Gautam Kumar

    How to configure JNLP slave if my master is behind ELB ?

    • Bhargav Amin

      Easy, just go to Configure Global Security on master jenkins > Set TCP port for JNLP agents option to a “Fixed port”.

      Once you have set a static port for JNLP agents to talk to on master, go to load balancer > add a listener which will listen to any port of you choice publically but will forward traffic from same port to the JSNLP agent port on instance.

      For eg : If JNLP port is 9010, listener should be something like this any port publically listening lets assume 8081 which will forward to 9010 port of the instance sitting behind load balancer.

      This should fullfill your requirement.

      Note: Add a security group rule for instance and load balancer