This page looks best with JavaScript enabled

How to add Docker as node in Jenkins

 ·  β˜• 4 min read  ·  🐧 sysadmin

Tutorial:

  1. Create a jenkins user on a Docker node
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
sudo useradd -d /var/lib/jenkins jenkins
sudo passwd jenkins
sudo mkdir /var/lib/jenkins/.ssh
sudo touch /var/lib/jenkins/.ssh/authorized_keys
sudo chmod 600 /var/lib/jenkins/.ssh/authorized_keys
sudo chmod 700 /var/lib/jenkins/.ssh
cd /var/lib/
sudo chown -R jenkins:jenkins jenkins
cd jenkins
ls -alh

Change shell to Bash for jenkins user

1
sudo vim /etc/passwd

Replace sh to bash for jenkins user. Save and exit

Login as jenkins user.

1
sudo su - jenkins
  1. Install Java 11 open JDK on a Docker node
SLES | openSUSE Leap 15.4
1
sudo zypper install java-11-openjdk
Debian
1
sudo apt install openjdk-11-jdk
Red Hat
1
sudo dnf install java-11-openjdk
  1. Check Java version
1
java -version
  1. Install Git on a Docker node
SLES | openSUSE Leap 15.4
1
sudo zypper install git
Debian
1
sudo apt install git
Red Hat
1
sudo dnf install git
  1. Generate RSA key for Docker node on a Jenkins server

Log to a master Linux server where Jenkins is installed. and switch to a jenkins user.

1
sudo su - jenkins

Now you need to generate RSA keys for a Docker node. Do not forget to set a passphrase. You will add it later in Jenkins node management panel.

1
ssh-keygen -t rsa -C "The access key for Docker node" -f /var/lib/jenkins/.ssh/id_rsa_docker_node
  1. Copy the RSA public key from Jenkins server to Docker node. Use the IP address of the Docker node.
1
ssh-copy-id -i id_rsa_docker_node.pub jenkins@10.10.0.121
  1. Disable the password login, empty passwords and root login in /etc/ssh/sshd_config on Jenkins server and Docker node.
1
2
3
4
5
6
7
8
# Deny access for root via ssh
sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
# Deny password authentication via ssh
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
# Deny using empty passwords
sed -i 's/#PermitEmptyPasswords no/PermitEmptyPasswords no/g' /etc/ssh/sshd_config
# Enable public key authentication
sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config

Restart ssh daemon

1
systemctl restart sshd.service
  1. Check can you login using private RSA key after disabling password login.
1
ssh -i id_rsa_docker_node jenkins@10.10.0.121
  1. Add a Linux node to a master Linux server with Jenkins
  • Login to Jenkins web panel: http://10.10.0.113:8080
  • Then click Manage Jenkins β†’ Manage nodes and clouds
  • Click + New Node on the left side panel.
  • Provide a node name
  • Select permanent agent
  • Set the description the same as node name
  • Set number of executors to 1 (it can be increased later)
  • Set remote root directory to /var/lib/jenkins
  • Set the label docker
  • Usage: use this node as much as possible
  • Launch method: Launch agents via SSH
  • Host: provide the IP address of the Docker node
  • Credentials β†’ add β†’ select Jenkins
  • Kind - choose from the dropdown list SSH username with private key
  • Provide a username: jenkins
  • Select enter directly
  • Paste the private key copied from the id_rsa_docker_node on a master Linux server with Jenkins (see the part: Generate RSA key for Docker node on a Jenkins server)
  • In the Description field provide a friendly name like RSA key for jenkins DOcker node or anything like that that will easily identify the credentials.
  • Provide a passphrase to this RSA private key you generated previously on a master Linux server.
  • Click add
  • Select newly created credentials from a dropdown list
  • Host Key Verification Strategy: choose: Known hosts file Verification Strategy
  • Availability: Keep this agent online as much as possible
  • In node properties select/check Environment variables and Tools Locations
  • In Environment variables section add:
  • Name: JAVA_HOME
  • Value: /usr/bin/java
  • In Tools Locations section add:
  • Name: Git (default)
  • Value: /usr/bin/git
  • Click save
  1. Set the treshold for free space on a node
  2. Extend var logical volume
1
2
3
sudo lvextend -L +2G /dev/mapper/docker--vg-var
sudo resize2fs /dev/mapper/docker--vg-var
df -kTh /var
  1. Cleanup of the /var/tmp directory - explanation with examples
1
2
3
sudo find /var/tmp -type -f -mtime -1 -exec rm {} \;
sudo find /var/tmp -type -f -mtime -1 -delete
sudo find /var/tmp -type -f -mtime -1 | xargs rm
Support the author with

sysadmin
WRITTEN BY
sysadmin
QA & Linux Specialist