Ukraine needs your help!

Support
Series: Kubernetes based Jenkins

How to Install Jenkins to Kubernetes?

Kubernetes, Tools

Jenkins is a CI/CD tool that allows us to build pipelines for delivering our code to different environments with quality assurance.

Bellow, I want to describe the way of using Jenkins which I use for my pet projects including this blog. In case you want to build the same development environment for yourself keep reading this guide.

Prerequisites

In this guide I don’t want to talk about how to build own Kubernetes cluster using dedicated server. Because, I am using own dedicated server and manage own Kubernetes cluster if you want to see the detail guide on this blog let me know into the comments.

You have to install Kubectl CLI for managing the Kubernetes cluster.

You could try to use one of following ways:

All of these ways allow you to set up your own cluster in the cloud in one click.

Also, we will use Helm as a package manager for Kubernetes. Follow the installation guide to start using it.

Preparation

First of all, we have to create a new Kubernetes namespace in which we will install Jenkins and additional containers for our pipelines which will be created by Jenkins automatically.

kubectl create namespace jenkins

The last step of this preparation is to create a Persistent Volume Claim which allows allocating memory for Jenkins use. For my purposes of use I need 10-15Gb may you will need more or less.

// pvc.yaml file
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: jenkins-pvc
  namespace: jenkins
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem
  resources:
    requests:
      storage: 10Gi

Execute the following command to create a Persistent Volume Claim:

kubectl apply -f pvc.yaml

Do not forget, that Kubectl CLI requires kubeconfig file, which allows access to your cluster.

Setting Up Jenkins

Now, we are ready to setup Jenkins to our cluster. Let’s add the new Helm Repository from where we will install Jenkins.

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

Next, we need to create values.yaml which allows to override default properties of Jenkins Helm Chart. Pay attention, persistence.existingClaim is a name created earlier PVC.

persistence:
  existingClaim: jenkins-pvc
master:
  serviceType: ClusterIP
  ingress:
    enabled: true
    annotations:
      kubernetes.io/ingress.class: nginx
      cert-manager.io/cluster-issuer: issuer-letsencrypt
      nginx.ingress.kubernetes.io/rewrite-target: /
    hostName: jenkins.myserver.com
    tls:
      - secretName: jenkins.myserver.com
        hosts:
          - jenkins.myserver.com

Also, I overrode the annotations and TLS data which will generate certificates automatically and assign the hostname for Jenkins. To be able to do the same you have to configure Let’s Encrypt to your cluster.

Time to install Jenkins Helm Chart:

helm install jenkins -f values.yaml jenkins/jenkins --namespace jenkins

Additionally, Jenkins creates Pods with needed containers for our pipelines, to be able to do that, we have to assign the role cluster-admin:

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

Now, if you are using Kubernetes Plugin when you will perform a new build, Kubernetes automatically will create a Pod with predefined containers for this build and terminate it after.

Follow this series to configure the Kubernetes Plugin.

Comments

Alex BarchukBlog about programming 🚀bcode.dev © 2022