/

Kubernetes 入门实战 Part3

24 PersistentVolume 数据持久化

PersistentVolume 属于集群的系统资源,是和 Node 平级的一种对象,Pod 对它没有管理权,只有使用权。

StorageClass 它抽象了特定类型的存储系统(比如 Ceph、NFS),在 PVC 和 PV 之间充当“协调人”的角色,帮助 PVC 找到合适的 PV。

# vim host-path-pv-pvc.yml
apiVersion: v1
kind: PersistentVolume
metadata:
# 只有 10MB 容量的存储设备
name: host-10m-pv
spec:
# kubectl explain PersistentVolume.spec.storageClassName
storageClassName:
host-vol
# kubectl explain PersistentVolume.spec.accessModes
accessModes:
# ReadWriteOnce:存储卷可读可写,但只能被一个节点上的 Pod 挂载。
# ReadOnlyMany:存储卷只读不可写,可以被任意节点上的 Pod 多次挂载。
# ReadWriteMany:存储卷可读可写,也可以被任意节点上的 Pod 多次挂载。
- ReadWriteOnce
capacity:
# Ki/Mi/Gi
storage: 10Mi
hostPath:
path: /tmp/host-10m-pv/

---
# PVC 不表示实际的存储,而是一个“申请”或者“声明”
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: host-5m-pvc
spec:
storageClassName: host-vol
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Mi
mkdir /tmp/host-10m-pv
kubectl apply -f host-path-pv-pvc.yml
kubectl get pv
# NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
# host-10m-pv 10Mi RWO Retain Bound default/host-5m-pvc host-vol 4s
kubectl get pvc
# NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
# host-5m-pvc Bound host-10m-pv 10Mi RWO host-vol 6s
# vim host-pvc-pod.yml
apiVersion: v1
kind: Pod
metadata:
name: host-pvc-pod
spec:
volumes:
- name: host-pvc-vol
persistentVolumeClaim:
claimName: host-5m-pvc
containers:
- name: ngx-pvc-pod
image: nginx:alpine
ports:
- containerPort: 80
volumeMounts:
- name: host-pvc-vol
mountPath: /tmp
kubectl apply -f host-pvc-pod.yml
kubectl get pod -o wide
kubectl exec -it host-pvc-pod -- sh
cd /tmp && touch a.md
# check in worker node
/tmp/host-10m-pv/

25 NFS 网络共享存储

References