In my previous blog post, I demonstrated how to deploy Jenkins in a master/slave architecture on Oracle Cloud Infrastructure by using the Oracle Cloud Infrastructure Compute plugin for Jenkins. This plugin enables you to spin up virtual machine (VM) or bare metal instances as slaves/agents on-demand within Oracle Cloud Infrastructure and then tear them down automatically after the job is complete. By spinning up a large number of agents, Jenkins can run many jobs in parallel.
As an alternative to using the VM-based plugin, you can instead create container-based Jenkins agents, which can be spun up more quickly than VMs (seconds versus minutes) and can be torn down quickly after the build job is complete. Jenkins container-based agents are provisioned based on a Docker container image with all the tools and environment settings that you need.
In this blog post, I'll demonstrate how to set up Jenkins agents as Docker containers and deploy them within a Kubernetes cluster in a few steps. I'll use a different plugin for Jenkins to accomplish this: the Kubernetes plugin for Oracle Cloud Infrastructure Container Engine for Kubernetes (OKE). OKE delivers secure, highly available Kubernetes clusters and manages containerized applications on Oracle Cloud Infrastructure. Following the steps in this blog post, you can create a Jenkins deployment as shown in the following figure:
The Kubernetes plugin for Jenkins is used to run dynamic Jenkins agents in a Kubernetes cluster.
Configure Kubernetes role-based access control (RBAC) to enable the default service account token to interact with the cluster by giving it the admin role:
Configure the Kubernetes Pod Template as shown in the following screenshot. Remember the label that you set for Jenkins agents because you will need it later when running the build jobs.
Configure the Container Template as shown in the following screenshot. For the purpose of this blog post, we are using Oracle Container Registry as the source to pull a custom Jenkins jnlp-slave docker image. Ensure that the Jenkins jnlp-slave docker image is already available in the registry. You can also use the public Docker Hub registry to pull in the Jenkins jnlp-slave image, in which case you enter jenkins/jnlp-slave in the Docker image field. Save the configuration.
This completes the deployment of Jenkins agents on Oracle Cloud Infrastructure Container Engine for Kubernetes (OKE).
In the preceding deployment, we ran the Jenkins master as a VM and scaled out Jenkins agents/slaves as Docker containers on the Container Service for Kubernetes (OKE). We can extend this deployment by deploying the Jenkins master inside the Kubernetes cluster, alongside the Jenkins slaves. This configuration provides fault tolerance for Jenkins containers (both master and slaves), service resiliency, and better resource utilization. Let’s look at how to achieve this in a few steps. The deployment looks similar to the setup shown in the following figure:
The deployment of the Jenkins master in Kubernetes includes the following steps:
The jenkins-master.yaml manifest file contains the deployment configuration for the Jenkins master, which creates a single replica. We'll useg the latest Jenkins image in this setup while exposing ports 8080 and 50000 on Jenkins master containers. The jenkins-dir volume mount is associated with the PVC called jenkins-master-pvc.
The jenkins-pvc.yaml file consists of a PVC configuration that includes Oracle Cloud Infrastructure block volumes. We'll reserve a 50-GB block volume, which will be used to store Jenkins build files and artifacts, if needed.
Store the manifest files in a directory (for example, jenkins-k8s) and run the following command to create a Jenkins master deployment with a PVC:
Verify that the deployment and pod are created:
Verify that the PVC is created, either by running the following command or going to the Block Volumes section of the Oracle Cloud Infrastructure Console:
Now that the deployment is created, we can create a service and expose it via an Oracle Cloud Infrastructure load balancer on port 80 while setting the target port to 8080 (the Jenkins master listens on 8080 by default).
You can see that the load balancer is provisioned on the Oracle Cloud Infrastructure Console. After it is provisioned, it will have a public IP address exposed with port 80 as the listener.
You can verify the public IP address of your service by running the following command:
Access the Jenkins dashboard on the public IP address that we obtained in step 3, on port 80. You should see the following screen:
Run the following commands to get the initial admin password for Jenkins.
After this, the process of configuring the Jenkins master is similar to what I illustrated in my previous blog post on deploying Jenkins on Oracle Cloud Infrastructure. After configuring the master, you can install the Kubernetes plugin and scale out the Jenkins slaves as illustrated in the first part of this post.
Oracle Cloud Infrastructure Container Engine for Kubernetes (OKE) offers seamless integration of the Jenkins Kubernetes plugin. Configuring OKE on Jenkins Kubernetes plugin is similar to configuring other Kubernetes engines. OKE delivers secure, highly available Kubernetes clusters and manages containerized applications on Oracle Cloud Infrastructure.