K8s引入了一组叫作Persistent Volume Claim(PVC)和Persistent Volume(PV) 的API对象,大大降低了用户声明和使用持久化Volume的门槛
在Pod的Volumes中,只要声明类型是persistentVolumeClaim,指定PVC的名字,当创建这个PVC对象,k8s就自动为它绑定一个符合条件的Volume,这个Volume,从PV来
PVC和PV的设计,类似“接口”和“实现”的思想,开发者只知道使用“接口”PVC,运维人员负责给“接口”绑定具体的实现PV,说白了PVC就是一种特殊的Volume
一、PV使用示例
apiVersion: v1
kind: PersistentVolume
metadata:
name: cqh
labels:
cqh: chenqionghe
spec:
capacity:
storage: 100Mi
volumeMode: Filesystem
accessModes: ["ReadWriteMany"]
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage
local:
path: /data/pv
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: cqh
operator: In
values:
- chenqionghe
创建后查看
root@VM-0-8-ubuntu:/home/ubuntu/statefulset# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
cqh 100Mi RWX Delete Bound default/cqh local-storage 4m
二、PVC使用示例
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: cqh
spec:
storageClassName: local-storage
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Mi
selector:
matchLabels:
cqh: chenqionghe
AccessModes为ReadWriteMany,表示这个Volume是可读写的,并且能挂载在多个节点上(官方支持的AccessMode)
执行创建后查看
root@VM-0-8-ubuntu:/home/ubuntu/statefulset# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
cqh Bound cqh 100Mi RWX local-storage 4m
三、Pod使用pvc示例
apiVersion: v1
kind: Pod
metadata:
name: cqh
spec:
containers:
- name: cqh-container
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: pv-storage
volumes:
- name: pv-storage
persistentVolumeClaim:
claimName: cqh
创建后查看
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
cqh 1/1 Running 0 4m 10.244.0.46 vm-0-8-ubuntu <none>
root@VM-0-8-ubuntu:/home/ubuntu/statefulset# kubectl describe po cqh
Name: cqh
Namespace: default
...
Volumes:
pv-storage:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName: cqh
ReadOnly: false
default-token-gqfrx:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-gqfrx
Optional: false
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 4m default-scheduler Successfully assigned default/cqh to vm-0-8-ubuntu
Normal Pulling 4m kubelet, vm-0-8-ubuntu pulling image "nginx"
Normal Pulled 4m kubelet, vm-0-8-ubuntu Successfully pulled image "nginx"
Normal Created 4m kubelet, vm-0-8-ubuntu Created container
Normal Started 4m kubelet, vm-0-8-ubuntu Started container