Tuesday, December 8, 2020

How to setup Jenkins slave node to run Docker Builds | Setup Jenkins Slave and Install Docker

How to configure Jenkins Slave to run Docker builds?

Create User as Jenkins
sudo useradd -m jenkins
sudo -u jenkins mkdir /home/jenkins/.ssh



Steps for installing Docker
sudo apt-get update && sudo apt install docker.io -y
 
Install Maven
sudo apt-get install maven -y
 
Add Jenkins to Docker Group
sudo usermod -aG docker jenkins
sudo newgrp docker
sudo systemctl daemon-reload
 
Restart Docker service
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl restart docker


Login to Jenkins Master and restart Jenkins service
sudo service jenkins restart
(Make sure you execute this in Jenkins Master)

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 go to Slave node and execute the below command
sudo -u jenkins vi /home/jenkins/.ssh/authorized_keys

This will be empty file, now copy the public keys from master into above file.
Once you pasted the public keys in the above file in Slave, come out of the file by entering wq!

 Login to master node and try to SSH from Master to Slave
ssh jenkins@slave_node_ip





this 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.



Now copy SSH keys into /var/lib/jenkins/.ssh folder also by executing below command in Jenkins master(make sure you exited from slave by typing exit command:

sudo cp ~/.ssh/known_hosts  /var/lib/jenkins/.ssh

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 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.

 
That's it! Jenkins Master and Slave is configured up!

6 comments:

  1. slave is another ec2 instance created on aws? I thought slave was in the same ec2 instance.

    ReplyDelete
  2. I followed the same steps as mentioned by you. But get to see the below message(in master)

    ubuntu@ip-172-31-58-13:~$ sudo service jenkins restart
    ubuntu@ip-172-31-58-13:~$ sudo cat ~/.ssh/id_rsa.pub
    cat: /home/ubuntu/.ssh/id_rsa.pub: No such file or directory
    ubuntu@ip-172-31-58-13:~$

    ReplyDelete
    Replies
    1. I googled and found that you missed to specify one command. Let me know if this is the case..

      ubuntu@ip-172-31-58-13:~$ ssh-keygen -t rsa -C "MyEmailAddress" -f ~/.ssh/id_rsa -P ""
      Generating public/private rsa key pair.
      Your identification has been saved in /home/ubuntu/.ssh/id_rsa.
      Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub.
      The key fingerprint is:
      SHA256:pUlkEw5AXcRCKunGNI5n+BSOvy+S7bX99E6T651K2Q4 MyEmailAddress
      The key's randomart image is:
      +---[RSA 2048]----+
      | .o+o+B. |
      | . .o=.. |
      | * . .o . |
      | X + . + |
      |+ X S |
      | B + |
      | oo . . E . |
      |o oo o . + * . |
      | oooo ...o*.+ |
      +----[SHA256]-----+
      ubuntu@ip-172-31-58-13:~$ sudo cat ~/.ssh/id_rsa.pub
      ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRiJyy7P7kHsHxgwW+5t9+BFMYxt995WVseE1nphQCV6BUXxXE74Y1OEuA2VP3dYGZJ33TKSS8OYH6D+AmSufwgmUZ4CkdbsUSKDeeo2SSbDu/z+eeeghmd5Ccmxx4oyx/A01Bwl4fbOumQoeTASxUbYdO0B3C1aXCF6+2sZJAJQ/2O5tOuwi3gbbAe/kJ+GkfjiHkhSC0UXg1wtjZ72XflNNGYo2bHYlFu8wpMRiYRhGGlVIXWjZEQFSR9TGF5V76d1ceJqoA/rRJfija8Wr8I2HX5VPMP62bkLWtP1y51fI+9atSH1+gUMAZERQ+U161P7eA+UEa/5j5AHt7PSjp MyEmailAddress
      ubuntu@ip-172-31-58-13:~$

      Delete
    2. yeah even i am getting same error
      how to resolve it

      Delete
  3. Hi DevOps Coach.. I am just wondering why we need additional slave in this case, we can run pipeline job from master even. Also on master , we can install docker. So could you please help to understand why slave is needed here.

    ReplyDelete

How to Setup Self-Hosted Linux Docker Build Agent in Azure DevOps | How to configure Self-Hosted Linux Docker Agents in Azure Pipelines | Create Custom Build Agents in Azure DevOps

  Let us learn how to configure a self-hosted agent using Docker in Azure DevOps pipelines. What is an Agent? An agent is computing infrastr...