Skip to main content

Running Jenkins inside a Kubernetes-Cluster

·3 mins

This post is a tutorial on how to setup a Jenkins instance running on Kubernetes.

There is also a previous tutorial on how to setup a Kubernetes cluster with K3s.

Jenkins Installation #

As a very first step, a namespace is being created. All Jenkins related resources will be applied to it:

kubectl create namespace jenkins

A persistent volume claim is also required in a file called jenkins-pvc.yaml:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: jenkins-claim
  namespace: jenkins
spec:
  storageClassName: local-path
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

This is applied with:

kubectl create -f jenkins-pvc.yaml -n jenkins

The deployment has to be added to a the file jenkins-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
      - name: jenkins
        image: jenkins/jenkins:lts
        ports:
          - name: http-port
            containerPort: 8080
          - name: jnlp-port
            containerPort: 50000
        volumeMounts:
          - name: jenkins-vol
            mountPath: /var/jenkins_vol
      volumes:
        - name: jenkins-vol
          persistentVolumeClaim:
            claimName: jenkins-claim

The deployment is applied with:

kubectl create -f jenkins-deployment.yaml -n jenkins

Now, there should be one pod running the Jenkins instance. This can be checked with:

kubectl get pods -n jenkins

In a file jenkins-service.yaml two service will be added, one for the Jenkins instance itself and another one for JNLP which is needed for the agent nodes:

apiVersion: v1
kind: Service
metadata:
  name: jenkins
spec:
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 30000
  selector:
    app: jenkins

---

apiVersion: v1
kind: Service
metadata:
  name: jenkins-jnlp
spec:
  type: ClusterIP
  ports:
    - port: 50000
      targetPort: 50000
  selector:
    app: jenkins

Both services will be applied with:

kubectl create -f jenkins-service.yaml --namespace jenkins

Also here it is good to check if everything worked:

kubectl get services --namespace jenkins

To access the Jenkins instance the IP of the node and the post-number of the NodePort-service is needed. The nodes IP-address is:

kubectl get nodes -o wide

Then, to get the port of the NodePort-service:

kubectl get svc -n jenkins

Finally, the running Jenkins instance:

IMAGE 00-jenkins

Jenkins Configuration #

Now, that the instance is up and running it needs to run through some configuration steps.

In order to begin, the initial password needs to be retrieved. It can be found in the logs of the pod.

The name of the pod can be determined by:

kubectl get pods -n jenkins

Then, to access the logs, the following command needs to be executed:

kubectl logs -n jenkins <jenkins-pod-name>

The password that is shown in the logs needs to be copied into the browser in order to continue with the “Continue”-button.

IMAGE 01-jenkins

In the next step some plugins need to be installed by pressing “Install suggested plugins”.

IMAGE 02-jenkins

All suggested plugins are installed automatically.

IMAGE 03-jenkins

To continue as admin with the initial password, the “Skip and continue as admin”-button needs to be pressed.

IMAGE 04-jenkins

Jenkins provides the complete Jenkins URL.

Please note, that this is not the safe-route to take, but enough for this tutorial. In case this should be a permanent Jenkins instance, of course the admins password has to be changed as soon as possible.

IMAGE 05-jenkins

After pressing “Start using Jenkins”, the initial configuration is finished.

IMAGE 06-jenkins

Final note #

In case to restart all over again, everything can be removed be deleting the namespace:

kubectl delete all --all -n jenkins

Resources #