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, including:
- Compute Engine
- Google Kubernetes Engine
- Cloud Build
Use the Pricing Calculator to generate a cost estimate based on your projected usage. New Google Cloud users might be eligible for a free trial.
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 Cloud project. Learn how to confirm that billing is enabled for your project.
- Enable the Compute Engine, GKE, and Cloud Build APIs.
Preparing your environment
First, prepare your deployment environment.
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.
After the process completes, you'll see the following output:
Welcome to Cloud Shell! For help, visit https://cloud.google.com/cloud-shell/help.
Set the default Compute Engine zone to
us-east1-d
:gcloud config set compute/zone us-east1-d
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.
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.
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.15
The extra scopes enable Jenkins to access Cloud Source Repositories and Container Registry.
Confirm that your cluster is running.
gcloud container clusters list
Look for
RUNNING
in theSTATUS
column.NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS jenkins-cd us-east1-d 1.15.11-gke.15 35.231.8.57 n1-standard-2 1.15.11-gke.15 2 RUNNING
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 Metrics-server is running at https://130.211.178.38/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy
Installing Helm
Use Helm to deploy Jenkins from the repository.
Download and install the Helm binary:
wget https://get.helm.sh/helm-v3.2.1-linux-amd64.tar.gz
Unzip the file to your local system:
tar zxfv helm-v3.2.1-linux-amd64.tar.gz cp linux-amd64/helm .
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)
Add the official stable repository:
./helm repo add jenkinsci https://charts.jenkins.io ./helm repo update
Ensure Helm is properly installed by running the following command:
./helm version
You will see versions appear for the client as
v3.2.1
:version.BuildInfo{Version:"v3.2.1", GitCommit:"fe51cd1e31e6a202cba7dead9552a6d418ded79a", GitTreeState:"clean", GoVersion:"go1.13.10"}
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.
Use the Helm CLI to deploy the chart with your configuration set:
./helm install cd-jenkins -f jenkins/values.yaml jenkinsci/jenkins --version 2.6.4 --wait
For more information on configuring the Jenkins installation, visit the Jenkins chart's documentation page.
After that command completes, ensure the Jenkins pod goes to the
Running
state and the container is in theREADY
state:kubectl get pods
You will see
1/1
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-7c786475dd-vbhg4 1/1 Running 0 1m
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 &
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
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
To open the Jenkins user interface, click Web Preview in Cloud Shell and click Preview on port 8080.
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.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:
- In the 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 disks
- In the Cloud Console, go to the Disks page.
-
Select the checkbox for
your
jenkins-home
disk. - To delete the disk, click Delete.
Deleting instances
To delete a Compute Engine instance:
- In the Cloud Console, go to the VM instances page.
- Select the checkbox for the instance that you want to delete.
- To delete the instance, click Delete.
What's next
Learn more about Jenkins on GKE best practices.
Learn about how to configure Jenkins for GKE.
Learn about how to set up continuous deployment to GKE using Jenkins.
Try out other Google Cloud features for yourself. Have a look at our tutorials.