Deploy Jenkins in Kubernetes cluster

8/10/2019

I'm begginer in Kubernetes and trying to deploy Jenkins in a cluster, but the pod is getting pending.

I need jenkins with access to kubernetes, including access to docker and kubectl commands, for continuos integration with my microservices.

With this example yaml file, I can start a jenkins instance on a local machine (my notebook) through Minikube.

But now I'm trying to use a cloud cluster as part of my field of study.

I accept suggestions for improvements.

As I said: I just want to upload an instance of jenkins, with which I can continually integrate my micro services.

These are my configs and logs.

What is my mistake?

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: jenkins-rbac
subjects:
  - kind: ServiceAccount
    name: default
    namespace: default
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io
---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: jenkins
  labels:
    type: local
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/jenkins/"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: jenkins-claim
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
---
apiVersion: v1
kind: Service
metadata:
  name: jenkins
  labels:
    app: jenkins
spec:
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 32256
  selector:
    app: jenkins
    tier: jenkins
  type: NodePort
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: jenkins
  labels:
    app: jenkins
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: jenkins
        tier: jenkins
    spec:
      containers:
      - image: sammubr/jenkins
        name: jenkins
        securityContext:
          privileged: true
        ports:
        - containerPort: 8080
          name: jenkins
        volumeMounts:
        - name: jenkins-persistent-storage
          mountPath: /var/jenkins_home
        - name: docker
          mountPath: /var/run/docker.sock
      volumes:
      - name: docker
        hostPath:
          path: /var/run/docker.sock
      - name: jenkins-persistent-storage
        persistentVolumeClaim:
          claimName: jenkins-claim

Then kubectl --context do-sfo2-teste-cluster apply -f jenkins.yaml

But is allways pending:

samuel@samuel-Inspiron-5548:~/Documentos/teste/jenkins$ kubectl get all
NAME                           READY   STATUS    RESTARTS   AGE
pod/jenkins-5dc7fbd78d-9wxfl   0/1     Pending   0          8m34s

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/jenkins      NodePort    10.245.30.47   <none>        80:32256/TCP   8m34s
service/kubernetes   ClusterIP   10.245.0.1     <none>        443/TCP        79m

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/jenkins   0/1     1            0           8m35s

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/jenkins-5dc7fbd78d   1         1         0       8m35s
samuel@samuel-Inspiron-5548:~/Documentos/teste/jenkins$ kubectl describe pod/jenkins-5dc7fbd78d-9wxfl
Name:               jenkins-5dc7fbd78d-9wxfl
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               <none>
Labels:             app=jenkins
                    pod-template-hash=5dc7fbd78d
                    tier=jenkins
Annotations:        <none>
Status:             Pending
IP:                 
Controlled By:      ReplicaSet/jenkins-5dc7fbd78d
Containers:
  jenkins:
    Image:        sammubr/jenkins
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:
      /var/jenkins_home from jenkins-persistent-storage (rw)
      /var/run/docker.sock from docker (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-5wdgs (ro)
Conditions:
  Type           Status
  PodScheduled   False 
Volumes:
  docker:
    Type:          HostPath (bare host directory volume)
    Path:          /var/run/docker.sock
    HostPathType:  
  jenkins-persistent-storage:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  jenkins-claim
    ReadOnly:   false
  default-token-5wdgs:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-5wdgs
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason            Age               From               Message
  ----     ------            ----              ----               -------
  Warning  FailedScheduling  7s (x8 over 10m)  default-scheduler  pod has unbound immediate PersistentVolumeClaims (repeated 2 times)
-- sammubr
continuous-deployment
continuous-integration
docker
jenkins
kubernetes

2 Answers

8/20/2019

Remove quotation marks in PersistentVolume definition file in line:

 hostPath:
    path: "/data/jenkins/"

Correct file should look like:

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: jenkins-rbac
subjects:
  - kind: ServiceAccount
    name: default
    namespace: default
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io
---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: jenkins
  labels:
    type: local
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data/jenkins/
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: jenkins-claim
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
---
apiVersion: v1
kind: Service
metadata:
  name: jenkins
  labels:
    app: jenkins
spec:
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 32256
  selector:
    app: jenkins
    tier: jenkins
  type: NodePort
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: jenkins
  labels:
    app: jenkins
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: jenkins
        tier: jenkins
    spec:
      containers:
      - image: sammubr/jenkins
        name: jenkins
        securityContext:
          privileged: true
        ports:
        - containerPort: 8080
          name: jenkins
        volumeMounts:
        - name: jenkins-persistent-storage
          mountPath: /var/jenkins_home
        - name: docker
          mountPath: /var/run/docker.sock
      volumes:
      - name: docker
        hostPath:
          path: /var/run/docker.sock
      - name: jenkins-persistent-storage
        persistentVolumeClaim:
          claimName: jenkins-claim

Apply changes;

 $ kubectl apply -f your-config-file.yaml

More information about PersistentVolumes and PersistentVolumeClaims you can find here: persistent-volume.

-- MaggieO
Source: StackOverflow

9/5/2019

As @ortomala-lokni mentioned earlier, you are having an issue with unbound PersistentVolumeClaims to the declared before PersitanceVolume, which causes eventually the Jenkins Pod unable to start up.

In the shared link by @ortomala-lokni to the similar question on SO, one can read about various reasons of this error, and how to fix them.

In your particular case the mismatch between what PVC's demands and PV provisioned actually on your cluster, is in accessModes (ReadWriteOnce vs. ReadWriteMany).

To fix your problem please update the 'PersistentVolumeClaim' definition accordingly, should look like this:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: jenkins-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

Note:
You don't need 'ReadWriteMany' access mode with single replica of Jenkins Pod. This is because with current definition of your 'jenkins' Deployment, under the hood the Deployment controller creates a ReplicaSet object, which by default ensures that only single instance of Jenkins Pod is running (= simplifying only single instance of Jenkins server will be writing to this volume at once).

Please find here another tutorial on setting up Jenkins on Kubernetes from scratch, using helm (recommended way).

-- Nepomucen
Source: StackOverflow