Tutorial: Deploying the Jenkins Helm Community Chart on IBM Cloud Kubernetes Service

5 min read

By: Eric Jodet and Benoit Jaunin

Install a Jenkins server on IBM Cloud Kubernetes Service and deploy an app.

Looking to deploy your favorite application using Jenkins running on IBM Cloud Kubernetes Service? The Jenkins Helm community chart enables you to install and configure a Jenkins server on a cluster.

The IBM Hybrid Cloud Team has authored a tutorial that will guide you through the steps required to set up and install a Jenkins server and deploy a sample Node and React application on IBM Cloud Kubernetes Service.

Jenkins

Jenkins® is an open source automation server. With Jenkins, organizations can accelerate and automate the software development process. Jenkins manages and controls software delivery processes throughout the entire lifecycle, including build, document, test, package, stage, deployment, static code analysis, and much more.

IBM Cloud Kubernetes Service

We recommend deploying Jenkins and the sample application by using the IBM Cloud Kubernetes Service, which provides a native Kubernetes experience. Kubernetes is an open source system for automating deployment, scaling, and management of containerized applications. The IBM Cloud Kubernetes Service is highly secure, easy to use, and fully manages your Kubernetes clusters, allowing the developer to focus on the fun stuff—building applications.

Getting started with CMT Jenkins

This tutorial will let you install and deploy the Jenkins Helm community chart on IBM Kubernetes Service and run a fully configured Jenkins server on IBM Cloud Kubernetes Service.

The provided script will automatically create a pipeline project that will deploy this sample Node and React application.

Installation overview

  • Prerequisites: Infrastructure set-up (IBM Cloud CLI installation, Kubernetes cluster creation, etc.)
  • Installation: Automated configuration and deployment of the Jenkins Helm community chart
  • Post-installation verification and set-up
  • Optional: Publish Jenkins build artifacts to IBM Cloud Object Storage

Prerequisites

1. Create a Kubernetes Cluster on IBM Cloud

In this example, we will provision a Standard cluster that has the minimum capacity to run our containerized Jenkins server example.

  • Go to the IBM Cloud create Kubernetes Cluster page in order to create your cluster.
  • Select the location (target region).
  • Select Standard cluster type.
  • Select Kubernetes version 1.10.
  • Enter cluster name or keep the default one.
  • Make sure to have at least two worker nodes.
  • Click on the Create Cluster button.
  • Wait for the cluster to be provisioned—this should take a couple of minutes.

2. Install the IBM Cloud CLI

Please refer to this page in order to install the IBM Cloud CLI.

3. Log in to IBM Cloud

Run the following command to log in to the IBM Cloud region where the cluster was created:

  • Using standard IBM Id: ibmcloud login -a
  • Using a federated IBM Id: ibmcloud login -a --sso

4. Install the IBM Cloud plugins

5. Helm

Ensure the Helm Client is installed.

Installation

Overview

The IBM Hybrid Cloud Team has authored a customized script that will help you set up and automate the deployment of the Jenkins Helm community chart to your IBM Cloud Kubernetes Service cluster. This script will do the following:

  • Set the context for your Kubernetes cluster in your CLI.
  • Create the target namespace - defaulted to jenkins.
  • Configure helm with IBM repositories.
  • Install and configure IBM Block Storage(*).
  • Set all the configuration values to manage ingress exposure of the Jenkins server, slave pods, and sample pipeline creation.
  • Install the Jenkins helm community chart.

(*) If IBM Block Storage is already installed on your cluster, make sure that the version is at least 1.3.0.

Download the deployment script

Download and unzip the install_jenkins_helm_community_chart_on_iks.sh script to any location of your choice.

Adapt the deployment script

Edit the script or set the following environment values according to your installation context and goals:

  • export IKS_REGION=${IKS_REGION:-"<target_region>"}
  • export IKS_ACCOUNT=${IKS_ACCOUNT:-"<your_account_id>"}
  • export IKS_CLUSTER=${IKS_CLUSTER:-"<your_cluster_name>"}
  • export IKS_NAMESPACE=${IKS_NAMESPACE:-"jenkins"}

Save your changes.

Run the script

  • Cd to the directory where the script was downloaded and unzipped.
  • Ensure you are logged in to IBM Cloud.
  • Run: install_jenkins_helm_community_chart_on_iks.sh.
  • Wait for the deployment to complete.

Example output:

Example output:

NOTES:
1. Get your 'admin' user password by running:
printf $(kubectl get secret --namespace jenkins cmt-jenkins-readme -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo

2. Visit http://cmt-jenkins-readme.cmt-jenkins.us-east.containers.appdomain.cloud

3. Login with the password from step 1 and the username: admin

For more information on running Jenkins on Kubernetes, visit:
https://cloud.google.com/solutions/jenkins-on-container-engine
Configure the Kubernetes plugin in Jenkins to use the following Service Account name cmt-jenkins-readme using the following steps:
Create a Jenkins credential of type Kubernetes service account with service account name cmt-jenkins-readme
Under configure Jenkins -- Update the credentials config in the cloud section to use the service account credential you created in the step above.

Post-installation verification and set-up

Log in to the Jenkins server

  • Refer to the above command output to log in as admin to the Jenkins server.
  • Get the Jenkins server URL.
  • Get the admin user's password.
  • Open a browser and log in.

You now have a configured Jenkins server running on a Kubernetes cluster—well done!

Jenkins credentials

The Jenkins credentials have been created but need to be updated with their corresponding secret.

IBM Cloud

  • ibm-cr-credentials: Used by the docker push (to private container registry) command.
    • Create the IBM Cloud Container Registry token: $ ibmcloud cr token-add --non-expiring --readwrite --description "jenkins sample".
    • Keep a copy of the value of the obtained Token.
  • ibmcloud-apikey: Used by ibmcloud commands to get the cluster configuration for the kubectl deploy commands.
    • Create the IBM Cloud API key: $ ibmcloud iam api-key-create MyKey -d "this is my API key".
    • Keep a copy of the value of the obtained apikey.

Jenkins

Go to Jenkins > Credentials and click the corresponding credential:

  • ibm-cr-credentials
    • Click on Update.
    • Replace the Password value with the Container Registry token you just created.
    • Save your changes.
  • ibmcloud-apikey
    • Click on Update.
    • Replace the Secret value with the IBM Cloud apiKey you just created.
    • Save your changes.

IBM Cloud Container Registry

Important: The pipeline deploy job pushes the built Docker image in the jenkinssample namespace.

This namespace must exist and be accessible by your account:

  • ibmcloud cr namespaces to list the existing namespaces
  • ibmcloud cr namespace-add jenkinssample to create the namespace in the private container registry
  • If you get this error: The requested namespace is already in use in registry 'registry.ng.bluemix.net'— create any namespace of your choice and reflect this change in the pipeline script (see below)

NodeJs Kubernetes Pod Template

  • Go to > Manage Jenkins > Configure System.
  • Locate the Cloud Kubernetes section.
  • Verify that the cmt-jenkins-jenkins-slave Kubernetes Pod template is present, along with its Container Templates.

Sample pipeline project

The sample pipeline project builds and deploys a sample Node and React application to a target Kubernetes cluster.

This target cluster might be the same as the one where you just deployed the Jenkins server, or it may be another IBM Cloud Kubernetes Service cluster.

The deployment script, therefore, needs to be adapted to match your environment.

  • Go the Jenkins home page.
  • Click on the sample pipeline project.
  • Once on the project, select Configure, and go to the pipeline script section. Locate the following code sections:
    • K8S_CLUSTER ='<your_cluster_name>': Replace this value with the target cluster name
    • K8S_NAMESPACE = 'default': (Optional) replace this value with the namespace where you want to deploy the application
    • ibmcloud api https://api.ng.bluemix.net: Change this value to the API of the region where the cluster is running

    • ibmcloud target -r us-south: Change this value to the region where the cluster is running

  • Optional: If you selected to use a container registry namespace other than the default one, replace jenkinssample with your value.
    • def customImage = docker.build("<your-cr-registry-namespace>/sample-nodejs-react:${env.BUILD_NUMBER}")
    • image: registry.ng.bluemix.net/<your-cr-registry-namespace>/sample-nodejs-react
  • Save your changes.

Deploy the sample application

Deploy the sample application by running the sample pipeline project:

  • Ensure both ibm-cr-credentials and ibmcloud-apikey Jenkins credentials are set (see above).
  • Select the sample pipeline project and click on Build Now:
    Jenkins1

  • Go to the job's Console Output:
    Jenkins2

  • Review the console output, and verify that the sample application was successfully deployed:
    + APP_IP=184.173.1.166
    + kubectl get service sample-nodejs-react --namespace default --no-headers
    + awk -F[:/] {print $2;}
    + awk {print $5;}
    + APP_PORT=32537
    + echo App is accessible there: http://184.173.1.166:32537
    App is accessible there: http://184.173.1.166:32537
    
    [Pipeline] End of Pipeline
    Finished: SUCCESS
  • You may now access you deployed Node React application using the URL found in the job's console output:
    react app

Wrapping up

From zero to hero—you just installed the Jenkins Helm community chart on IBM Kubernetes Service and deployed your first application using the provided pipeline sample.

Congratulations!

Optional: IBM Cloud Object Storage (COS)

Follow these steps if you want to publish Jenkins build artifacts to IBM Cloud Object Storage (COS).

This requires the creation of a new COS and the configuration of the Jenkins S3 plugin.

IBM Cloud: COS creation

  • Create a new COS:
    • Service name: Any name of your choice
    • Select a resource group: Select a Resource Group or keep the default one
    • Click on the Create button
  • Create a new Bucket:
    • Unique bucket name: Any name of your choice
    • Location: Location of your choice
    • All other settings: Keep default values
      COS Bucket

       

Note: Both bucket name and location will be used later as parameters in the Jenkins pipeline script in order to publish Jenkins build artifacts to this bucket.

  • COS Service credentials:
    • Service credentials creation: Create the IBM COS Service credentials in order to generate access and secret keys. Ensure HMAC credentials are generated by adding the following in the Add Inline Configuration Parameters (Optional) field: {"HMAC":true}.
    • Access key id and secret: Click on View credentials in order to keep track of the values of access_key_id and secret_access_key that will be used later to complete the setup of the Jenkins S3 plugin.

Jenkins: S3 plugin configuration

  • Log in to the Jenkins server:
    • Refer to the above command output to log in as admin to the Jenkins server.
    • Get the Jenkins server URL.
    • Get the admin user's password.
  • Configure a new S3 profile:
    • Go to Manage Jenkins > Global Configuration. Under Amazon S3 profiles, enter the required parameters:
      • Profile name: Any name of your choice
      • Access key: The value of access_key_id (see above)
      • Secret key: The value of secret_access_key (see above)
    • Apply your changes: Ensure Check passed! message is displayed. 
      Check passed

    • Save your changes.

Jenkins: Sample pipeline modification

  • Select the sample pipeline project that was automatically created during installation and click onConfigure.
  • Locate and uncomment the IBM COS file upload block:
    • Remove the line /* Uncomment the following block in order to enable IBM COS file upload
    • Remove the line end of IBM COS file upload block */
  • Adapt the s3Upload command to your IBM COS environment:
    • Bucket name: Enter the name of the COS bucket you created. Here: [bucket: 'cmt-jenkins'
    • Bucket location: Enter the name of the location where bucket was created. Here: selectedRegion: 'us-east'
    • Profile name: The name of the Jenkins S3 pluguin profile you just created. Here: profileName: 'IBM Cloud'
  • Save your changes.

IBM COS file upload

If the pipeline project has been to upload files to IBM COS:

  • Jenkins: In the job's console output, ensure the s3Upload completed successfully:
    [sample] Running shell script
    + tar -czf jenkins-sample-42.tar.gz ./
    tar: ./jenkins-sample-42.tar.gz: file is the archive; skipping
    [Pipeline] s3Upload
    Publish artifacts to S3 Bucket Build is still running
    Publish artifacts to S3 Bucket Using S3 profile: IBM Cloud
    Publish artifacts to S3 Bucket bucket=cmt-jenkins, file=jenkins-sample-42.tar.gz region=us-east, will be uploaded from slave=false managed=false , server encryption false
    Post stage
    [Pipeline] echo
    Successfully uploaded "${BUILD_TAG}.tar.gz" to IBM COS
  • IBM COS: Verify that a tar file was uploaded to the bucket you created.
    uploaded

References

Be the first to hear about news, product updates, and innovation from IBM Cloud