pvc
About 1598 words
Volume Mounting Overview
In Kubernetes, a Volume is shared storage accessible to multiple containers within a Pod. Unlike Docker volumes, Kubernetes volumes have an explicit lifecycle tied to their Pod.
Main Features of Volumes:
- Lifecycle tied to the Pod: When the Pod is deleted, the volume is cleaned up as well.
- Multiple storage types: Local, network, cloud storage, and more.
- Sharing between containers: Multiple containers in a Pod can share the same volume.
PV (PersistentVolume) and PVC (PersistentVolumeClaim)
PV (PersistentVolume)
A PV is a cluster-level storage resource provisioned in advance by an administrator. It represents a piece of storage in the cluster, such as:
- Local storage.
- NFS
- AWS EBS
- GCE Persistent Disk
- Azure Disk, and others.
PVC (PersistentVolumeClaim)
A PVC is a user's request for storage, analogous to a Pod requesting Node resources. Users request storage through PVCs, and Kubernetes finds and binds a suitable PV according to each PVC's requirements.
Relationship Between PVs and PVCs
User creates PVC → Kubernetes matches a PV → Binding → Pod uses PVCPractical Examples
1. Creating a PV
# nfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
server: 192.168.1.100
path: /data/nfs2. Creating a PVC
# nfs-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi3. Using a PVC in a Pod
# pod-with-pvc.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-with-pvc
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: nfs-storage
mountPath: /usr/share/nginx/html
volumes:
- name: nfs-storage
persistentVolumeClaim:
claimName: nfs-pvc4. Local Storage PV Example
# local-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: local-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage
local:
path: /mnt/data
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node1Use Cases for Other Volume Types
1. ConfigMap Volume
Mount ConfigMap data inside a container:
apiVersion: v1
kind: Pod
metadata:
name: configmap-pod
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: config-volume
mountPath: /etc/nginx/conf.d
volumes:
- name: config-volume
configMap:
name: nginx-config2. Secret Volume
Mount sensitive data:
apiVersion: v1
kind: Pod
metadata:
name: secret-pod
spec:
containers:
- name: app
image: myapp
volumeMounts:
- name: secret-volume
mountPath: /etc/secrets
readOnly: true
volumes:
- name: secret-volume
secret:
secretName: my-secret3. EmptyDir Volume
Temporary storage whose data is deleted when the Pod is deleted:
apiVersion: v1
kind: Pod
metadata:
name: emptydir-pod
spec:
containers:
- name: writer
image: busybox
command: ['sh', '-c', 'echo "Hello" > /data/hello.txt; sleep 3600']
volumeMounts:
- name: shared-data
mountPath: /data
- name: reader
image: busybox
command: ['sh', '-c', 'cat /data/hello.txt; sleep 3600']
volumeMounts:
- name: shared-data
mountPath: /data
volumes:
- name: shared-data
emptyDir: {}4. HostPath Volume
Mount a path directly from the host:
apiVersion: v1
kind: Pod
metadata:
name: hostpath-pod
spec:
containers:
- name: app
image: nginx
volumeMounts:
- name: host-storage
mountPath: /usr/share/nginx/html
volumes:
- name: host-storage
hostPath:
path: /data
type: DirectoryOrCreateKey Concepts
AccessModes
- ReadWriteOnce (RWO): Read-write access, mounted by one node only.
- ReadOnlyMany (ROX): Read-only access, mounted by multiple nodes.
- ReadWriteMany (RWX): Read-write access, mounted by multiple nodes.
ReclaimPolicy
- Retain: Keep the data; manual cleanup is required.
- Delete: Automatically delete the PV and its storage.
- Recycle: Deprecated; use Delete instead.
StorageClass
Dynamically creates PVs and provisions storage on demand:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast-ssd
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
fsType: ext4Best Practices
- Use StorageClass in production for dynamic storage provisioning.
- Select an appropriate access mode for the application.
- Set reclaim policies carefully to avoid data loss.
- Use ConfigMaps and Secrets for configuration and sensitive information.
- Avoid HostPath unless you have a specific requirement.
Pod Volumes
Volumes。
Core Concept: Why Do We Need Volumes?
Before discussing volumes, understand one key issue: files inside a container are ephemeral.
- When a container crashes or restarts, its filesystem resets to the initial state of the image.
- Multiple containers in the same Pod have separate, isolated filesystems by default.
Volumes exist to solve these problems by providing:
- Data persistence: Data in a Volume can survive Pod rescheduling or restarts.
- Data sharing between containers in a Pod: Multiple containers in the same Pod can mount one Volume and share its files.
Volume Definition and Lifecycle
- Definition location: Volumes are defined at the Pod level, not the container level, under
spec.volumes. - Mounting: Containers use
spec.containers.volumeMountsto mount a defined Volume at a specific path in their filesystem. - Lifecycle: A Volume's lifecycle is tied to its Pod.
- The Volume exists as long as the Pod exists.
- When the Pod is deleted, its Volumes are generally cleaned up, depending on their type; data in
hostPathand persistent volumes can remain.
A Simple Pod Volume Example
A classic example defines a Pod with two containers: one writes logs and the other reads them. They share data through a Volume.
apiVersion: v1
kind: Pod
metadata:
name: shared-volume-pod
spec:
# 1. Define the Volume at the Pod level
volumes:
- name: shared-log-volume # Volume name
emptyDir: {} # Volume type: an initially empty directory
containers:
- name: app-container
image: busybox
command: ['sh', '-c', 'while true; do echo "$(date): Hello from App!" >> /app-logs/log.txt; sleep 5; done']
# 2. Mount the Volume at a specific path inside the container
volumeMounts:
- name: shared-log-volume # Name of the Volume to mount
mountPath: /app-logs # Mount point inside this container
- name: sidecar-container
image: busybox
command: ['sh', '-c', 'tail -f /sidecar-logs/log.txt']
volumeMounts:
- name: shared-log-volume # Mount the same Volume
mountPath: /sidecar-logs # The mount path can differExplanation:
volumes: Defines anemptyDirvolume namedshared-log-volume.volumeMounts: Both containers reference that volume bynameand mount it at their respective paths,/app-logsand/sidecar-logs.- Result: Files that
app-containerwrites to/app-logs/log.txtare immediately visible tosidecar-containerat/sidecar-logs/log.txt. The data survives container restarts until the Pod is deleted.
Common Volume Types
Kubernetes supports a wide range of Volume types for different storage backends and purposes.
1. emptyDir
- Description: An empty directory created with the Pod. Deleting the Pod permanently deletes the data in
emptyDir. - Uses: Scratch space, disk caching, or file sharing between containers in a Pod.
- Storage medium: Node disk or node memory, configured with
emptyDir: { medium: "Memory" }.
2. hostPath
- Description: Mounts a file or directory from the node host into the Pod.
- Uses: Access host system information such as
/var/lib/docker, or run daemons that require host device access, such as monitoring agents. - Risks: Couples the Pod tightly to its node, complicating scheduling and migration; it may also introduce security risks.
- Example:
volumes: - name: host-volume hostPath: path: /data # Directory on the node type: DirectoryOrCreate # Create the path if it does not exist
3. Cloud and Network Storage Volumes
These are core volume types for persistent data, allowing data to remain accessible even if the Pod is scheduled to another node.
- NFS: Mounts an NFS network filesystem.
- CSI: The Container Storage Interface, the recommended standard approach. It allows third-party storage providers, such as AWS, GCP, Azure, Ceph, and GlusterFS, to integrate storage systems with Kubernetes.
awsElasticBlockStore(EBS)gcePersistentDisk(PD)azureDiskcephfs
- Configuration example (NFS):
volumes: - name: nfs-volume nfs: server: 10.0.0.41 path: /exports/data readOnly: false
4. Special-Purpose Volumes
configMap/secret/downwardAPI- These are not conventional "storage volumes": they inject Kubernetes cluster metadata or configuration into Pods as files.
configMap: Mounts configuration data as files.volumes: - name: app-config configMap: name: my-app-configsecret: Similar to ConfigMap but stores sensitive information, such as passwords and tokens, with content encoded in Base64.downwardAPI: Makes metadata about the Pod or node, such as Pod name, namespace, and labels, readily available to the Pod.
Best Practices and Advanced Patterns: PersistentVolume (PV) and PersistentVolumeClaim (PVC)
In production, avoid defining specific network storage volumes directly in Pod definitions, such as nfs or csi. Prefer the declarative PV/PVC model.
- PersistentVolume (PV): A piece of network storage provisioned in advance by a cluster administrator. Like a node, it is a cluster resource.
- PersistentVolumeClaim (PVC): A user's request for storage, asking the cluster for a PV with a specific capacity and access mode.
Workflow:
- An administrator creates a PV, such as a 100Gi NFS volume.
- A user creates a PVC in the Pod YAML requesting 10Gi of storage.
- Kubernetes finds a suitable PV and binds it to the PVC.
- The user references the PVC in the Pod's
volumes, rather than a specific storage type.
Example:
# 1. Define a PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-app-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
# 2. Reference the PVC in a Pod
apiVersion: v1
kind: Pod
metadata:
name: app-with-pvc
spec:
volumes:
- name: app-storage
persistentVolumeClaim:
claimName: my-app-pvc # Key: references the PVC by name
containers:
- name: app
image: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: app-storageThis approach abstracts and decouples storage. Developers do not need to know how the underlying storage works; they only declare what storage they need.
Summary
| Feature | Description |
|---|---|
| Purpose | Data persistence and sharing data between containers in a Pod. |
| Definition location | Pod-level spec.volumes. |
| Usage | Container-level spec.containers.volumeMounts. |
| Lifecycle | Tied to the Pod; persistent volume data has an independent lifecycle. |
| Common types | emptyDir, hostPath, nfs, csi, configMap, secret. |
| Best practice | Manage persistent storage with PV/PVC in production. |
- PVs are created by administrators or StorageClasses.