使用本地存储
更新时间:2023-09-12
emptyDir
emptyDir 类型的 Volume 在 Pod 分配到 Node 上时被创建,Kubernetes 会在 Node 上自动分配一个目录,因此无需指定宿主机 Node 上对应的目录文件,这个目录的初始内容为空。
说明
当 Pod 从 Node 上移除时,emptyDir 中的数据会被永久删除。
示例
创建 Pod,该 Pod 指定数据卷为 emptyDir:
Plain Text
1 apiVersion: v1
2 kind: Pod
3 metadata:
4 name: test-ed
5 spec:
6 containers:
7 - image: nginx
8 name: test-container
9 volumeMounts:
10 - mountPath: /test
11 name: cache-volume
12 volumes:
13 - name: cache-volume
14 emptyDir: {}
查看创建的 Pod 详情:
Plain Text
1 $ kubectl describe pod test-ed
进入 Pod 内部查看挂载效果:
Plain Text
1 $ kubectl exec -it test-ed -c test-container /bin/sh
hostPath
HostPath 类型则是映射 Node 文件系统中的文件或者目录到 Pod 里。在使用 HostPath 类型的存储卷时,也可以设置 type 字段,支持的类型有文件、目录、File、Socket、CharDevice 和 BlockDevice。
示例
创建 Pod,该 Pod 数据卷指定 HostPath:
Plain Text
1 apiVersion: v1
2 kind: Pod
3 metadata:
4 name: test-hp
5 spec:
6 containers:
7 - image: nginx
8 name: test-container
9 volumeMounts:
10 - mountPath: /test-pd
11 name: test-volume
12 volumes:
13 - name: test-volume
14 hostPath:
15 #directory location on host
16 path: /data
Local Volume
Local Volume 允许将 Node 本地的磁盘、分区或者目录作为持久化存储使用。Local Volume 适合应用于数据缓存,应用可以就近访问数据,快速处理,同时还适用于分布式存储系统,如分布式数据库 Cassandra ,分布式文件系统 ceph/gluster。
示例
创建storage class
Plain Text
1 kind: StorageClass
2 apiVersion: storage.k8s.io/v1
3 metadata:
4 name: local-volume
5 provisioner: kubernetes.io/no-provisioner
6 volumeBindingMode: WaitForFirstConsumer
说明
其中WaitForFirstConsumer表示PV不要立即绑定PVC,而是直到有Pod需要用PVC的时候才绑定。
静态创建 PV
静态创建一个 5GiB 的 PV;该 PV 使用 node 的 /data/volume 目录(需要手动创建目录):
Plain Text
1 apiVersion: v1
2 kind: PersistentVolume
3 metadata:
4 name: example-local-pv
5 spec:
6 capacity:
7 storage: 5Gi
8 accessModes:
9 - ReadWriteOnce
10 persistentVolumeReclaimPolicy: Retain
11 storageClassName: local-volume
12 local:
13 path: /data/volume
14 nodeAffinity:
15 required:
16 nodeSelectorTerms:
17 - matchExpressions:
18 - key: kubernetes.io/hostname
19 operator: In
20 values:
21 - HOSTNAME # 替换对应的节点名称
使用 local volume PV
创建 PVC 绑定 local volume 的 PVC,并创建 Pod 使用该 PVC:
- 创建 PVC
Plain Text
1 apiVersion: v1
2 kind: PersistentVolumeClaim
3 metadata:
4 name: mypvc
5 spec:
6 accessModes:
7 - ReadWriteOnce
8 resources:
9 requests:
10 storage: 5Gi
11 storageClassName: local-volume
- 创建 Pod
Plain Text
1 kind: Pod
2 apiVersion: v1
3 metadata:
4 name: mypod
5 spec:
6 containers:
7 - name: myfrontend
8 image: nginx
9 volumeMounts:
10 - mountPath: "/usr/share/nginx/html"
11 name: mypd
12 volumes:
13 - name: mypd
14 persistentVolumeClaim:
15 claimName: mypvc
- 查看创建的 PV、PVC、Pod。
Plain Text
1 $ kubectl get pv
2 NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
3 example-local-pv 5Gi RWO Retain Bound default/mypvc local-volume 5s
4 $ kubectl get pvc
5 NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
6 mypvc Bound example-local-pv 5Gi RWO local-volume 4m20s
7 $ kubectl get pods
8 NAME READY STATUS RESTARTS AGE
9 mypod 1/1 Running 0 4m28s
- 向本地的/data/volume写入内容,使用curl查看是否写入成功
Plain Text
1 $ echo "hello world" > /data/volume/index.html
2 $ curl 172.19.4.63 #这里替换自己的pod的ip
3 hello world
注意
删除时注意首先删除创建的pod,随后删除pvc,pv。