Oracle by Example brandingConfiguring Jenkins Kubernetes Plug-in With Oracle Cloud Infrastructure Container Engine for Kubernetes

section 0Before You Begin

This 15-minute tutorial shows you how to configure the Jenkins Kubernetes plug-in with Oracle Cloud Infrastructure Container Engine for Kubernetes.

Background

Oracle Cloud Infrastructure Container Engine for Kubernetes delivers secure, highly available Kubernetes clusters and enables you to deploy containerized applications to the cloud. You specify the compute resources that the applications require, and Container Engine for Kubernetes provisions them on Oracle Cloud Infrastructure in an existing tenancy.

The Jenkins Kubernetes plug-in uses a Kubernetes cluster to dynamically provision a Jenkins agent using the Kubernetes scheduling mechanisms to optimize the loads, run a single build, and then tear-down that agent. Using the Jenkins Kubernetes plug-in reduces the number of build agents to manage.

Configure the Jenkins Kubernetes plug-in with Oracle Cloud Infrastructure Container Engine for Kubernetes to dynamically provision the Jenkins agents on the Oracle Cloud Infrastructure's Kubernetes clusters.

What Do You Need?

  • Jenkins server
  • Jenkins Kubernetes plug-in installed in the Jenkins server
  • An Oracle Cloud Infrastructure Kubernetes cluster and its kubeconfig configuration file
  • kubectl installed and configured to use

section 1Get Kubernetes Server URL, Secret Token, and Server Certificate Key

  1. On the terminal window of your computer, enter the following kubectl command to get the Kubernetes server URL available in the kubeconfig configuration file for the Oracle Cloud Infrastructure's Kubernetes cluster:
    $ kubectl config view | grep server | cut -f 2- -d ":" | tr -d " "
  2. Enter the following kubectl command to get the Kubernetes secret token available in the kubeconfig configuration file for the Oracle Cloud Infrastructure's Kubernetes cluster:
    $ kubectl describe secret $(kubectl get secrets | grep default | cut -f1 -d ' ') | grep -E '^token' | cut -f2 -d':' | tr -d '\t'
  3. Get the Kubernetes server certificate key:
    • Copy the certificate-authority-data from the kubeconfig configuration file for the Oracle Cloud Infrastructure's Kubernetes cluster. On the terminal window, enter the following kubectl command to save the certificate-authority-data as the environment variable, certificatedata:
       $ export certificatedata=paste the certificate-authority-data within single quotation marks here such as ’9UQms5eVlXTnNaVEV….IKLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo’
    • Enter the following kubectl command to decode the certificate-authority-data:
       $ echo $certificatedata | base64 --decode

section 2Authorize Your Configuration

You must add a rule for Oracle Cloud Infrastructure Container Engine for Kubernetes to allow your configuration to work.

  1. Open the text editor on your computer, create a .yaml file using the details in rbac-yaml-file-details.txt, and save it as rbac.yaml in the tmp directory.
  2. On the terminal window of your computer, run the rbac.yaml file using the following kubectl command:
    kubectl apply -f /tmp/rbac.yaml

section 3Configure the Jenkins Kubernetes Plug-in

This tutorial uses the ready-made Docker images for Jenkins agents using the Java Network Launching Protocol (JNLP) at Jenkins/Jenkins-slave.

  1. Sign in to the Jenkins server with your credentials.
  2. On the Welcome page, click Manage Jenkins, and then click Configure System.
  3. Select Cloud from the configuration drop-down list, click Add a new cloud, and then select Kubernetes.
  4. In Kubernetes, enter the following:
    • Name: Kubernetes
    • Kubernetes URL: The Kubernetes server URL available in the kubeconfig configuration file of your Oracle Cloud Infrastructure's Kubernetes cluster.
    • Kubernetes server certificate key: The decoded certificate-authority-data.
    • Kubernetes Namespace: default
    • Jenkins URL: The URL for your Jenkins server.

      For example, http://myServer:8080 where myServer is the name of the system running Jenkins.

  5. For Credentials, click Add, and select Jenkins. In the Jenkins Credentials Provider: Jenkins dialog box, select Secret text from the Kind drop-down list, add the Kubernetes secret token in Secret, enter default2 in ID, and then click Add.
    Jenkins credentials
    Description of the illustration credentials.png
  6. Click Test Connection.
    Kubernetes configuration details
    Description of the illustration cloudkubernetes.png
  7. In Kubernetes Pod Template, enter the following to configure the pod template of Kubernetes Cloud:
    • Name: jnlp slave
    • Namespace: default
    • Label: OBETest
    • Usage: Use this node as much as possible
  8. In Container Template, enter the following to configure the container template of Kubernetes Cloud:
    • Name: jnlp
    • Docker Image: jenkins/jnlp-slave
    • Working Directory: /home/jenkins
    • Allocate pseudo-TTY: Select the check box.
    Container template details
    Description of the illustration containertemplate.png
  9. Click Save.

section 4Verify the Jenkins Kubernetes Plug-in

  1. On the Welcome page, click Manage Jenkins, then click Manage Nodes, and ensure that the number of executors for the master node is more than zero.
  2. On the Welcome page, click New Item.
  3. In Enter an item name, enter OBETest, select Freestyle project, and then click OK.
    New item
    Description of the illustration newitem.png
  4. In the General tab, select the Restrict where this project can be run check box, and in Label Expression, enter OBETest.
    General tab
    Description of the illustration generaltab.png
  5. In the Build tab, select Execute shell from the Add build step drop-down list.
  6. In Execute Shell, enter echo "helloworld" in the Command field, and then click Save.
  7. On the Project OBETest page, click Build Now.
  8. In Build History, click Console Output from the #1 build drop-down list.
    Build finished successfully
    Description of the illustration buildsuccess.png
  9. In Console Output, verify that the agent is provisioned from the Kubernetes pod template label OBETest, and the build is finished successfully.
    Console output
    Description of the illustration consoleoutput.png

more informationWant to Learn More?