instruction: Show me how to configure jenkins for CI/CD automation
output: This guide doesn’t use local persistent volume as this is a generic guide.
  For using persistent volume for your Jenkins data, you need to create volumes of relevant cloud or on-prem data center and configure it.
  git clone https://github.com/scriptcamp/kubernetes-jenkins
  kubectl create namespace devops-tools
  --- 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
  kubectl apply -f jenkins-01-serviceAccount.yaml
  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 : 3Gi
  kubectl get nodes
  kubectl create -f jenkins-02-volume.yaml
  apiVersion : apps/v1 kind : Deployment metadata : name : jenkins namespace : devops-tools spec : replicas : 1 selector : matchLabels : app : jenkins-server strategy : type : Recreate template : metadata : labels : app : jenkins-server spec : securityContext : # Note: fsGroup may be customized for a bit of better # filesystem security on the shared host fsGroup : 1000 runAsUser : 1000 ### runAsGroup: 1000 serviceAccountName : jenkins-admin containers : - name : jenkins image : jenkins/jenkins:lts # OPTIONAL: check for new floating-tag LTS releases whenever the pod is restarted: imagePullPolicy : Always resources : limits : memory : " 2Gi" cpu : " 1000m" requests : memory : " 500Mi" cpu : " 500m" ports : - name : httpport containerPort : 8080 - name : jnlpport 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
  runAsUser=1000
  fsGroup=1000 # Or custom ID, per above
  mkdir -p /var/jenkins_home
  chown -R $runAsUser:$fsGroup /var/jenkins_home
  chmod -R g+rwX /var/jenkins_home
  The deployment file uses local storage class persistent volume for Jenkins data.
  For production use cases, you should add a cloud-specific storage class persistent volume for your Jenkins data.
  volumes : - name : jenkins-data emptyDir : \{}
  kubectl apply -f jenkins-03-deployment.yaml
  kubectl get deployments -n devops-tools
  kubectl describe deployments --namespace = devops-tools
  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
  Here, we are using the type as 'NodePort' which will expose Jenkins on all kubernetes node IPs on port 32000.
  If you have an ingress setup, you can create an ingress rule to access Jenkins.
  Also, you can expose the Jenkins service as a Loadbalancer if you are running the cluster on AWS, Google, or Azure cloud.
  kubectl apply -f jenkins-04-service.yaml
  http://<node-ip>:32000
  kubectl get pods --namespace = devops-tools
  kubectl logs jenkins-deployment-2539456353-j00w5 --namespace = devops-tools
  You can watch the Jenkins server logs (posted to stdout or stderr of the JVM and collected by Kubernetes) with the following loop to handle Jenkins restarts as well: while sleep 0.1 ; do kubectl logs -f -l app=jenkins-server -n devops-tools ; done & (The label jenkins-server is defined in jenkins-03-deployment.yaml ) Alternatively, stern tool can be used to watch multiple pods' logs.
  while sleep 0.1 ; do kubectl logs -f -l app=jenkins-server -n devops-tools ; done &
  kubectl exec -it "deployment.apps/jenkins" cat /var/jenkins_home/secrets/initialAdminPassword -n devops-tools
  kubectl exec -it jenkins-559d8cd85c-cfcgk cat /var/jenkins_home/secrets/initialAdminPassword -n devops-tools
  helm repo add jenkinsci https://charts.jenkins.io
  helm repo update
  helm search repo jenkinsci
  kubectl apply -f jenkins-01-volume.yaml
  It’s worth noting that, in the above spec, hostPath uses the /data/jenkins-volume/ of your node to emulate network-attached storage.
  This approach is only suited for development and testing purposes.
  For production, you should provide a network resource like a Google Compute Engine persistent disk, or an Amazon Elastic Block Store volume.
  Minikube configured for hostPath sets the permissions on /data to the root account only. Once the volume is created you will need to manually change the permissions to allow the jenkins account to write its data. minikube ssh sudo chown -R 1000:1000 /data/jenkins-volume
  minikube ssh sudo chown -R 1000:1000 /data/jenkins-volume
  kubectl apply -f jenkins-02-sa.yaml
  storageClass : jenkins-pv
  serviceAccount : create : false # Service account name is autogenerated by default name : jenkins annotations : {}
  Where `name: jenkins` refers to the serviceAccount created for jenkins.
  chart = jenkinsci/jenkins
  helm install jenkins -n jenkins -f jenkins-values.yaml $chart
  This outputs something similar to the following:
  NAME: jenkins
  LAST DEPLOYED: Wed Sep 16 11:13:10 2020
  NAMESPACE: jenkins
  STATUS: deployed
  REVISION: 1
  jsonpath = "{.data.jenkins-admin-password}" secret = $( kubectl get secret -n jenkins jenkins -o jsonpath = $jsonpath ) echo $( echo $secret | base64 --decode )
  jsonpath = "{.spec.ports[0].nodePort}" NODE_PORT = $( kubectl get -n jenkins -o jsonpath = $jsonpath services jenkins ) jsonpath = "{.items[0].status.addresses[0].address}" NODE_IP = $( kubectl get nodes -n jenkins -o jsonpath = $jsonpath ) echo http:// $NODE_IP : $NODE_PORT /login
  kubectl get pods -n jenkins
  kubectl get pods -n jenkins
  NAME READY STATUS RESTARTS AGE
  jenkins-645fbf58d6-6xfvj 1/1 Running 0 2m
  jsonpath = "{.data.jenkins-admin-password}" secret = $( kubectl get secret -n jenkins jenkins -o jsonpath = $jsonpath ) echo $( echo $secret | base64 --decode )
  Um1kJLOWQY
  👆🏻Note that your password will be different.
  jsonpath = "{.data.jenkins-admin-password}" kubectl get secret -n jenkins jenkins -o jsonpath = $jsonpath
  WkIwRkdnbDZYZg==
  kubectl get pods -n jenkins
  kubectl -n jenkins port-forward <pod_name> 8080:8080
  Forwarding from 127.0.0.1:8080 -> 8080
  Forwarding from [ ::1]:8080 -> 8080
  kubectl create -f jenkinsHelm-03-deployment.yaml -n jenkins
  kubectl get deployments -n jenkins
  kubectl create -f jenkinsHelm-04-service.yaml -n jenkins
  kubectl get services -n jenkins
  NAME TYPE CLUSTER-IP EXTERNAL-IP PORT ( S ) AGE
  jenkins NodePort 10.103.31.217 <none> 8080:32664/TCP 59s
  minikube ip
  192.168.99.100
  kubectl get pods -n jenkins
  kubectl logs <pod_name> -n jenkins
  *************************************************************
  *************************************************************
  *************************************************************
  
  Jenkins initial setup is required.
  An admin user has been created and a password generated.
  Please use the following password to proceed to installation:
  
  94b73ef6578c4b4692a157f768b2cfef
  
  This may also be found at:
  /var/jenkins_home/secrets/initialAdminPassword
  
  *************************************************************
  *************************************************************
  *************************************************************
  If you are not sure what plugins you need, choose Install suggested
  plugins .
  You can install (or remove) additional Jenkins plugins at a later point in time
  via the Manage Jenkins > Plugins page in Jenkins.