k8s之hostPath存储卷

一、简介

  • hostPath:用于将目录从工作节点的文件系统挂载到pod中。
  • 数据的生命周期与节点相同。

我们知道,虽然hostPath卷实现pod中数据存储到节点的文件系统中,但是pod的调度不是固定的,也就是当pod消失后deployment重新创建一个pod,而这pod如果不是被调度到之前pod的节点,那么该pod就不能访问之前的数据。

二、配置参数

path:指定工作节点上的目录路径,必选字段
type:指定节点之上存储类型

  DirectoryOrCreate       # 目录存在就使用,不存在就先创建再使用
  Directory               # 目录必须存在
  FileOrCreate            # 文件存在就使用,不存在就先创建再使用
  File                    # 文件必须存在
  Socket                  # unix套接字必须存在
  CharDevice              # 字符设备必须存在
  BlockDevice             # 块设备必须存在

三、创建存储卷

root@k8s-master01:~/learning-k8s/examples/volumes# cat pod-with-hostpath-vol.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis:7-alpine
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: redisdata
      mountPath: /data
  volumes:
  - name: redisdata
    hostPath:
      type: DirectoryOrCreate
      path: /data/redis
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl get pod 
NAME                     READY   STATUS    RESTARTS   AGE
redis                    1/1     Running   0          4m10s

四、删除容器验证数据持久性

1、进入redis容器创建测试数据

root@k8s-master01:~/learning-k8s/examples/volumes# kubectl exec -it redis /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/data # 
/data # redis-cli 
127.0.0.1:6379> SET site testdate
OK
127.0.0.1:6379> BGSAVE
Background saving started
127.0.0.1:6379> exit
/data # ls
dump.rdb
/data # 

2、删除容器,验证数据持久性
容器第一次创建被调度到k8s-node01节点上

root@k8s-master01:~/learning-k8s/examples/volumes# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES
redis                    1/1     Running   0          29m     10.244.2.26   k8s-node01   <none>           <none>

删除重新创建重新调度到k8s-node01上,,并且数据还存在。

root@k8s-master01:~/learning-k8s/examples/volumes# kubectl delete pod redis
pod "redis" deleted

root@k8s-master01:~/learning-k8s/examples/volumes# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES
redis                    1/1     Running   0          4s      10.244.2.27   k8s-node01   <none>           <none>

root@k8s-master01:~/learning-k8s/examples/volumes# kubectl exec -it redis /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/data # redis-cli 
127.0.0.1:6379> KEYS *
1) "site"
127.0.0.1:6379> exit
/data # ls
dump.rdb
/data # 

3、人为把容器调度到k8s-node02 上,通过nodeName: k8s-node02 设置,验证数据是否存在。

root@k8s-master01:~/learning-k8s/examples/volumes# cat pod-with-hostpath-vol.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  nodeName: k8s-node02 
  containers:
  - name: redis
    image: redis:7-alpine
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: redisdata
      mountPath: /data
  volumes:
  - name: redisdata
    hostPath:
      type: DirectoryOrCreate
      path: /data/redis
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl apply -f pod-with-hostpath-vol.yaml 
pod/redis created
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl get pod -o wide  -w
NAME                     READY   STATUS              RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES
redis                    1/1     Running             0          8m49s   10.244.1.3    k8s-node02   <none>           <none>

root@k8s-master01:~/learning-k8s/examples/volumes# kubectl exec -it redis /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/data # redis-cli 
127.0.0.1:6379> KEYS *
(empty array)
127.0.0.1:6379> exit
/data # ls

以上可以看到k8s-node02没有数据,由此可以说明,hostPath只会在第一次创建的节点数据持久。

posted @ 2024-01-22 22:45  *一炁化三清*  阅读(782)  评论(0编辑  收藏  举报