PersistentVolumeClaim in a namespace does not connect to a PersistentVolume

9/7/2018

My PersistentVolumeClaim will not use the PersistentVolume I have prepared for it.

I have this PersistentVolume in monitoring-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: monitoring-volume
  labels:
    usage: monitoring
spec:
  capacity:
    storage: 50Gi
  accessModes:
  - ReadWriteOnce
  hostPath:
    path: /data/k8data/monitoring

After I have done

kubectl apply -f monitoring-pv.yaml 

I can check that it exists with kubectl get pv

NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS        CLAIM                                     STORAGECLASS   REASON    AGE
monitoring-volume                          50Gi       RWO            Retain           Available                                                                        5m

My PersistentVolumeClaim in monitoring-pvc.yaml looks like this:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: monitoring-claim
  namespace: monitoring
spec:
  storageClassName: manual
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi
  selector:
    matchLabels:
      usage: monitoring    

When I do kubectl apply -f monitoring-pvc.yaml it gets created.

I can look at my new PersistentVolumeClaim with get pvc -n monitoringand I see

NAME                           STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
monitoring-claim               Pending                                                                        manual         31s

When I look at my PersistentVolume with kubectl get pv I can see that it's still available:

NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS        CLAIM                                     STORAGECLASS   REASON    AGE
monitoring-volume                          50Gi       RWO            Retain           Available                                                                        16m

I had expected the PersistentVolume to be Boundbut it isn't. When I use a ´PersistentVolumeClaim´ with the same name as this, a new PersistentVolumeClaim is created that is written in /tmp and therefore not very persistent.

When I do the same operations without a namespace for my PersistentVolumeClaim everything seems to work.

I'm on minikube on a Ubuntu 18.04.

What do I need to change to be able to connect the volume with the claim?

-- Thomas Sundberg
kubernetes

2 Answers

4/17/2020

Persistent Volume and Volume Claim should in same namespace. You need to add namespace: monitoring. now you can try this below code

for Persistent Volume

monitoring-pv.yml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: monitoring-volume
  labels:
    usage: monitoring
  namespace: monitoring 
spec:
  capacity:
    storage: 50Gi
  accessModes:
  - ReadWriteOnce
  hostPath:
    path: /data/k8data/monitoring

for Persistent volume claim

monitoring-pvc.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: monitoring-claim
  namespace: monitoring
spec:
  storageClassName: manual
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi
  selector:
    matchLabels:
      usage: monitoring
-- Suresh
Source: StackOverflow

9/7/2018

When I reviewed my question and compared it to a working solution, I noticed that I had missed storageClassName that was set to manual in an example without a namespace that I was able to use.

My updated PersistentVolumenow looks like this:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: monitoring-volume
  labels:
    usage: monitoring
spec:
  storageClassName: manual
  capacity:
    storage: 50Gi
  accessModes:
  - ReadWriteOnce
  hostPath:
    path: /data/k8data/monitoring

The only difference is

  storageClassName: manual

My preliminary findings is that this was the silly mistake I had done.

-- Thomas Sundberg
Source: StackOverflow