Search code examples
kubernetesnamespacespersistent-volumes

Why a Kubernetes persistent volume does not have namespace?


Context:
Initially, I had a set of k8s files. Resources from the files were deployed and worked well in namespace 'namespace1'.
Then I tried to install another copy of all k8s infrastructure to namespace 'namespace2' putting everything I had to a new Helm chart.
The Helm chart values.yaml file set namespace of every resource to 'namespace2' because all k8s yaml files were converted by me to Helm template files and amended the way all resources to have namespace: {{ .Values.namespace }}.
Helm failed telling me that I already have a persistent volume in namespace "".
Trying to understand what's wrong I looked at persistent volumes using

kubectl get -n namespace1 pv
kubectl get -n namespace2 pv
kubectl get -n "" pv
kubectl get -n '' pv
kubectl describe -n namespace1 pv
kubectl describe -n namespace2 pv
kubectl describe -n "" pv
kubectl describe -n '' pv

and I noticed that I always see the same two my existing persistent volumes that were deployed initially to namespace 'namespace1'.
At least I thought that I deployed them to that namespace.
I checked the YAML file defining the persistent volumes and made sure the volumes had namespace 'namespace1' properly configured. Then I tried

kubectl get -A pv

and got

NAMESPACE   NAME                                CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                          STORAGECLASS   REASON   AGE
            persistentvolume/resdb-pv-volume    100Mi      RWO            Retain           Bound    some-stuff/resdb-pv-claim    respv                   27d
            persistentvolume/sondb-pv-volume    100Mi      RWO            Retain           Bound    some-stuff/sondb-pv-claim    sonpv                   27d

Question:
Why don't persistent volumes have a namespace displayed in the output of kubectl get?
Nor is namespace field displayed in the output of kubectl describe. There's no Namespace: field in the output altogether.
Don't persistent volumes in general have a namespace they belong to?
Is it a known glitch/bug of Minikube 1.32.0 or underlying Kubernetes 1.28.3 I am using?


Solution

  • Persistent volumes (PVs) in Kubernetes are cluster-wide resources, meaning they do not belong to any specific namespace. This is why when you check for PVs using kubectl, you don't see a namespace listed for them.

    Unlike PVs, Persistent Volume Claims (PVCs) are tied to specific namespaces. So, while PVCs must be in a particular namespace, the PVs they claim can be accessed from any namespace across the entire cluster.

    This design allows PVs to be shared and accessed by PVCs from different namespaces if necessary. The behavior you're seeing is normal and not a glitch or bug in Minikube or Kubernetes.