Setting up Jenkins on Google Kubernetes Engine

This tutorial shows you how to set up Jenkins on 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 billable components of Google Cloud Platform, including:

  • Compute Engine
  • Google 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. In the Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to the project selector page

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

  4. Enable the Compute Engine, GKE, and Cloud Build 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 GKE.

  4. Navigate to the sample code directory:

    cd continuous-deployment-on-kubernetes
    

Creating a Kubernetes cluster

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

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

    gcloud container clusters create jenkins-cd \
      --machine-type n1-standard-2 --num-nodes 2 \
      --scopes "https://www.googleapis.com/auth/source.read_write,cloud-platform" \
      --cluster-version 1.12
    

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

  2. 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.12.8-gke.10     35.231.8.57  n1-standard-2  1.12.8-gke.3   2          RUNNING
    
  3. Confirm that you can connect to your cluster.

    kubectl cluster-info
    

    If you are able to successfully connect to your cluster, Kubernetes component URLs display.

    Kubernetes master is running at https://130.211.178.38
    GLBCDefaultBackend is running at https://130.211.178.38/api/v1/proxy/namespaces/kube-system/services/default-http-backend
    Heapster is running at https://130.211.178.38/api/v1/proxy/namespaces/kube-system/services/heapster
    KubeDNS is running at https://130.211.178.38/api/v1/proxy/namespaces/kube-system/services/kube-dns
    kubernetes-dashboard is running at https://130.211.178.38/api/v1/proxy/namespaces/kube-system/services/kubernetes-dashboard
    

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.14.1-linux-amd64.tar.gz
    
  2. Unzip the file to your local system:

    tar zxfv helm-v2.14.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 repo 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.14.1:

    Client: &version.Version{SemVer:"v2.14.1", GitCommit:"5270352a09c7e8b6e8c9593002a73535276507c0", GitTreeState:"clean"}
    Server: &version.Version{SemVer:"v2.14.1", GitCommit:"5270352a09c7e8b6e8c9593002a73535276507c0", 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 1.2.2 --wait
    

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

  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
    
  3. 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" -o jsonpath="{.items[0].metadata.name}")
    kubectl port-forward $POD_NAME 8080:8080 >> /dev/null &
    
  4. Check that the Jenkins Service was created properly.

    kubectl get svc
    

    Example Output:

    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. 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. To open the Jenkins user interface, click Web Preview in Cloud Shell and click Preview on port 8080.

    port8080

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

  4. Click the log in button.

You now have access to Jenkins and a Kubernetes cluster managed by GKE. 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 that you created for the tutorial.

To delete the project:

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

    Go to the Manage resources page

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

Deleting disks

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

    Go to the Disks page

  2. Click the checkbox for your jenkins-home disk.
  3. Click Delete to delete the disk.

Deleting instances

To delete a Compute Engine instance:

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

    Go to the VM Instances page

  2. Click the checkbox for the instance you want to delete.
  3. Click Delete to delete the instance.

What's next

Var denne siden nyttig? Si fra hva du synes:

Send tilbakemelding om ...