Deploy Jenkins on Oracle Cloud Infrastructure
Faster software development has become a competitive advantage for companies. The automation of software development processes facilitates speed and consistency, which led to the rise for having a Continuous Integration (CI) and Continuous Delivery and Deployment (CD) pipelines. Jenkins is a very popular product among Oracle Cloud Infrastructure customers, which can automate all of the phases of CI and CD. You can host Jenkins on Oracle Cloud Infrastructure to centralize your build automation and scale your deployment as the needs of your software projects grow.
This is the first in a series of blog posts on how to set up a CI/CD build pipeline on Oracle Cloud Infrastructure using Jenkins. Jenkins is extensible by design via plugins. Plugins give Jenkins the flexibility to automate a wide range of processes on diverse platforms. Without delving too much into the architecture of Jenkins, let’s quickly understand the concept of master/slave in Jenkins. Jenkins supports the* master and slave/agent* mode, where the workload of building projects is delegated to multiple agent nodes by the master, allowing a single Jenkins installation to host multiple projects, or to provide different environments needed for builds/tests.
A master operating by itself is the basic installation of Jenkins and in this configuration the master handles all the tasks for your build system. If you start to use Jenkins frequently with just a master, it’s common to find that you run out of resources (memory, CPU, etc.). At this point, you can either upgrade your master or you can set up agents to pick up the load. Alternatively, in a scenario where you need several different environments to test your builds, using an agent to represent each of your required environments can be a better solution. An agent is a computer that is set up to offload build projects from the master. Once the agent has been set up, this distribution of tasks is fairly automatic.
In this tutorial, we demonstrate how to create a Jenkins master/slave architecture on Oracle Cloud Infrastructure using the Jenkins Oracle Cloud Infrastructure Compute plugin. When installed on Jenkins master, the plugin allows you to spin up instances (slaves/agents) on demand within Oracle Cloud Infrastructure, and remove instances or free resources automatically once the build job completes.
Let’s look at how to launch a Jenkins master/slave deployment on Oracle Cloud Infrastructure, in a few easy steps:
Step 1 - VCN set up and Jenkins installation
- Create a VCN with a single subnet in an availability domain to house Jenkins master and agent nodes. In this tutorial, we create both master and agents in the same subnet (this is not mandatory).
- Launch an instance in the newly created subnet. In this case, we are using a VMStandard1.1 shape running Oracle Linux 7.5. We use this instance to run our Jenkins master node. A good practice is to select a slightly smaller instance shape for the Jenkins master and larger shapes for agent nodes, as the heavy lifting of running the actual build is done by the agent nodes.
- Log into this instance using the public IP address and the associated public key of the instance. As Jenkins runs on Java, update the yum packages and install Java 8 on this instance, by issuing the following commands:
sudo yum -y update
sudo yum -y install java
- Now that the dependencies have been installed, go ahead and install Jenkins using these commands:
sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key
sudo yum -y install jenkins
- Start the Jenkins service using the command:
sudo service jenkins start
If successful, the command output looks similar to this:
- By default, Jenkins listens on port TCP 8080. Open this port on the instance firewall by configuring the firewall.
sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp
sudo firewall-cmd --reload
- Now Jenkins is configured on the instance. This instance acts as the Jenkins master node. To access this node from the outside internet, open the TCP port 8080 on the security list for the subnet which houses the Jenkins master instance.
Step 2 - Configuring Jenkins Master
- On the web browser, access Jenkins dashboard using the public IP of the instance and port 8080. You will see the Unlock Jenkins screen, which displays the location of the initial password.
- In the terminal window, use the cat command to display the password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
- Copy the 32-character alphanumeric password from the terminal and paste it into the Administrator password field, then click Continue. The next screen presents the option of installing suggested plugins or selecting specific plugins.
- Go ahead and install the suggested plugins.
- When the installation is complete, you are prompted to set up the first administrative user. You can skip this step and continue as an admin using the initial password used in the previous step. Here, we proceed as admin. You should see a Jenkins is ready! confirmation screen. Click Start using Jenkins to visit the main Jenkins dashboard. At this point, basic configuration of Jenkins master has been successfully completed.
- Next, go ahead and install Oracle Cloud Infrastructure Compute plugin which allows us to launch slave nodes. To do this, go to Manage Jenkins and click Manage Plugins.
Step 3 - Installing and Configuring Jenkins Oracle Cloud Infrastructure Compute plugin
- In the Manage Plugins section, under the Available tab, search for Oracle Cloud Infrastructure Compute plugin and perform Install without restart on it.
- Go back to the Manage Jenkins page and go to Configure System. Scroll all the way to the bottom and click Add a new cloud. Then, click Oracle Cloud Infrastructure Compute.
- Populate the fields with your API Fingerprint, API Key, User, and Tenancy OCID which you can locate on the Oracle Cloud Infrastructure console. For more information on how to locate these fields refer to https://github.com/oracle/oci-compute-jenkins-plugin. Click “Test Connection”, If all the information you entered is correct, the dialog displays Successfully connected to Oracle Cloud Infrastructure Compute.
- Now, scroll down and click on Instance Templates, where you specify the shape and subnets of your Jenkins agent nodes. We suggest selecting the shape of slaves that are bigger than the master, as these run the actual build. In this tutorial, we selected a VMStandard2.1 instance running Oracle Linux 7.5 in the same availability domain and subnet as the Jenkins Master node.
- Click the Advanced button and configure an init script for the agent nodes. Agent nodes also require having Java installed on them before communicating with the master, so go ahead and specify that in the init script section.
Step 4 - Launching Jenkins Agent Nodes
- The previous step completes the configuration of Jenkins agent nodes. Go ahead and save this template. Navigate to Build execution status and click on it. You will see that the Oracle Cloud Infrastructure compute plugin is listed there.
- Finally, launch the agent node as shown. You can launch multiple agent nodes, by repeating the same operation multiple times.
- Once you launch, the console displays:
- This takes a couple of minutes. If you go back to the Oracle Cloud Infrastructure console, you see the slave instance being provisioned:
- Once the instance is fully provisioned, it is listed in the Jenkins Dashboard also, as indicated in the following figure:
Conclusion
This concludes this tutorial on setting up Jenkins master/slave nodes on Oracle Cloud Infrastructure (OCI). There are other ways to deploy Jenkins on OCI which are also quite suitable for enterprise deployments. We shall demonstrate these deployment strategies in my subsequent blog posts:
Traditional deployment - which involves manually launching and configuring the agent nodes by just using Jenkins SSH slave plugins and not Oracle cloud infrastructure compute plugin. A setup like this involves a lot of manual configuration, but on the contrary, it gives a lot more granular control over how to launch Jenkins agents. Currently, using the Oracle cloud infrastructure compute plugin, you can launch agent nodes within a single Availability Domain, if you plan on launching agent nodes across multiple Availability Domains, we recommend this approach.
Containerized deployment - which involves deploying and running Jenkins master and the worker nodes as Docker containers. Running Jenkins in Docker containers allows you to use servers running Jenkins agent nodes more efficiently. It also simplifies the configuration of the agent node servers. Using containers to manage builds allows the underlying servers to be pooled into a cluster. The Jenkins agent containers can then run and execute a build on any of the servers with resources available to support the build. This ability to pool multiple builds to run independently of each other on the server improves the utilization of the server.
In my next blog post, we shall demonstrate how to create a Jenkins build pipeline on Oracle Cloud Infrastructure using the setup we just created.