- 持久性卷(PV) 是由管理员提供的网络存储。它是集群中的资源,独立于pod。
- 持久卷声明(PVC) pod所需存储空间的声明,用户不需要知道底层配置,声明必须在创建pod的相同命名空间中创建。
创建持久卷
pv-volume.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: task-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
配置文件指定卷位于集群节点上的/mnt/data
。该配置还指定了10 gibibytes的大小和读写一次的访问模式,这意味着卷可以由单个节点以读写方式挂载。
它为PersistentVolume定义了StorageClass名称手册,用于将PersistentVolumeClaim请求绑定到这个PersistentVolume。
创建PersistentVolume:
kubectl apply -f ./pv-volume.yaml
查看PV
查看pv列表
kubectl get pv
查看pv详情
kubectl describe pv task-pv-volume
创建PersistentVolumeClaim
pv-claim.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: task-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
创建PersistentVolume:
kubectl apply -f ./pv-claim.yaml
查看pvc
查看pvc列表
kubectl get pvc
查看pvc详情
kubectl describe pvc task-pv-claim
Pod 使用 pv 与 pvc
pv-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: task-pv-pod
spec:
volumes:
- name: task-pv-storage
persistentVolumeClaim:
claimName: task-pv-claim
containers:
- name: task-pv-container
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: task-pv-storage
注意,Pod的配置文件指定了一个PersistentVolumeClaim,但没有指定一个PersistentVolume。
创建pod
kubectl apply -f ./pv-pod.yaml