tonglin0325的个人主页

K8S学习笔记——创建pv

在K8S上使用存储的时候,需要创建Persistence Volumes(持久化卷)用于持久化数据,否则当pod重启后,数据将会丢失,可以参考官方文档:

1
2
https://kubernetes.io/zh-cn/docs/concepts/storage/persistent-volumes/

常用的持久卷类型有local(
 节点上挂载的本地存储设备),rbd(依赖Ceph,RBD 卷只能由单个使用者以读写模式安装。不允许同时写入),cephfs(依赖Ceph,同一 cephfs 卷可同时被多个写者挂载)等

1
2
https://kubernetes.io/zh-cn/docs/concepts/storage/volumes/

persistent volume,persistent volume claim和storage class的区别可以参考:Kubernetes对象之PersistentVolume,StorageClass和PersistentVolumeClaim

默认的storage class有HostPath,HostPath 卷 (仅供单节点测试使用;不适用于多节点集群; 请尝试使用 local 卷作为替代)

1
2
3
4
kubectl get sc -A
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
standard (default) k8s.io/minikube-hostpath Delete Immediate false 40d

如果要创建一个local的storage class,首先需要编写一个local-storage.yaml

1
2
3
4
5
6
7
8
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
reclaimPolicy: Retain

创建local sc

1
2
kubectl apply -f local-storage.yaml

查看创建的sc

1
2
3
4
5
kubectl get sc -A
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
local-storage kubernetes.io/no-provisioner Retain WaitForFirstConsumer false 2m41s
standard (default) k8s.io/minikube-hostpath Delete Immediate false 41d

给local storage class下面增加一个kafka pv,kafka-local-pv.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: v1
kind: PersistentVolume
metadata:
name: data-kafka-0
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: /tmp/kafka/data-0
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- minikube

创建kafka pv

1
2
kubectl apply -f kafka-local-pv.yaml

查看创建的kafka pv

1
2
3
4
kubectl get pv -A
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
data-kafka-0 5Gi RWO Retain Available local-storage 6s

给local storage class下面增加一个zk pv,zookeeper-local-pv.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: v1
kind: PersistentVolume
metadata:
name: data-zookeeper-0
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: /tmp/zookeeper/data-0
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- master

创建zookeeper pv

1
2
kubectl apply -f zookeeper-local-pv.yaml

查看创建的zookeeper pv

1
2
3
4
5
kubectl get pv,pvc --all-namespaces
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/data-kafka-0 5Gi RWO Retain Available local-storage 12m
persistentvolume/data-zookeeper-0 5Gi RWO Retain Available local-storage 2m5s