Installing Jenkins on a Kubernetes Cluster A Step-by-Step Guide

Are you looking to install Jenkins on a Kubernetes cluster? This guide provides a detailed walkthrough using YAML manifest files, offering direct control and customization over your Jenkins deployment. This approach enhances visibility into the deployment process.

Prerequisites

Before starting, make sure you have the following:

  • A running Kubernetes cluster (local or cloud-based).

  • kubectl CLI tool installed and configured.

  • Basic understanding of Kubernetes concepts.

  • Cluster administrator privileges.

Installing Jenkins with YAML Manifests

Step 1: Create a Namespace for Jenkins

First, create a dedicated namespace to organize Jenkins resources:

kubectl create namespace jenkins

Step 2: Create a Service Account and RBAC Configuration

Jenkins requires specific permissions to interact with the Kubernetes cluster. Set up the necessary RBAC configuration by creating a jenkins-sa.yaml file:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: jenkins-admin
rules:
  - apiGroups: [""]
    resources: ["*"]
    verbs: ["*"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins-admin
  namespace: jenkins
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: jenkins-admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: jenkins-admin
subjects:
- kind: ServiceAccount
  name: jenkins-admin
  namespace: jenkins

Step 3: Apply the RBAC Configuration

Apply the jenkins-sa.yaml file using:

kubectl apply -f jenkins-sa.yaml

Step 4: Create Persistent Volume

Jenkins needs persistent storage for its data. Create a PV and PVC using the following jenkins-volume.yaml file:

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins-pv-volume
  labels:
    type: local
spec:
  storageClassName: local-storage
  claimRef:
    name: jenkins-pv-claim
    namespace: jenkins
  capacity:
    storage: 30Gi
  accessModes:
    - ReadWriteOnce
  local:
    path: /mnt
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - worker01
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-pv-claim
  namespace: jenkins
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

Step 5: Apply the Volume Configuration

Apply the jenkins-volume.yaml file using:

kubectl apply -f jenkins-volume.yaml

Step 6: Verify PV and PVC Status

Verify the status of PV and PVC using:

kubectl get pv,pvc -n jenkins

Step 7: Install Jenkins on Kubernetes Cluster

Create the Jenkins deployment manifest file (jenkins-deployment.yaml) with these configurations:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins-server
  template:
    metadata:
      labels:
        app: jenkins-server
    spec:
      securityContext:
          fsGroup: 1000
          runAsUser: 1000
      serviceAccountName: jenkins-admin
      containers:
        - name: jenkins
          image: jenkins/jenkins:lts
          resources:
            limits:
              memory: "2Gi"
              cpu: "2"
            requests:
              memory: "1Gi"
              cpu: "1"
          ports:
            - name: web-port
              containerPort: 8080
            - name: agent-port
              containerPort: 50000
          livenessProbe:
            httpGet:
              path: "/login"
              port: 8080
            initialDelaySeconds: 90
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 5
          readinessProbe:
            httpGet:
              path: "/login"
              port: 8080
            initialDelaySeconds: 60
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          volumeMounts:
            - name: jenkins-data
              mountPath: /var/jenkins_home
      volumes:
        - name: jenkins-data
          persistentVolumeClaim:
            claimName: jenkins-pv-claim

Step 8: Apply the Jenkins Deployment

Apply the jenkins-deployment.yaml file:

kubectl apply -f jenkins-deployment.yaml

Step 9: Verify Deployment and Pod Status

Verify the status of the deployment and pods:

kubectl get deployment -n jenkins
kubectl get pods -n jenkins

Step 10: Create Jenkins Service

Expose the Jenkins deployment using a NodePort service. Create a jenkins-service.yaml file:

apiVersion: v1
kind: Service
metadata:
  name: jenkins-service
  namespace: jenkins
  annotations:
      prometheus.io/scrape: 'true'
      prometheus.io/path:   /
      prometheus.io/port:   '8080'
spec:
  selector:
    app: jenkins-server
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 32000

Step 11: Apply the Jenkins Service

Apply the jenkins-service.yaml file:

kubectl apply -f jenkins-service.yaml

Step 12: Access Jenkins Web UI

Access the Jenkins dashboard by navigating to any worker node IP on port 32000:

http://<worker-node-ip>:32000

Step 13: Retrieve Initial Admin Password

On the first login, Jenkins will prompt for the initial Admin password. Get the pod name and then execute the command in the pod:

kubectl get pods -n jenkins
kubectl exec -it <Jenkins-pod-name> cat /var/jenkins_home/secrets/initialAdminPassword -n Jenkins

Step 14: Enter Admin Password and Configure Jenkins

Copy the password and paste it in the administrator password field, then click Continue. Choose Install suggested plugins. Create your first admin user as per your setup, then click on Save and Continue.

Step 15: Confirm Jenkins URL

Confirm your Jenkins URL, which is the worker node IP and node port (32000). Click on Save and Finish, then click on Start using Jenkins.

Step 16: Test Jenkins Installation

To test the Jenkins installation, create a demo job to execute a shell command.

Installing Jenkins with Helm

Helm is a popular package manager for Kubernetes, simplifying the deployment and management of applications. Here’s how you can use Helm to install Jenkins:

Step 1: Add the Jenkins Helm Repository

Add the Jenkins Helm repository to your local Helm client:

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

Step 2: Install Jenkins Using Helm

Install Jenkins using the Helm chart:

helm install jenkins jenkins/jenkins -n jenkins

Step 3: Access Jenkins Web UI

After installation, follow the Helm chart’s instructions to access the Jenkins web UI, usually involving port forwarding or exposing a service.


With these steps, you should have a fully functional Jenkins instance running on your Kubernetes cluster. Feel free to adapt these instructions to fit your specific environment and needs.