Setup Dynamic Docker Slave(Agent) and Integrate with Jenkins Master
- 4 minsIntroduction
Jenkins offers a robust master-slave architecture that allows for distributed builds, making it highly flexible. In this guide, we’ll explore how to configure slave nodes using Docker and connect them to the Jenkins Master.
Benefits of Using Docker Containers as Jenkins Build Agents:
- Temporary (Ephemeral): Agents can be easily spun up and destroyed as needed.
- Optimized Resource Use: Docker containers help in utilizing system resources more efficiently.
- Customizable Agents: Different builds, such as Java 8 or Java 11, can run on tailored agents.
- Scalability: It allows for easy scaling of the build infrastructure.
Prerequisites
1- Two Ubuntu virtual machines (VMs) are required: one for Jenkins Master and another for the Docker Host.
2- Jenkins Master is already installed and operational.
- Port 8080 is open in the firewall If You have Firewall enabled.
3- Set up the Docker Host.
- Port 4243 is open on the Docker Host machine, If You have Firewall enabled.
- Ports 32768 to 60999 are also open on the Docker Host machine, If You have Firewall enabled.
Step 1 - Set Up Docker Host with Remote API
Access the Docker host machine and edit the Docker service configuration file. Look for the ExecStart line and replace it with the updated version.
sudo vi /lib/systemd/system/docker.service
Update the ExecStart line to the following:
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock
Restart the Docker service by running the following commands:
sudo systemctl daemon-reload
sudo service docker restart
Verify that the API is correctly set up by executing this curl command:
curl http://localhost:4243/version
Step 2 - Create Jenkins Slave Docker Image
1- Clone the public repository for Jekinds Docker Slave image bulding. The repository forked from another project. We need to clone the code in Docker Host VM. We will build image in Docker Host VM and We will no need to pull image from public registery.
Clone the repository containing the Dockerfile using the command below:
git clone https://github.com/guneycansanli/jenkins-docker-slave
cd jenkins-docker-slave
Build the Docker image for the Jenkins slave:
sudo docker build -t guney-jenkins-slave .
To view the list of Docker images available on the host, run:
sudo docker images
Step 3 - Set Up Jenkins Server with Docker Plugin
Log in to the Jenkins Master and ensure the Docker plugin is installed.
- Navigate to:
- Manage Jenkins -> Plugins -> Available Plugins -> Seatch Docker and install.
- Check installed plugins and make sure Docker Plugin is enabled.
Configuraiton of Docker Slave , Cloud Node
- Navigate to:
- Manage Jenkins -> Configure Nodes and Clouds
- Add new cloud.
- Select Docker and save.
- Enter the Docker host’s DNS name or IP address, I will use IP since I have no DNS set-up:
tcp://docker_host_dns-or-IP:4243
- Ensure that Enabled is selected.
- You can test connection to click test connection.
Step 4 - Set Up Docker Agent Templates
- Navigate to Docker Agent Templates.
- Enter a label, such as “docker-slave”, and provide a name for the template.
- Ensure Enabled is selected.
- Specify the name of the Docker image you built earlier on the Docker host.
- Set the Remote file system root to:
/home/jenkins
- Choose Connect with SSH as the connection method.
- Enter the SSH credentials according to your Dockerfile:
Username: jenkins Password: password
Adding New SSH Creds:
- Select Never Pull as the pull strategy since the image is already present on the Docker Host.
- Click Save to apply the changes.
Step 5 - Create a Build Job in Jenkins
- Create a Pipeline Job
- In Jenkins, create a new pipeline job and use the following pipeline script:
pipeline {
agent {
label "docker-slave"
}
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
-
Click Apply and Save the job.
- Run the Job:
- Once you build the job, the output will display a message similar to:
- Once your build completed , You can check Docker containers in the dokcer host machine and You will see there is no container running because It was a container for only build purposes and It is terminated by Jenkins after build/pipeline completed.
Thanks for reading…
Guneycan Sanli.