Setup Dynamic Docker Worker Agent
Problem Statement:
Solution:
Dynamically creating nodes and running automation on that solves the problem in case of Jenkins. There are multiple approaches to the same and each one of them have their own advantages and disadvantages.
Approach 1:
Approach 1.1
Approach 1.2
?Approach 1 shortcomings:
Approach 2:
Approach 2 benefits:
Approach 2 Shortcoming:
Approach 2 Implementation:
Configure Docker Host With Remote API:
Jenkins master connects to the docker host using REST APIs. So we need to enable the remote API for our docker host.
Make sure the following ports are enabled in your server firewall to accept connections from Jenkins master.
32768 to 60999?is used by Docker to assign a host port for Jenkins to connect to the container. Without this connection, the build slave would go in a pending state.
领英推荐
Lets get started,
Step 1:?Spin up a VM, and install docker on it. You can follow the official?documentation for installing docker.?based on the Linux distribution you use. Make sure the docker service is up and running.
Step 2: Log in to the server and open the docker service file?/lib/systemd/system/docker.service. Search for?ExecStart?and replace that line with the following.
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock
Step 3:?Reload and restart docker service.
sudo systemctl daemon-reload
sudo service docker restart
Step 4: Validate API by executing the following curl commands. Replace?54.221.134.7?with your host IP.
curl https://localhost:4243/version
Create Docker file:
FROM ubuntu:18.0
# Make sure the package repository is up to date.
RUN apt-get update && \
? ? apt-get -qy full-upgrade && \
? ? apt-get install -qy git && \
# Install a basic SSH server
? ? apt-get install -qy openssh-server && \
? ? sed -i 's|session? ? required? ? ?pam_loginuid.so|session? ? optional? ? ?pam_loginuid.so|g' /etc/pam.d/sshd && \
? ? mkdir -p /var/run/sshd && \
# Install JDK 8 (latest stable edition at 2019-04-01)
? ? apt-get install -qy openjdk-8-jdk && \
# Install maven
? ? apt-get install -qy maven && \
# Cleanup old packages
? ? apt-get -qy autoremove && \
# Add user jenkins to the image
? ? adduser --quiet jenkins && \
# Set password for the jenkins user (you may want to alter this).
? ? echo "jenkins:jenkins" | chpasswd && \
? ? mkdir /home/jenkins/.m2
COPY .ssh/authorized_keys /home/jenkins/.ssh/authorized_keys
RUN chown -R jenkins:jenkins /home/jenkins/.m2/ && \
? ? chown -R jenkins:jenkins /home/jenkins/.ssh/
# Standard SSH port
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]4
Prepare Jenkins to configure docker cloud and template:
Cloud configuration:
Go to Jenkins -> Manage Jenkins -> Manage Cloud -> Add cloud
Configure the agent template by providing following details
Utilize Docker cloud in Jenkins Job:
Use docker node label while configuring a jenkins job, use the same template which has been configured in the docker template while configuring the docker cloud
This to be affected and needs Testing: