Deploy Jenkins on Kubernestes Cluster with Persistent Volume

Introduction

Jenkins is a free and open source automation server which is know for building, testing, and deploying software applications.

Kubernetes is a free and open source container management tool, we can deploy and manage containerized applications across a cluster of nodes. It provides features such as automated rollouts and rollbacks, self-healing, load balancing, and scaling, all of which make it easier to manage and scale containerized applications.

In this post, We will show you how to deploy Jenkins on Kubernetes cluster.

Step 1: Deploy Storage

We need to a storage to make jenkins data persistence by using given command..

nano jenkins-storage.yaml

Paste the following storage configuration.

kind: PersistentVolume
apiVersion: v1
metadata:
  name: jenkins-pv-volume
  labels:
    type: local
    app: jenkins
spec:
  storageClassName: manual
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/jenkins_data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: jenkins-pv-claim
  labels:
    app: jenkins
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Gi

We can update the k8s node mounting path and storage capicity in real env and save and edit from the nano editor.

To deploy the storage.

kubectl create -f jenkins-storage.yaml

To validate the volume.

kubectl get pvc

Step 2: Deploy Jenkins

We need to create the jenkins deployment yaml config file and where we can update the replica count and jenkins image version and other things.

nano jenkins-deployment.yaml

Paste the following configuration.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
        - name: jenkins
          image: jenkins/jenkins:lts
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 8080
          volumeMounts:
            - mountPath: /var/jenkins_home
              name: jenkins
      volumes:
        - name: jenkins
          persistentVolumeClaim:
            claimName: jenkins-pv-claim

To deploy the jenkins.

kubectl create -f jenkins-deployment.yaml

To validate the deployment.

kubectl get deployment

To valide the pods.

kubectl get pods

Step 3: Deploy Jenkins Service

We need to create the jenkins service yaml config to make accesbale jenkins from network, Use the folllwing command for the same.

nano jenkins-service.yaml

Paste the following configuration.

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

We can replace the port number and network typ as per your need.

To deploy the jenkins service.

kubectl create -f jenkins-service.yaml

To validate the jenkins service.

kubectl get svc

Step 4: Accessing Jenkins

We need to copy the endpoint of jenkins and paste in the brower URL with port 8080.

Get your NodePort IP address or Load Balancer IP / DNS to access your Jenkins in the browser, If everything is okay so then you will get the unlock Jenkins page like this.

To get jenkins initialAdminPassword

To get initialAdminPassword

kubectl logs [pod-id]

After execution of kubectl logs [pod-id] command, You should get the in initialAdminPassword password in the logs after that we need to use in Jenkins server like this.

We have to create our admin user of the Jenkins, We need to put the username, password, name and email id.

We need to save our Jenkins endpoint with Public/Private/Domain along with port number.

Click on Start using Jenkins to get Jenkins dashboard.

Here we can create starting new Jenkins jobs as per our needs.

Conclusion

We have successfully deployed the Jenkins on the K8S cluster. If you are still facing problems, feel free to leave a commit. 

Author

Deploy Jenkins on Kubernestes Cluster with Persistent Volume

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top