How To Setup Jenkins On Kubernetes Cluster – Beginners Guide
Setup Jenkins On Kubernetes Cluster
1)Create a Namespace
2)Create a service account with Kubernetes admin permissions.
3)Create local persistent volume for persistent Jenkins data on Pod restarts.
4)Create a deployment YAML and deploy it.
5)Create a service YAML and deploy it.
Jenkins Kubernetes Manifest Files
All Jenkins Kubernetes manifest files used in this blog are hosted on Github. If you have trouble copying the manifest from the blog, please clone the repository.
git clone https://github.com/scriptcamp/kubernetes-jenkins
Use the Github files for reference and follow the steps in the next sections
Kubernetes Jenkins Deployment
Here’s a high-level view of what we’re going to do.
Read Also- Build-Test-Deploy with Kubernetes (CI/CD)
Let’s get started with deploying Jenkins on Kubernetes.
Step 1: Create a Namespace for Jenkins. It is good to categorize all the devops tools as a separate namespace from other applications.
kubectl create namespace devops-tools
Step 2: Create a serviceAccount.yaml file and copy the following administrator service account manifest.
--- 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: devops-tools ---
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:
devops-tools
The serviceAccount.yaml creates a jenkins-admin clusterRole, jenkins-admin ServiceAccount and binds the clusterRole to the service account.
The jenkins-admin cluster role has all permissions to manage cluster components. You can also restrict access by specifying individual resource actions.
Now create the service account using kubectl.
kubectl apply -f serviceAccount.yaml
Step 3: Create volume.yaml and copy the following persistent volume manifest.
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:
devops-tools capacity: storage:
10Gi accessModes: - ReadWriteOnce local:
path: /mnt nodeAffinity: required:
nodeSelectorTerms
: - matchExpressions:
- key: kubernetes.io/hostname operator:
In values: - worker-node01
--- apiVersion: v1 kind: PersistentVolumeClaim
metadata: name:
jenkins-pv-claim namespace:
devops-tools spec: storageClassName:
local-storage accessModes: -
ReadWriteOnce resources:
requests: storage: 3
Accessing Jenkins Using Kubernetes Service
apiVersion: v1
kind: Service
metadata:
name: jenkins-service
namespace: devops-tools
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
Create the Jenkins service using kubectl.
kubectl apply -f service.yaml
Now if you browse to one of the node IPs on port 32000, you will be able to access the Jenkins dashboard.
http://<node-ip>:32000
When you first access the dashboard, Jenkins will ask for an initial admin password.
You can get this from the pod log, from the Kubernetes dashboard or from the CLI. You can get pod details using the following CLI commands.
kubectl get pods --namespace=devops-tools
And with the pod name, you can get the logs as shown below. replace the pod name with your pod name.
kubectl logs jenkins-deployment-2539456353-j00w5 --namespace=jenkins
The password can be found at the end of the log as shown below.
Alternatively, you can run the exec command to get the password directly from the location as shown below.
kubectl exec -it jenkins-559d8cd85c-cfcgk cat /var/jenkins_home/secrets/initialAdminPassword -n devops-tools
Once you enter the password you can proceed to install the sugested plugin and create an admin user. All these steps are self-explanatory from the jenkins dashboard.
Hope you like this blog….
- VLSI Engineer - October 30, 2024
- AnchorSetup using Docker-Compose - October 18, 2024
- Devops assignment - October 17, 2024