一、简介
- local能够作为PV使用的本地存储卷。
- local卷插件用于将本地存储设备(如磁盘、分区或目录) 配置为卷。
- hostPath卷在Pod被重建后可能被调试至其它节点而无法再次使用此前的数据,而基于local卷,调度器能自行完成调度绑定。
- 基于local的PV,需要管理员通过nodeAffinit声明其定义在的节点。
- local 卷只能用作静态创建的持久卷。不支持动态配置。
二、创建local PV
1、创建存储类
- 配置PVC绑定local PV时,通常要创建个StorageClass。
root@k8s-master01:~/learning-k8s/examples/volumes/local-pv-demo# cat storageclass-local.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: local
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
root@k8s-master01:~/learning-k8s/examples/volumes/local-pv-demo# kubectl apply -f storageclass-local.yaml
storageclass.storage.k8s.io/local created
root@k8s-master01:~/learning-k8s/examples/volumes/local-pv-demo# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
local kubernetes.io/no-provisioner Delete WaitForFirstConsumer false 4s
2、创建率属于存储类的PV
- 进入节点1准备本地磁盘目录,作为local pv的存储卷
root@k8s-node01:~# mkdir /disks/vol1 -pv
mkdir: created directory '/disks'
mkdir: created directory '/disks/vol1'
root@k8s-node01:~# ls /disks/vol1/
- 创建PV
root@k8s-master01:~/learning-k8s/examples/volumes/local-pv-demo# cat local-pv-demo.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: local-pv-demo
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Delete
storageClassName: local
local:
path: /disks/vol1 #将本地磁盘作为目录,事先存在。
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname #匹配的节点标签
operator: In
values:
- k8s-node01 #PV绑定的节点名称
root@k8s-master01:~/learning-k8s/examples/volumes/local-pv-demo# kubectl apply -f local-pv-demo.yaml
persistentvolume/local-pv-demo created
root@k8s-master01:~/learning-k8s/examples/volumes/local-pv-demo# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
local-pv-demo 5Gi RWO Delete Available local 4s
3、创建PVC
- 由于PVC是创建的延迟绑定的,所以创建完成后处于Pending 状态
root@k8s-master01:~/learning-k8s/examples/volumes/local-pv-demo# cat pvc-localpv-demo.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-localpv-demo
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: local
root@k8s-master01:~/learning-k8s/examples/volumes/local-pv-demo# kubectl apply -f pvc-localpv-demo.yaml
persistentvolumeclaim/pvc-localpv-demo created
root@k8s-master01:~/learning-k8s/examples/volumes/local-pv-demo# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-mysql-0 Bound pvc-7785640d-623d-4dd6-90ff-0ce026254733 10Gi RWO openebs-hostpath 5d13h
pvc-demo Bound pv-nfs-demo 5Gi RWX 96m
pvc-localpv-demo Pending local 6s
三、Pod消费PVC
- 由于可消费的PVC只在节点1,所以Pod被调度到节点1
root@k8s-master01:~/learning-k8s/examples/volumes/local-pv-demo# cat pod-with-localpv.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-with-localpv
spec:
containers:
- name: redis
image: redis:7-alpine
ports:
- containerPort: 6379
name: redis
volumeMounts:
- mountPath: "/data"
name: data
volumes:
- name: data
persistentVolumeClaim:
claimName: pvc-localpv-demo
root@k8s-master01:~/learning-k8s/examples/volumes/local-pv-demo# kubectl apply -f pod-with-localpv.yaml
pod/pod-with-localpv created
root@k8s-master01:~/learning-k8s/examples/volumes/local-pv-demo# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-with-localpv 1/1 Running 0 22s 10.244.2.32 k8s-node01 <none> <none>
- PVC也被Bound
root@k8s-master01:~/learning-k8s/examples/volumes/local-pv-demo# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-mysql-0 Bound pvc-7785640d-623d-4dd6-90ff-0ce026254733 10Gi RWO openebs-hostpath 5d13h
pvc-demo Bound pv-nfs-demo 5Gi RWX 104m
pvc-localpv-demo Bound local-pv-demo 5Gi RWO local 7m45s
标签:pv,root,volumes,demo,PV,k8s,local
From: https://www.cnblogs.com/OpenSourceSite/p/17985999