Setting up Jenkins on Google Kubernetes Engine

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

Objectives

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

Costs

This tutorial uses the following billable components of Google Cloud:

  • Google Kubernetes Engine

To generate a cost estimate based on your projected usage, use the pricing calculator. New Google Cloud users might be eligible for a free trial.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Enable the GKE APIs.

    Enable the APIs

  5. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  7. Enable the GKE 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 Cloud Console, and includes Cloud SDK and other tools you need for Google Cloud 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! Type "help" to get started.
    
  2. Clone the sample code:

    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 GKE.

  3. Navigate to the sample code directory:

    cd continuous-deployment-on-kubernetes
    

Creating a GKE cluster

  1. Provision a GKE cluster. This step can take up to several minutes to complete.

    gcloud container clusters create jenkins-cd \
      --zone us-east1-d --scopes cloud-platform
    

    The cloud-platform scope enables Jenkins to access Cloud Source Repositories and Container Registry.

  2. Confirm that you can connect to your cluster.

    kubectl cluster-info
    

    If you are able to successfully connect to your cluster, the output resembles the following:

    Kubernetes control plane is running at https://35.196.84.95
    GLBCDefaultBackend is running at https://35.196.84.95/api/v1/namespaces/kube-system/services/default-http-backend:http/proxy
    KubeDNS is running at https://35.196.84.95/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
    Metrics-server is running at https://35.196.84.95/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy
    
  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)
    

Installing Jenkins

Use Helm to deploy Jenkins from the repository.

  1. Add the Jenkins Helm chart repository:

    helm repo add jenkinsci https://charts.jenkins.io
    helm repo update
    

You 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 cd-jenkins -f jenkins/values.yaml jenkinsci/jenkins --wait
    

    For more information on configuring the Jenkins installation, visit the Jenkins chart's documentation page.

  2. Ensure the Jenkins pod goes to the Running state and the container is in the READY state:

    kubectl get pods
    

    You will see 2/2 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-0   2/2     Running   0          6m30s
    
  3. Check that the Kubernetes Services were created properly.

    kubectl get svc
    

    The output resembles the following:

    NAME               CLUSTER-IP     EXTERNAL-IP   PORT(S)     AGE
    cd-jenkins         10.35.249.67   <none>        8080/TCP    3h
    cd-jenkins-agent   10.35.248.1    <none>        50000/TCP   3h
    kubernetes         10.35.240.1    <none>        443/TCP     9h
    

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. Set up port forwarding to the Jenkins UI from Cloud Shell:

    export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/component=jenkins-master" -l "app.kubernetes.io/instance=cd-jenkins" -o jsonpath="{.items[0].metadata.name}")
    kubectl port-forward $POD_NAME 8080:8080 >> /dev/null 2>&1 &
    
  2. To open the Jenkins user interface, click Web Preview in Cloud Shell and click Preview on port 8080.

    port8080

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

Clean 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 that you created for the tutorial.

To delete the project:

  1. In the Cloud Console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Deleting the GKE cluster

  1. Delete the GKE cluster:
    gcloud container clusters delete jenkins-cd --zone us-east1-d
    

What's next