Jenkins Setup on Kubernetes

For quick deployment, we can deploy jenkins server thru kubernetes cluster.

We will use the latest jenkins-controller jenkins/jenkins:lts as lts image version.

Do not use in production. If you want a production grade deployment. Kindly, refer to this repository https://github.com/jenkinsci/helm-charts

It is fairly recommended to plan ahead the sustainability of the Jenkins Server deployment. As systems like Jenkins which is not designed to be scalable is highly prone to fail.

  1. Let’s create a namespace.yaml file:
apiVersion: v1
kind: Namespace
metadata:
  name: jenkins-controller

then apply it thru:

kubectl apply -f namespace.yaml
  1. Next, let’s create rbac-and-serviceaccount.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-controller
---
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-controller
  1. Create PersistentVolume and PersistentVolumeClaim named jenkins-pv-and-pvc.yaml:
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins-pv-volume
  labels:
    type: pv
spec:
  claimRef:
    name: jenkins-pv-claim
    namespace: jenkins-controller
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-pv-claim
  namespace: jenkins-controller
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
  1. Create StatefulSet named jenkins-statefulset.yaml:
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: jenkins-server
  namespace: jenkins-controller
spec:
  serviceName: jenkins-server
  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: "1000m"
            requests:
              memory: "500Mi"
              cpu: "500m"
          ports:
            - name: httpport
              containerPort: 8080
            - name: jnlpport
              containerPort: 50000
          startupProbe:
            failureThreshold: 12
            httpGet:
              path: '/login'
              port: http
            periodSeconds: 10
            timeoutSeconds: 5
          livenessProbe:
            failureThreshold: 5
            httpGet:
              path: '/login'
              port: http
            initialDelaySeconds: null
            periodSeconds: 10
            timeoutSeconds: 5
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: '/login'
              port: http
            initialDelaySeconds: null
            periodSeconds: 10
            timeoutSeconds: 5
          volumeMounts:
            - mountPath: /var/jenkins_home
              name: jenkins-home
              readOnly: false
      volumes:
        - name: jenkins-home
          persistentVolumeClaim:
            claimName: jenkins-pv-claim
  1. Expose Jenkins through Load Balancer (Kubernetes Service) named jenkins-service.yaml:
---
apiVersion: v1
kind: Service
metadata:
  name: jenkins-service
  namespace: jenkins-controller
  annotations:
    prometheus.io/scrape: "true"
    prometheus.io/path: /
    prometheus.io/port: "8080"
spec:
  selector:
    app: jenkins-server
  type: LoadBalancer
  ports:
    - port: 8080
      targetPort: 8080
  1. Apply the created resources via:
kubectl apply -f *.yaml

Source Code