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
In this document, you use the following billable components of Google Cloud:
- Google Kubernetes Engine
To generate a cost estimate based on your projected usage,
use the pricing calculator.
Before you begin
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project.
-
Enable the GKE APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project.
-
Enable the GKE APIs.
Preparing your environment
First, prepare your deployment environment.
Activate Cloud Shell. Cloud Shell gives you access to the command line in Google Cloud console, and includes gcloud CLI and other tools you need for Google Cloud development. Cloud Shell can take several minutes to provision.
After the process completes, you'll see the following output:
Welcome to Cloud Shell! Type "help" to get started.
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.
Navigate to the sample code directory:
cd continuous-deployment-on-kubernetes
Creating a GKE cluster
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.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
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.
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.
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.
Ensure the Jenkins pod goes to the
Running
state and the container is in theREADY
state:kubectl get pods
You will see
2/2
in theREADY
column andRunning
in theSTATUS
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
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
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 &
To open the Jenkins user interface, click Web Preview in Cloud Shell and click Preview on port 8080.
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 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:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Deleting the GKE cluster
- Delete the GKE cluster:
gcloud container clusters delete jenkins-cd --zone us-east1-d
What's next
Learn more about Jenkins on GKE best practices.
Learn how to configure Jenkins for GKE.
Learn how to set up continuous deployment to GKE using Jenkins.