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

  • Jenkins up and running
  • You need a Jenkins server up and running. This will be our master server. If you haven't Jenkins server, you can follow this tutorial and set up your Jenkins Master. This will be the server that will be used to
    1. Scheduling build jobs
    2. Distribute builds to the slaves for the actual execution
    3. Monitor the build progress
  • EC2 Instance
  • You need an EC2 instance to configure the slave. We need need to allow access for port 22 on that server for ssh purposes. This slave node can run on a variety of operating systems like Windows, Linux, MacOS, etc. This will be the server that will be used to
    1. 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

    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

    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

    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

    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

    Register Slave node in Master node

    Now to go Jenkins Master, manage jenkins->manage nodes alt text

    Click on new node

    Then give name and check permanent agent

    alt text

    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

    alt text

    Click Save

    alt text

    Conclusion

    In this tutorial, we learned how to configure Jenkins Master Slave architecture. If you have any issue regarding this tutorial, mention your issue in comment section or reach me through my E-mail.

    Happy Coding