K8S学习笔记——创建pv
在K8S上使用存储的时候,需要创建Persistence Volumes(持久化卷)用于持久化数据,否则当pod重启后,数据将会丢失,可以参考官方文档:
https://kubernetes.io/zh-cn/docs/concepts/storage/persistent-volumes/
常用的持久卷类型有local( 节点上挂载的本地存储设备),rbd(依赖Ceph,RBD 卷只能由单个使用者以读写模式安装。不允许同时写入),cephfs(依赖Ceph,同一 cephfs
卷可同时被多个写者挂载)等
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
卷作为替代)
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
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: local-storage provisioner: kubernetes.io/no-provisioner volumeBindingMode: WaitForFirstConsumer reclaimPolicy: Retain
创建local sc
kubectl apply -f local-storage.yaml
查看创建的sc
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
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
kubectl apply -f kafka-local-pv.yaml
查看创建的kafka pv
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
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
kubectl apply -f zookeeper-local-pv.yaml
查看创建的zookeeper pv
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
本文只发表于博客园和tonglin0325的博客,作者:tonglin0325,转载请注明原文链接:https://www.cnblogs.com/tonglin0325/p/4675068.html