Simplify Jenkins Slave Agents Setup in OpenShift

Dale Bingham

I want to share how I am using Config Maps in my OpenShift deployment templates to quickly and easily add Jenkins Slave Agents for your Jenkinsfile Pipeline. My sample project I have out there for the PeopleAPI uses them along with the SonarQube Jenkins Slave image here to spawn off Jenkins Slaves within my OpenShift cluster to do the pipeline builds, scans, deployments, and such. Doing this by hand is a horrible developer experience (DX) so please learn this and automate for your Jenkins pipeline if that is what you use.

Example Config Map for a .NET Core 2.1 Jenkins Slave Pod Template

Jenkins Slave Pods in OpenShift

The Jenkins applications you can setup within OpenShift have a specific Jenkins image included within the openshift namespace that each project can use (Red Hat documentation). You can specify a pipeline Jenkinsfile to automatically add it. You could really specify a specific Deployment Configuration to deploy it as well. My PeopleAPI repo above just specified a Jenkinsfile and it was included. The vanilla, generic one of course. And my pipeline uses Jenkins Slaves to perform jobs defined in my very simple Jenkinsfile pipeline.

You will note in the Jenkinsfile linked that the agent { label ‘sonar-dotnet’ } and agent { label ‘dotnetcore21’ } specific entries. This tells Jenkins to spin up Jenkins Slave pods within my OpenShift cluster and perform the steps listed in that stage of my Jenkinsfile. For instance, I use a .NET Core 2.1 Jenkins Slave to do my build and a base Jenkins Slave image within OpenShift to perform the build step defined in my project template YAML. These Jenkins Slaves correspond to the Manage Jenkins → Configure System setup within your local Jenkins application in your project. Scroll down to the bottom of that page and see the K8s Pod Template listings like the image below. They specify a name, label (we reference in our Jenkinsfile), container name, container image and other specifics.

If you have to use some other than the included Maven and NodeJS pod templates for Jenkins Slaves here you have to click Add Template and then fill out information. Then click Add Container and add more. It is a PITA!! And if you script projects via templates and add/test/destroy/adjust/add/test like I do this is an EXTREME PITA and the DX sucks. Bad!

Config Maps to the Rescue!

This is where Config Maps come in. You can specify Config Maps to setup all these Jenkins Slave pod templates for you upon project creation/template application (best) or even after you have a project and need to add them to your Jenkins setup (still not bad). I have several listed here in my GH repo that you can use. And I will be adding more so feel free to copy, use, fork at will. The config maps look like the below and specify the name, the role, the image and some other pieces. I highlighted the main areas to adjust per Config Map so they do not overwrite each other and point to the correct image to use.

Example Config Map in OpenShift for Jenkins Slave images

I like to include these in my deployment YAML so they are automatically included when I apply my project template. That way when Jenkins spins up in my project it already reads these and has them ready to roll! However if you have a project you want to use these with you can one-at-a-time add them through the Web Console interface. Click on the “Add to Project” button and then “Import YAML / JSON” to have the screen pop up. You can copy/paste the base Jenkins Slave image I have here to see what it does. Click Create and then go to your Resources → Config Maps area to see it listed.

Listing of all Config maps in my PeopleAPI project in OpenShift

If you have several to add repeat all these steps. Then to apply them you can re-deploy the Jenkins application in your project. Or you can click on the name of one of the Config Maps and then click the Add to Application button on the far right. Specify “jenkins” in the application dropdown and click Save. Jenkins will reset, read in all your Jenkins Slave configurations and apply them all quickly and easily. Now you are ready to roll!

This has saved me hours in setting up these K8s Pod Templates, trying to remember what image I use for some and the Red Hat Access Registry I use for others. Not to mention adding in a custom Jenkins setup with all my plugins to quickly setup and run any DevSecOps for existing or new projects consistently and easily. That is what I am about at Cingulara. Automating everything and helping out developers. I hope this helps you shrink your time to GSD!

Dale Bingham

Written by

CTO of Cingulara. Software Geek by trade. Father of three daughters. Husband. Lover of newer tech where it fits. Follow at @cingulara

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade