Jenkins on Kubernetes in Google Cloud

Jenkins is a very popular and widely used CI/CD platform by many organizations. It is so flexible, and there are lots of plugins available for all kind of functionalities you want around CI/CD. Let’s see how we can deploy it on GKE using Helm, and what kind of advantages we gain out of this deployment.

What is Jenkins?

Jenkins is an open-source automation server that lets you flexibly orchestrate your build, test, and deployment pipelines. Jenkins allows developers to iterate quickly on projects without worrying about overhead issues that can stem from continuous delivery. Let’s deploy Jenkins on GKE using Helm.

What is Kubernetes?

Kubernetes Engine is Google Cloud’s hosted version of Kubernetes – a powerful cluster manager and orchestration system for containers. Kubernetes is an open-source project that can run on many different environments—from laptops to high-availability multi-node clusters; from virtual machines to bare metal. As mentioned before, Kubernetes apps are built on containers – these are lightweight applications bundled with all the necessary dependencies and libraries to run them. This underlying structure makes Kubernetes applications highly available, secure, and quick to deploy—an ideal framework for cloud developers.

What is Helm?

Helm is a package manager you can use to configure and deploy Kubernetes apps. It bundles all the dependencies and configuration yaml files into single deployable package.

Helm deploys packaged applications to Kubernetes and structures them into charts. The charts contain all pre-configured application resources along with all the versions into one easily manageable package.

Helm streamlines installing, upgrading, fetching dependencies, and configuring deployments on Kubernetes with simple CLI commands. Software packages are found in repositories or are created.

Helm charts repository is a central repository where you can find all the public helm charts (packages).

What are the advantages of deploying Jenkins on GKE?

When you need to set up a continuous delivery (CD) pipeline, deploying Jenkins on Kubernetes Engine provides important benefits over a standard VM-based deployment.

When your build process uses containers, one virtual host can run jobs on multiple operating systems. Kubernetes Engine provides ephemeral build executors—these are only utilized when builds are actively running, which leaves resources for other cluster tasks such as batch processing jobs. Another benefit of ephemeral build executors is speed—they launch in a matter of seconds.

Kubernetes Engine also comes pre-equipped with Google’s global load balancer, which you can use to automate web traffic routing to your instance(s). The load balancer handles SSL termination and utilizes a global IP address that’s configured with Google’s backbone network—coupled with your web front, this load balancer will always set your users on the fastest possible path to an application instance.

The solution we build will look something like the following diagram:

jenkins-on-kubernetes-on-gcp-1

Credit: Google Cloud

 

 

Let’s do it

Creating a Kubernetes Cluster

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

Check if cluster is created or not:

gcloud container clusters list

Get credentials for your cluster:

gcloud container clusters get-credentials jenkins-gke

Kubernetes Engine uses these credentials to access your newly provisioned cluster—confirm that you can connect to it by running the following command:

kubectl cluster-info

 

Setup Helm

Add Helm’s stable chart repo:

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

Ensure the repo is up to date:

helm repo update

 

Configure and Install Jenkins

When installing Jenkins, a values file can be used as a template to provide values that are necessary for setup.

You will use a custom values file to automatically configure your Kubernetes Cloud and add the following necessary plugins:

  • Kubernetes:1.29.4
  • Workflow-multibranch:latest
  • Git:4.7.1
  • Configuration-as-code:1.51
  • Google-oauth-plugin:latest
  • Google-source-plugin:latest
  • Google-storage-plugin:latest

This will allow Jenkins to connect to your cluster and your GCP project.

Download the custom values file:

gsutil cp gs://spls/gsp330/values.yaml jenkins/values.yaml

Use the Helm CLI to deploy the chart with your configuration settings:

helm install cd jenkins/jenkins -f jenkins/values.yaml --wait

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

kubectl get pods

Configure the Jenkins service account to be able to deploy to the cluster:

kubectl create clusterrolebinding jenkins-deploy --clusterrole=cluster-admin --serviceaccount=default:cd-jenkins

Setup port forwarding to the Jenkins UI from the 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 &

Check that the Jenkins Service was created properly:

kubectl get svc

We are using the Kubernetes Plugin so that our builder nodes will be automatically launched as necessary when the Jenkins master requests them. Upon completion of their work, they will automatically be turned down and their resources added back to the clusters resource pool.

Notice that this service exposes ports 8080 and 50000 for any pods that match the selector. This will expose the Jenkins web UI and builder/agent registration ports within the Kubernetes cluster. Additionally, the jenkins-ui services are exposed using a ClusterIP so that it is not accessible from outside the cluster.

 

Connect to Jenkins

The Jenkins chart will automatically create an admin password for you. To retrieve it, run:

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

You should now be able to log in with username admin and your auto-generated password.

 

Conclusion

Awesome, your Jenkins is now up and running to play around!



Leave a Reply

Your email address will not be published. Required fields are marked *

*