Setup Docker container as Build Slaves for Jenkins for a Dynamic Provisioning
Introduction
Today we will continue our journey to explore the fantastic world of DevOps with its automation tools. I would never stop tell you that in Production world, developers and engineers work on different projects, so they need various environment to accomplish their tasks in better and quicker record time.Therefore platform like Docker, kubernetes,… would help them to containerize and deploy their apps quicker and save money in case of resources.
In my previous write-up i show you how to create your own docker images with components centos, maven, openjdk8…And today i will walk you through a step by step how we’ll use this image to integrate a Master node Jenkins with a docker engine to launch some slave containers.
Environment
For this demo, i will use:
- one Guest Os of Rhel8 from Virtualbox which contains the Jenkins Master node inside.
- And my local machine Ubuntu as a Docker engine server to provide containers for the Jenkins.
My Plan
So what is my plan?? i will submit a Job to Master node and as i don’t want this job to be executed in Master node ,therefore the Master node will look for Docker in a cloud (means i have to establish a connection between the Docker engine and the Master ) which will launch a container to run the jobs submitted in the Master node and as soon as the jobs terminated the container is removed from the Master ( Case of a Dynamic provisioning). And then after launch a maven app from github to test also our Docker container.
Below steps will show you how to achieve this plan.
Step 1: Start the Jenkins service and login it
I had already made a write-up on how to install Jenkins on a machine go to see it at: https://tchamieedmond.medium.com/create-a-quick-distributed-multi-nodes-cluster-with-jenkins-using-a-master-node-in-rhel8-os-and-3-fe62caf2b340?source=your_stories_page------------------------------------- or you can use Official Doc from https://www.jenkins.io/download/
Step 2: Install Docker Plugin in the Master node
Head over to Jenkins Dashboard –> Manage Jenkins –> Manage Plugins. And install docker plugin then after restart the jenkins service
Step 3: Start Docker engine service on my local Machine
I have already docker installed on my machine and i consider you too also otherwise i refer you to https://docs.docker.com/engine/install/ubuntu/
Step 4 : Enable tcp connection on Docker engine
For that you have to go Docker service file and bind the tcp port connection. From below screenshot the config file of service is:
Modify the service file by binding the ip address of your local machine with port 2376 like:
# sudo vim /lib/systemd/system/docker.service
# systemcl daemon-reload => restart the daemon
# systemctl restart docker.service => restart the docker service as we have modified the config file
Step 5: Return to Master node to manage nodes by configuring Cloud
I want the Master to go another machine running docker engine and launch a container so for that we have to configure option cloud in master like:
Head over to Jenkins Dashboard –> Nodes –> Configure Clouds.
- Choose “Name” as : “Docker” => as we want to integrate Docker engine
- In “Docker Host URL”: => type the ip of the Machine running the Docker engine server and port of Docker engine which you define in the Config file of this service
- Now click “Test connection” =>shows connection refused so you have to disable firewall on the machine running Docker engine server:
In my case it’s ubuntu so i will deactivate firewalld service like: sudo service ufw stop
Now After disable firewall click again “Test connection” and you will see the version of your Docker engine showing in Jenkins.
Now Click on “Docker Agent templates” and add the image from https://hub.docker.com/repository/docker/tcdocker2021/jenkins-slave-maven-jdk
So after add a label name and name for the container which will be used by Jenkins like the image below :
After save it also in Configure Clouds options
Step 6: Launch a freestyle project job to test our setup
Before launch the job, you can see we don’t yet have a docker container node available in our nodes
- Start a new project like
- Next page in “Restrict where this project can be run ” choose the name you define as label when configure the “Configure Cloud” previously
- Now After click save, you will be on the below page and and you will see that the job not yet started . So click on “Build now” to start the job:
- After click build now, below you can see the job is in a pending state
- After some minutes, as the node doesn’t exist on master, so Master has to contact docker engine in another machine (my local ubuntu machine) and then launch a container. After job terminated the container is removed and know that this process is very quick.
- As is a dynamic provisioning, the process is very quick, below you can see while job running, the slave node is created from Docker engine by our master
- After job ran, you can check the “Console Output” and see Master has created a node at the node name given in my “Configure Cloud” options
- Click on “Built on Docker” for the confirmation, a container was launched and removed asap
And that’s how we can make a dynamic provisioning in Jenkins with a Docker Engine as integration.
Step 7: Let’s create a package and a test with our job from a Github maven app and our docker image
- Now with our previous job , will modify it by choosing “git” as “Source Code Management”.
- The github link choosen: https://github.com/Tcarters/simple-java-maven-app.git
- And under “Build”, will test our maven app from github by test it before and then create a package for this app
- Now After click save, you will be on the below page and and you will see that the job not yet started . So click on “Build now” to start the job:
- After click “Build Now”, below you can see the job is in a pending state
- After some minutes, go to “under nodes” , while the job is running you will see again a Docker node is created
- If you go quickly in your local machine running the Docker engine you will see a docker container launched successfully
- Check the Console Output, we can see our jobs ran with success
- And also under Workspace, we have a package created successfully
CONCLUSION
Congrats we made it. With this post you see effectively the power of a Dynamic provisioning with Jenkins along Docker, and know that’s one of show case of how we can integrate containerization with an automation tool to deploy an app in an environment.
Thank you and see you in another fun story where we can discover something new in DevOps world 👻 !!!