How to Setup Slaves on Jenkins 2.0 Using Password and ssh Keys

One of the best features of Jenkins is its distributed nature. You can configure multiple build slaves to for better segregation and scalability. For example, you might want to test a cross-platform code base with different operating system. In this case, you can configure different OS slaves and run the job against it. Also, a distributed architecture will reduce the load on the master server.

Setup Slaves on Jenkins 2.0

In this guide, I will walk you through the steps for setting up the slaves.

There are two ways for setting up the slaves.

1. Using username and password

2. Using ssh keys.

Slave Prerequisites

You need to do the following in the slave machines before adding it to the master.

Note: I am using Ubuntu machine as a slave.

1. Create a jenkins user and a password using the following command.

Type a password when prompted. The above commands should create a user and a home directory named jenkins under “/home”.

2. Now, login as jenkins user.

3. Create a “jenkins_slave” directory under /home/jenkins.

Setting up slaves using username and password

1. Head over to Jenkins dashboard –> Manage Jenkins –> Manage Nodes

2. Select new node option.

jenkins 2.0 node config

3. Give it a name, select the “permanent agent” option and click ok.

jenkins 2.0 add node

4. Enter the details as shown in the image below and save it. For credential box, click the add button and enter the slaves jenkins username and password (For logging into the slave machine) that you created during the slave configuration. To know what each option means, click the question mark at the right side of each text box.

READ  List of DevOps Blogs and Resources for Learning

jenkins 2.0 node credentials

4. Once you click save, Jenkins will automatically connect to the slave machine and will configure an agent.

Like this, you can add multiple nodes as slaves.

Setting up slaves using ssh keys

1. Login to the slave server as a jenkins user.

2. Create a .ssh directory and cd into the directory.

3. Create an ssh key pair using the following command. Press enter for all the defaults when prompted.

4. Add the public to authorized_keys file using the following command.

5. Now, copy the contents of the private key to the clipboard.

Add the private key to jenkins credential list

1. Go to jenkins dashboard –> credentials –> Global credentials –> add credentials , select and enter all the credentials as shown below and click ok.

jenkins 2.0 ssh credentials

Setup slaves from Jenkins master

1. For this, follow the first 3 steps we did for slave configuration using username and password.

2. Follow all the configuration in the 4th steps. But this time, for launch method, select the credential you created with the ssh key.

Test the slaves

To test the slave, create a sample project and select the option as shown below. You need to select the node using the label option. If you start to type the letter the node list will show up.

READ  How To Setup an Elasticsearch Cluster - Beginners Guide

jenkins 2.0 selecting slaves

 

Conclusion

In this tutorial, we learnt how to  setup slaves on Jenkins 2. Ther is no complex step involved in this configuration. When it comes to automating the process, you will need scripts and jenkins CLI to this. I will cover that in future posts.

Share if you like it and leave a comment if you have queries!!

Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.

10 comments

  1. Raghuram Pulijala Reply

    Thanks Bibin, for the clean steps… got my setup working..

  2. Sachin DV Reply

    Hey bibin,thanks for the blog.
    How to run a shell script in slave jenkins that configured in jenkins master or developers PC?

  3. Sachin DV Reply

    Under execute shell i had given shell script path and file name eg:” sh /home/sachin/Myscripts/shellll.sh”.Will this transferred to slave pc and run?.In my case it wont

  4. Joe Reply

    Hello,

    Do you have to have Jenkins installed on the slave servers for this connection via SSH to work? I tried without the Jenkins install on the slaves and it did not work. My guess is that it should work because this is a system connection problem and Jenkins really doesn’t assist in any kind of way with this process. You should just need it on the master.

  5. teo Reply

    I tried to connect via “Setting Up Slaves Using Ssh Keys” I got below error log and Node still offline. Do you have any idea for this ?

    [06/05/17 16:48:03] [SSH] Opening SSH connection to 191.11.11.111:22.
    Key exchange was not finished, connection is closed.
    java.io.IOException: There was a problem while connecting to 191.11.11.111:22
    at com.trilead.ssh2.Connection.connect(Connection.java:834)
    at com.trilead.ssh2.Connection.connect(Connection.java:703)
    at com.trilead.ssh2.Connection.connect(Connection.java:617)
    at hudson.plugins.sshslaves.SSHLauncher.openConnection(SSHLauncher.java:1265)
    at hudson.plugins.sshslaves.SSHLauncher$2.call(SSHLauncher.java:790)
    at hudson.plugins.sshslaves.SSHLauncher$2.call(SSHLauncher.java:785)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.io.IOException: Key exchange was not finished, connection is closed.
    at com.trilead.ssh2.transport.KexManager.getOrWaitForConnectionInfo(KexManager.java:95)
    at com.trilead.ssh2.transport.TransportManager.getConnectionInfo(TransportManager.java:237)
    at com.trilead.ssh2.Connection.connect(Connection.java:786)
    … 9 more
    Caused by: java.io.IOException: Cannot read full block, EOF reached.
    at com.trilead.ssh2.crypto.cipher.CipherInputStream.getBlock(CipherInputStream.java:81)
    at com.trilead.ssh2.crypto.cipher.CipherInputStream.read(CipherInputStream.java:108)
    at com.trilead.ssh2.transport.TransportConnection.receiveMessage(TransportConnection.java:232)
    at com.trilead.ssh2.transport.TransportManager.receiveLoop(TransportManager.java:706)
    at com.trilead.ssh2.transport.TransportManager$1.run(TransportManager.java:502)
    … 1 more
    [06/05/17 16:48:03] Launch failed – cleaning up connection
    [06/05/17 16:48:03] [SSH] Connection closed.

    Thanks

  6. Yashwanth Reply

    Nice blog !! Is there any way to configure the emails from the slave node? I meant , if I have jobs running in the slave node with editable email plugin. Whenever, I trigger the email it should be sending emails uopn slave email config but not Master.

Leave a Reply

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