✦ For everyone, free.

Practical knowledge for real and everyday life

Home

Kubernetes Namespace Storage Organization

Kubernetes Namespace Storage Organization manages storage across namespaces, ensuring efficient and isolated data handling in Kubernetes.

Kubernetes Namespace Storage Organization is the set of conventions for scoping PersistentVolumeClaim, StorageClass usage, and volume-related quota to namespaces so that storage consumption, access boundaries, and data ownership stay aligned with the teams and applications responsible for them.


Namespace Scoping of Storage Objects

PersistentVolumeClaims Are Namespaced

A PersistentVolumeClaim (PVC) belongs to exactly one namespace, and a pod can only bind to a PVC within its own namespace — there is no native mechanism for a pod in one namespace to consume a PVC created in another. This constraint is the foundation of namespace storage organization: it forces every data-owning workload's storage claims to live alongside the workload itself.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: cache-data
  namespace: checkout-prod
spec:
  accessModes: ["ReadWriteOnce"]
  storageClassName: fast-ssd
  resources:
    requests:
      storage: 20Gi

PersistentVolumes Remain Cluster-Scoped

PersistentVolume (PV) objects, unlike PVCs, are cluster-scoped and not namespaced. Organizing storage well therefore means treating PVs as infrastructure-team-managed resources (often provisioned dynamically) while treating PVCs as the namespace-local, application-team-managed interface to that storage.

StorageClass as a Shared, Cluster-Wide Contract

StorageClass objects are also cluster-scoped, meaning namespace organization does not create separate storage tiers by itself — instead, naming and documentation conventions (fast-ssd, standard-hdd, archive) communicate which storage classes are appropriate for which namespace's workloads, and admission policy can restrict which classes a given namespace is permitted to request.


Quota and Capacity Governance per Namespace

Storage Requests in ResourceQuota

ResourceQuota can bound the total storage requested across all PVCs in a namespace, as well as the count of PVCs, preventing a single namespace from exhausting a shared storage backend's capacity.

apiVersion: v1
kind: ResourceQuota
metadata:
  name: storage-quota
  namespace: checkout-prod
spec:
  hard:
    requests.storage: 500Gi
    persistentvolumeclaims: "20"
    fast-ssd.storageclass.storage.k8s.io/requests.storage: 200Gi

Per-StorageClass Quota Scoping

ResourceQuota supports scoping storage limits to a specific StorageClass by name, which lets a namespace be permitted generous quota on inexpensive storage while being tightly capped on a scarce, high-performance tier.

Capacity Planning Across Namespaces

Because PVCs are namespace-scoped but often draw from a shared underlying storage pool (a cloud block storage service, a Ceph cluster), capacity planning requires aggregating PVC requests across every namespace, typically through a periodic report rather than a single Kubernetes object.

kubectl get pvc --all-namespaces -o json \
  | jq -r '.items[] | [.metadata.namespace, .spec.resources.requests.storage] | @tsv'

Access Control for Storage Resources

RBAC on PersistentVolumeClaims

Because PVCs are namespaced, standard Role and RoleBinding objects govern who may create, modify, or delete them within a namespace, allowing a platform team to grant application teams self-service PVC creation while restricting access to the cluster-scoped PersistentVolume and StorageClass objects.

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: pvc-manager
  namespace: checkout-prod
rules:
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "create", "delete"]

Preventing Cross-Namespace Volume Reuse

Because a PV that is Released from one namespace's PVC is not automatically available to another namespace without administrator intervention (clearing the claimRef or provisioning a new volume), namespace storage organization inherently prevents accidental data leakage between namespaces through volume reuse — a deliberate safety property of the model rather than a limitation.

Secret-Backed Storage Credentials

Where a StorageClass provisioner requires credentials (an external NFS server, a cloud storage account), those credentials are stored as namespace-scoped Secret objects referenced by the StorageClass parameters or by a CSI driver's namespace-scoped configuration, keeping credential access aligned with namespace RBAC.


StatefulSet Storage Patterns

volumeClaimTemplates and Per-Pod PVCs

A StatefulSet's volumeClaimTemplates field generates one PVC per replica, each named with a predictable pattern (<template-name>-<statefulset-name>-<ordinal>), all created within the StatefulSet's namespace, which keeps the entire storage footprint of a stateful application enumerable from within one namespace.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: database
  namespace: checkout-prod
spec:
  serviceName: database
  replicas: 3
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: fast-ssd
        resources:
          requests:
            storage: 100Gi

Retained PVCs After Scale-Down

By default, PVCs created through volumeClaimTemplates are not deleted when a StatefulSet is scaled down, meaning storage organization practices must include an explicit cleanup or retention policy per namespace to avoid orphaned claims silently consuming quota.

PersistentVolumeClaimRetentionPolicy

The persistentVolumeClaimRetentionPolicy field on a StatefulSet lets a namespace's operators declare, per workload, whether PVCs should be deleted or retained on scale-down and on StatefulSet deletion, making storage lifecycle an explicit, reviewable part of the workload manifest rather than an implicit cluster default.

spec:
  persistentVolumeClaimRetentionPolicy:
    whenDeleted: Retain
    whenScaled: Delete

Backup, Migration, and Data Lifecycle

Namespace-Scoped Backup Tooling

Backup solutions (such as Velero) commonly select resources for backup using a namespace filter, which means well-organized namespace-to-application mapping directly determines how granular and how efficient a backup or restore operation can be.

velero backup create checkout-prod-backup --include-namespaces checkout-prod

Cross-Namespace Data Migration

Moving a stateful workload's data between namespaces (during a reorganization or a tenant split) requires an explicit data copy — via a backup-and-restore tool, a volume snapshot restored into a new PVC, or an application-level export/import — since Kubernetes provides no native cross-namespace PVC move operation.

Volume Snapshots per Namespace

VolumeSnapshot objects, like PVCs, are namespaced, and a snapshot can only be restored into a new PVC within the same namespace it was taken in, reinforcing that storage lifecycle operations are designed to respect namespace boundaries end to end.

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: cache-data-snapshot
  namespace: checkout-prod
spec:
  volumeSnapshotClassName: csi-snapclass
  source:
    persistentVolumeClaimName: cache-data