Hasitha Chandula
Hasitha Chandula

Reputation: 435

use preexisting volume to deployment in kubernetes

I'm trying to add preexisting volume to one of my deployment to use persistent data to jenkins. I'm using hetzner cloud as cloud provider and also using sci drivers to point the preexisting volume. But I'm getting below error,

enter image description here

this is my volume.yml file

apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins-pv
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 10Gi
  claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: jenkins-pvc
    namespace: development
  csi:
    driver: csi.hetzner.cloud
    fsType: ext4
    volumeHandle: "111111"
    readOnly: false
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: csi.hetzner.cloud/location
              operator: In
              values:
                - hel1
  persistentVolumeReclaimPolicy: Retain
  storageClassName: hcloud-volumes
  volumeMode: Filesystem

this is my deployment file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: development
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins-server
  template:
    metadata:
      labels:
        app: jenkins-server
    spec:
      imagePullSecrets:
        - name: regcred
      securityContext:
        allowPrivilegeEscalation: true
        privileged: true
        readOnlyRootFilesystem: false
        runAsUser: 0
      serviceAccountName: jenkins
      containers:
        - name: jenkins
          image: jenkins/jenkins:lts-jdk11
          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-pv
              mountPath: /var/jenkins_home
      volumes:
        - name: jenkins-pv
          persistentVolumeClaim:
            claimName: jenkins-pv

is there any way to fix this?

Upvotes: 2

Views: 627

Answers (2)

kkopczak
kkopczak

Reputation: 862

I have found this similar question.

Your error:

Events:
  Type      Reason              Age                 From                Message
  ----      ------              ----                ----                ------
  Warning   FailedScheduling    18s (x8 over 5m26s) default-scheduler   0/4 nodes are available: 4 persistentvolumeclaim "jenkins-pv" not found
persistentvolumeclaim "jenkins-volume-claim" not found.

says that you're missing PersistentVolumeClaim named jenkins-pv.

Here is an example how to create one:

kubectl -n <namespace> create -f - <<EOF
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-pv
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem
  resources:
    requests:
      storage: 10Gi
EOF

In case you have more that one PV available, you should use selector(s). In this documentation one can find how to do so. Using this the claim will bind to the desired pre-created PV with proper capacity use selector.

See also this and this questions.

Upvotes: 1

dcardozo
dcardozo

Reputation: 71

You must refer your persistentVolumeClaim and not your volume. If you have a persistentVolumeClaim named: jenkins-pvc as stated on your PV object, you must edit your claimName as:

...
volumes:
  - name: jenkins-pv
    persistentVolumeClaim:
    claimName: jenkins-pvc

Upvotes: 0

Related Questions