Still now we have setup the Jenkins and executed some sample now. This helps only building/running job only on master server. If we have parallel build or multiple jobs needs to be perform single server won’t help much, either we may need more hardware or additional server. For that Jenkins is giving master-slave feature, which helps to run then build or jobs on multiple servers.
In this post we should like to demonstrate how to create a master-slave environment in Jenkins. Jenkins makes it possible to configure multiple nodes as slave.
Refer Our Jenkins Articles on Jenkins Page
To achieve this with following easy steps,
- Setup Jenkins Master
- Installing Slave Linux and Windows
- Adding new slave node on Jenkins master
Step1: Setup Jenkins Master
You can install Jenkins on any machine windows/linux. In our case we would like to take CentOS 7, For Ubuntu follow Jenkins Installation
Install Jenkins in Redhat/centos 7
# wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo # rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
Install jenkins
# yum install jenkins
Install java
# yum install java-1.8.0-openjdk
Setup firewall
# firewall-cmd –zone=public –add-port=8080/tcp –permanent # firewall-cmd –reload
Start Jenkins
# systemctl restart jenkins.service
Step2: Installing Slave Linux and Windows
Install java using following command,
# yum install java -y
Add Jenkins user to authenticate slave server, you can any user name.
# useradd Jenkins # passwd Jenkins
Step3: Adding new slave node on Jenkins master
Linux
To add slave to Jenkins, goto Jenkins dashboard à Manage Jenkins à manage nodes. Click on New Node on left side panel, it routes to following page,
When we add node for the first time we get only single option “Permanent Agent”, but after adding a slave then you will get another option. This option will allow you copying the node from another node which is named as “Copy Existing Node”
Set a number of executors
- (one or more) as needed.
- Set a Remote FS Root
- A home directory for the master on the agent machine.
Note: For a Windows agent, use something like: “C:\Jenkins\”
- Select the appropriate Usage setting:
- For an additional worker: Utilize this node as much as possible
- For specialized jobs: Leave this machine for tied jobs only
- Launch Method:
- TODO: add steps for other methods.
Note: 1. An easy way to control a Windows agent is by using Launch agent via Java Web Start (Recommended for Windows)
- Availability
- Keep this agent online as much as possible
- TODO: add details for each option.
In the last, click save and then okay. It’ll take few minutes to connect and bring the slave node online. If its not online after some time, just check the logs to find the exact error. To get the log, click on the slave machine name and then click logs.
Windows as Slave
To add slave to Jenkins, goto Jenkins dashboard a Manage Jenkins a manage nodes. Click on New Node on left side panel, it routes to following page, (Same as linux host)
Enter node name and select permanent agent.
When we add node for the first time we get only single option “Permanent Agent”, but after adding a slave then you will get another option. This option will allow you copying the node from another node which is named as “Copy Existing Node”
Now you need to connect your agent machine to the master using the following steps.
- Open a browser on the agent machine and go to the Jenkins master server url (http://yourjenkinsmaster:8080).
- Go to Manage Jenkins > Manage Nodes,
- Click on the newly created agent machine. You will need to login as someone that has the “Connect” Agent permission if you have configured global security.
- Click on the Launch button to launch agent from browser on agent.
- Follow anyone of the command to install the agent package.
- Run the agent command line
Javews http://<<jenkins-server-IP/domain:Jenkins slave/slave-agent.jnlp
Or If the slave headless
Java -jar agent.jar http://<<jenkins-server-IP/domain:Jenkins slave/slave-agent.jnlp
- Now the agent should be online and connected.
Docker as Slave
Although you can add VM as build slave in Jenkins, it’s more flexible and convenient to make build slave as docker container, because you don’t need to maintain each slave VM, you can just give Jenkins a slave host’s IP and slave’s docker image template, then Jenkins create slave as docker container on that host. Jenkins make this happen by a plugin called Docker-Plugin (Yet Another Docker Plugin).
Let’s see how to install and configure this plugin.
Install the plugin
Navigate to the Manage Jenkins > Manage Plugins page in the web UI. Find Docker-Plugin and install it.
Plugin Configuration
Step1: On docker host, to expose docker’s unix port so Docker-Plugin can access docker host and create build slave container, edit /etc/default/docker to modify the value of
DOCKER_DOCKER_OPTS="-H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock"
Step2: Now we can return to Jenkins Web UI and configure Jenkins:
Manage Jenkins > Configure system > Cloud > docker:
If click “Test Connection”, you can see docker API version shown on UI.
2) In “Docker Template” section, click “Container settings”, input Volumes:
/var/run/docker.sock:/var/run/docker.sock /home/jenkins/workspace
The volumes are volume mappings that Docker-Plugin will use to create slave containers. The first volume mapping mounts docker socket into the container to enable docker command can be listened by docker daemon on the host, the second line let Jenkins find her workspace to execute the job.
3) Docker-Plugin by default will destroy build slave container after each build, you can save your slave container for debugging purpose or deploy purpose. On “Container settings”, from “Availability” label, click “Experimental options”, choose “Experimental: Keep this agent online as much as possible”.
Now the docker slave configuration is complete. The whole configuration can be saved by committing the Jenkins image. To verify, you can create a job to run. Your Job configuration can be as below screenshots if you want a pipeline job to build from a repository.
When the job is running, you can see the created slave containers from “Build Executor Status”, docker-xyz is a slave container’s name. After the job finished building, the container status will be “Idle”, if you destroy the container on your slave host manually, the status will be “offline”.