Kubernetes PersistentVolumes PersistentVolumeClaims and Examples
Figure: PersistentVolumes, like cluster Nodes, do not belong to any namespace, unlike Pods and PersistentVolumeClaims
Access Modes
A PersistentVolume can be mounted on a host in any way supported by the resource provider.
The access modes are:
- ReadWriteOnce – the volume can be mounted as read-write by a single node
- ReadOnlyMany – the volume can be mounted read-only by many nodes
- ReadWriteMany – the volume can be mounted as read-write by many nodes
Schedule
A PV can specify node affinity to define constraints that limit what nodes this volume can be accessed from. Pods that use a PV will only be scheduled to nodes that are selected by the node affinity.
pv
aep-pv-volume.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
name: aep-pv-volume
labels:
type: local
spec:
capacity:
storage: 4Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/pv/"
创建PV
kubectl apply -f aep-pv-volume.yaml
pvc
aep-pv-claim.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: aep-pv-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 4Gi
创建
kubectl apply -f aep-pv-claim.yaml
pod using pvc
kind: Pod
apiVersion: v1
metadata:
name: aep-pv-pod
spec:
volumes:
- name: aep-pv-storage
persistentVolumeClaim:
claimName: aep-pv-claim
containers:
- name: aep-pv-container
image: zhixingheyitian/sparkthrift:0425
volumeMounts:
- mountPath: "/mnt/pem1"
name: aep-pv-storage
创建
kubectl apply -f aep-pv-pod.yaml
查看结果信息
aep-pv-claim
# kubectl describe pvc aep-pv-claim
Name: aep-pv-claim
Namespace: default
StorageClass:
Status: Bound
Volume: aep-pv-volume
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"PersistentVolumeClaim","metadata":{"annotations":{},"name":"aep-pv-claim","namespace":"default"},"spec":{"acces...
pv.kubernetes.io/bind-completed: yes
pv.kubernetes.io/bound-by-controller: yes
Finalizers: [kubernetes.io/pvc-protection]
Capacity: 4Gi
Access Modes: RWO
VolumeMode: Filesystem
Events: <none>
Mounted By: aep-pv-pod
aep-pv-pod
# kubectl describe pod aep-pv-pod
Name: aep-pv-pod
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: sr535/10.1.2.135
Start Time: Tue, 14 May 2019 17:02:51 +0800
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"aep-pv-pod","namespace":"default"},"spec":{"containers":[{"image":"zh...
Status: Running
IP: 10.32.0.10
Containers:
aep-pv-container:
Container ID: docker://2361d808acfbdd7d33f5612f3bf4a54b441164e7c93c7e5e4c16076cf00b421f
Image: zhixingheyitian/sparkthrift:0425
Image ID: docker-pullable://zhixingheyitian/sparkthrift@sha256:8695d876dc3bff9806302fb04f3469e29c8dab957b439d4326bcaff804b2abb8
Port: <none>
Host Port: <none>
State: Running
Started: Tue, 14 May 2019 17:02:52 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/mnt/pem1 from aep-pv-storage (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-qqr9h (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
aep-pv-storage:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName: aep-pv-claim
ReadOnly: false
default-token-qqr9h:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-qqr9h
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events: <none>