在K8s中创建本地存储PV
在本地演练K8s,我们不一定需要分布式存储,只需一个本地存储就可以满足我们的需求。但是,本地存储不能自动分配持久化卷(PV),需要我们先创建好PV,PVC才能获取到资源。
1. 创建一个本地存储类
# local-storage.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: local-storage
annotations:
storageclass.kubernetes.io/is-default-class: "true"
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
kubectl apply -f local-storage.yaml
其中的storageclass.kubernetes.io/is-default-class: "true"
表示该类为默认存储类型。在实际环境中,我们一般都给PVC指定存储类型,避免数据存储到哪都难找。
2. 创建PV
sudo mkdir /mnt/logging-es-data-0
# es-local-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: logging-es-data-0
labels:
type: logging-es-data
spec:
capacity:
storage: 5Gi
# volumeMode field requires BlockVolume Alpha feature gate to be enabled.
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage
local:
path: /mnt/logging-es-data-0
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- k8s-single
kubectl apply -f es-local-pv.yaml
3. PVC中使用PV
如下为 StatefulSets 中的PVC模板。
volumeClaimTemplate:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "local-storage"
selector:
matchLabels:
type: "logging-es-data"
resources:
requests:
storage: 2Gi
PVC会通过selector去选择满足资源要求的PV。