Setting up Jenkins on Kubernetes Engine

This tutorial shows you how to set up Jenkins on Kubernetes Engine to help orchestrate your software delivery pipeline.

Objectives

  • Creating a Kubernetes cluster with Kubernetes Engine.
  • Installing Jenkins using Helm.
  • Connecting to Jenkins.

Costs

This tutorial uses billable components of Google Cloud Platform, including:

  • Compute Engine
  • Kubernetes Engine
  • Cloud Build

Use the Pricing Calculator to generate a cost estimate based on your projected usage. New GCP users might be eligible for a free trial.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Select or create a GCP project.

    Go to the Manage resources page

  3. Make sure that billing is enabled for your project.

    Learn how to enable billing

  4. Enable the Compute Engine, Kubernetes Engine, and Cloud Builder APIs.

    Enable the APIs

Preparing your environment

First, prepare your deployment environment.

  1. Activate Cloud Shell. Cloud Shell gives you access to the command line in GCP Console, and includes Cloud SDK and other tools you need for GCP development. Cloud Shell can take several minutes to provision.

    Activate Cloud Shell

    After the process completes, you'll see the following output:

    Welcome to Cloud Shell! For help, visit https://cloud.google.com/cloud-shell/help.
    

  2. Set the default Compute Engine zone to us-east1-d:

    gcloud config set compute/zone us-east1-d
    

  3. Clone the sample code, or download the zip file.

    git clone https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetes.git
    

    The Git repository contains Kubernetes manifests that you'll use to deploy Jenkins. The manifests and their settings are described in Configuring Jenkins for Kubernetes Engine.

  4. Navigate to the sample code directory:

    cd continuous-deployment-on-kubernetes
    

Creating a Kubernetes cluster

You can use Kubernetes Engine to create and manage your Kubernetes cluster.

  1. Create a Compute Engine network for the Kubernetes Engine cluster to connect to and use.

    gcloud compute networks create jenkins
    

  2. Provision a Kubernetes cluster using Kubernetes Engine. This step can take up to several minutes to complete.

    gcloud container clusters create jenkins-cd \
      --network jenkins --machine-type n1-standard-2 --num-nodes 2 \
      --scopes "https://www.googleapis.com/auth/projecthosting,storage-rw,cloud-platform"
    

    The extra scopes enable Jenkins to access Cloud Source Repositories and Container Registry.

  3. Confirm that your cluster is running.

    gcloud container clusters list
    

    Look for RUNNING in the STATUS column.

    NAME        LOCATION    MASTER_VERSION  MASTER_IP    MACHINE_TYPE   NODE_VERSION  NUM_NODES  STATUS
    jenkins-cd  us-east1-d  1.9.7-gke.3     35.231.8.57  n1-standard-2  1.9.7-gke.3   2          RUNNING
    

  4. Confirm that you can connect to your cluster by running a command to check the number of nodes.

    kubectl get nodes
    

    If you are able to successfully connect to your cluster, you will see 2 nodes listed.

    NAME                                        STATUS    ROLES     AGE       VERSION
    gke-jenkins-cd-default-pool-0e5fa564-drl8   Ready         3m        v1.9.7-gke.3
    gke-jenkins-cd-default-pool-0e5fa564-h8vb   Ready         3m        v1.9.7-gke.3
    

Installing Helm

Use Helm to deploy Jenkins from the repository.

  1. Download and install the Helm binary:

    wget https://storage.googleapis.com/kubernetes-helm/helm-v2.9.1-linux-amd64.tar.gz
    

  2. Unzip the file to your local system:

    tar zxfv helm-v2.9.1-linux-amd64.tar.gz
    cp linux-amd64/helm .
    

  3. Add yourself as a cluster administrator in the cluster's RBAC so that you can give Jenkins permissions in the cluster:

    kubectl create clusterrolebinding cluster-admin-binding --clusterrole=cluster-admin \
            --user=$(gcloud config get-value account)
    

  4. Grant Tiller, the server side of Helm, the cluster-admin role in your cluster:

    kubectl create serviceaccount tiller --namespace kube-system
    kubectl create clusterrolebinding tiller-admin-binding --clusterrole=cluster-admin \
                   --serviceaccount=kube-system:tiller
    

  5. Initialize Helm. This ensures that the Tiller is properly installed in your cluster.

    ./helm init --service-account=tiller
    ./helm update
    

  6. Ensure Helm is properly installed by running the following command:

    ./helm version
    

    You will see versions appear for both the client and the server as v2.9.1:

    Client: &version.Version{SemVer:"v2.9.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}
    Server: &version.Version{SemVer:"v2.9.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}
    

Installing Jenkins

You will use a custom values file to configure the Jenkins installation. For details on the configuration, look at the jenkins/values.yaml file.

  1. Use the Helm CLI to deploy the chart with your configuration set:

    ./helm install -n cd stable/jenkins -f jenkins/values.yaml --version 0.16.6 --wait
    

  2. After that command completes, ensure the Jenkins pod goes to the Running state and the container is in the READY state:

    kubectl get pods
    

    You will see 1/1 in the READY column and Running in the STATUS column. It can take a few minutes for Jenkins to complete its initialization.

    NAME                          READY     STATUS    RESTARTS   AGE
    cd-jenkins-7c786475dd-vbhg4   1/1       Running   0          1m
    

The Jenkins installation is using the Kubernetes Plugin to create builder agents. They will be automatically launched as necessary when the Jenkins master needs to run a build. When their work is done, they are automatically terminated and their resources are added back to the cluster's resource pool.

Connecting to Jenkins

  1. Retrieve the admin password that was automatically created by the Jenkins Helm chart:

    printf $(kubectl get secret cd-jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo
    

  2. Set up port forwarding to the Jenkins UI from Cloud Shell:

    export POD_NAME=$(kubectl get pods -l "component=cd-jenkins-master" -o jsonpath="{.items[0].metadata.name}")
    kubectl port-forward $POD_NAME 8080:8080 >> /dev/null &
    

  3. To open the Jenkins user interface, click Web Preview in Cloud Shell and click Preview on port 8080.

    port8080

  4. Click log in on the top right of the window. Enter admin for the User field and the password value from the previous step for the Password field.

  5. Click the log in button.

You now have access to Jenkins and a Kubernetes cluster managed by Kubernetes Engine. To take this solution further, you could use these components in your continuous delivery pipeline.

Cleaning up

After you've finished the tutorial, clean up the resources you created on GCP so you won't be billed for them in the future.

Deleting the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

To delete the project:

  1. In the GCP Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Deleting disks

  1. In the GCP Console, go to the Disks page.

    Go to the Disks page

  2. Click the checkbox next to your jenkins-home disk.
  3. Click the Delete button at the top of the page to delete the disk.

Deleting instances

To delete a Compute Engine instance:

  1. In the GCP Console, go to the VM Instances page.

    Go to the VM Instances page

  2. Click the checkbox next to the instance you want to delete.
  3. Click the Delete button at the top of the page to delete the instance.

What's next

Was this page helpful? Let us know how we did:

Send feedback about...