In this tutorial, I will walk through the steps to deploy Jenkins on Google Kubernetes Engine (GKE).

Prerequisites

Before starting this tutorial, you should have:

  • A Google Cloud Platform account
  • A GKE cluster created
  • Basic knowledge of Jenkins CI/CD, Kubernetes and Helm

Introduce Jenkins

Jenkins is an open-source automation server that helps automate various aspects of software development, including building, testing, and deployment. It enables teams to automate their software development process by integrating with a wide range of tools and technologies. Jenkins provides a simple and extensible way to create and manage Continuous Integration and Continuous Delivery pipelines, making it a popular choice for DevOps teams.

Jenkins logo

Common Jenkins deployment strategy

Running Jenkins on a single server/virtual machine

Pros of running Jenkins on a single server:

  • Easy to set up and maintain.
  • Minimal resource usage, which means it can run on low-end hardware. Easier to manage access controls and security measures.
  • Less complexity in managing multiple servers.

Cons of running Jenkins on a single server:

  • Limited scalability - it can only - handle a certain amount of workload before it starts to slow down or crash.
  • Single point of failure - if the server goes down, the entire Jenkins instance is inaccessible.
  • Increased security risks - if the server is compromised, it can lead to a complete loss of data.
  • Limited redundancy - there are no backups or replicas to fall back on in case of failure.

Running Jenkins on a Kubernetes cluster

Pros of running Jenkins on a Kubernetes cluster:

  • Scalability: Jenkins can be easily scaled up or down based on the workload by increasing or decreasing the number of worker nodes in the cluster.
  • High availability: Kubernetes provides features like automatic node replacement and rolling updates, which ensure that Jenkins remains available and running even during hardware failures or software updates. Resource efficiency: Jenkins can make use of Kubernetes features like resource quotas and resource limits to efficiently utilize cluster resources and avoid resource contention.
  • Easy deployment: Kubernetes allows for easy deployment and management of Jenkins using YAML manifests, making it easy to set up and configure.

Cons of running Jenkins on a Kubernetes cluster:

  • Complexity: Setting up Jenkins on a Kubernetes cluster can be complex, especially for those who are new to Kubernetes.
  • Additional overhead: Running Jenkins on Kubernetes adds additional overhead in terms of managing the cluster, which may not be necessary for smaller environments.
  • Learning curve: Using Kubernetes requires a certain level of knowledge and understanding of container orchestration, which may be a barrier for some users.

In this tutorial, I will go through the steps to deploy Jenkins on a GKE cluster using Helm.

Deployment guideline

Create a GKE cluster and reserve a static external IP address

You will need to create a GKE cluster, there is a detail guideline here.

GKE

And reserve a static external IP address following the guideline here.

Public IP

Create a Helm value.yml

Helm value.yml is for Jenkins Helm chart to apply the right configuration for Jenkins instance on GKE. Here is an example:

controller:
    adminUser: admin
    adminPassword: admin
    jenkinsUrl: https://example.com/ci/
    jenkinsUriPrefix: /ci

You can find more configuration here

Install Jenkins using Helm chart

Add Jenkins Helm repo

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

Install Jenkins Helm chart with the created value.yml

helm install jenkins jenkins/jenkins -f values.yml

Create an Ingress manifest to make the Jenkins publicly accessible

For this deployment, an Ingress is required to make the Jenkins publicly accessible, and a Managed Certificate object from GCP to enable HTTPS secure access.

apiVersion: networking.gke.io/v1
kind: ManagedCertificate
metadata:
  name: jenkins-tls
spec:
  domains:
    - example.com
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: jenkins-ingress
  annotations:
    kubernetes.io/ingress.class: "gce"
    kubernetes.io/ingress.allow-http: "false"
    networking.gke.io/managed-certificates: jenkins-tls
    kubernetes.io/ingress.global-static-ip-name: "jenkins-public-ip"
spec:
  rules:
    - http:
        paths:
          - path: /jenkins/*
            pathType: Exact
            backend:
              service:
                name: ci
                port:
                  number: 8080

Update your DNS provider

You will need to update your DNS provider to resolve the host example.com to the jenkins-public-ip above. Then you can access https://example.com/ci to access your Jenkins on GKE cluster.

Create a pipeline job to test your Jenkins on GKE

Here is an example for you to create a pipeline job

pipeline {
  agent {
    kubernetes {
      yaml """
      apiVersion: v1
      kind: Pod
      metadata:
        labels:
          app: test-pipeline
      spec:
        containers:
        - name: test-pipeline
          image: your-registry.com/agent-image:latest
          command: ["sleep"]
          args: ["9999"]
      """
    }
  }
  stages {
    stage('Build job') {
      steps {
        container('test-pipeline') {
            sh 'echo $PATH'
        }
      }
    }
  }
}

Conclusion

In this tutorial, I showed you how to deploy Jenkins on GKE using Helm, configure an Ingress, and test a pipeline job. With Jenkins running on GKE, you can easily automate your CI/CD pipeline and scale as needed.