Kubernetes hostPath 存储卷
  AWkq7aIjuRwO 2023年11月02日 48 0

hostPath存储卷

hostPath存储卷的方式,pod挂载的目录是node节点的机器上的某一个目录,这种方式使用的也不多,因为不能保证你未来在创建pod的时候,pod创建还是在原有的node节点上,这种方式通常有两种方式,一种是基于节点选择器,还一种方式就是daemopset 他会在每个节点都会创建一个pod,一般这种方式才会用到hostPath其他方式deployment rs创建出来的pod,用hostPath的方式比较少,因为你保证不了更新的时候还会到你原有的node节点上,这就是hostPath的一个缺点.只有daemoSet作为一个守护进程的方式的pod控制器用hostPath比较多,其他比较少

看下字段

[root@k8s-master1 secret]# kubectl explain pod.spec.volumes.hostPath
KIND:     Pod
VERSION:  v1

RESOURCE: hostPath <Object>

DESCRIPTION:
     hostPath represents a pre-existing file or directory on the host machine
     that is directly exposed to the container. This is generally used for
     system agents or other privileged things that are allowed to see the host
     machine. Most containers will NOT need this. More info:
     https://kubernetes.io/docs/concepts/storage/volumes#hostpath

     Represents a host path mapped into a pod. Host path volumes do not support
     ownership management or SELinux relabeling.

FIELDS:
   path <string> -required-
   #路径 那个目录
     path of the directory on the host. If the path is a symlink, it will follow
     the link to the real path. More info:
     https://kubernetes.io/docs/concepts/storage/volumes#hostpath

   type <string>
   #他的类型 下面解释一下
     type for HostPath Volume Defaults to "" More info:
     https://kubernetes.io/docs/concepts/storage/volumes#hostpath

配置 hostPath 存储卷的嵌套字段有两个∶

一个用于指定工作节点上的目录路径的必选字段 path;另一个用于指定节点之上存储类型的 type。hostPath 支持使用的节点存储类型有如下几种

DirectoryOrCreate∶指定的路径不存在时,自动将其创建为 0755 权限的空目录,属主和属组均为 kubelet。

Directory∶ 事先必须存在的目录路径。

FileOrCreate∶ 指定的路径不存在时,自动将其创建为 0644 权限的空文件,属主和属组均为kubelet。

File∶ 事先必须存在的文件路径 不存在不行 必须存在

Socket∶ 事先必须存在的 Socket 文件路径。 如果你是socket文件必须得存在

CharDevice∶事先必须存在的字符设备文件路径。 字符设备 文件系统类型里面 有普通文件二进制文件,字符设备文件 socket文件 还有块设备文件 ,这是Linux操作系统上的各种各样的文件类型我们可以看下

-rw-------. 1 root root     1336 7月  12 14:49 anaconda-ks.cfg
crw-rw---- 1 root video    10, 175 8月  17 11:00 agpgart
#以c开头的是字符串设备文件  
drwxr-xr-x 2 root root         180 8月  17 11:00 block
#d开头的是块设备
lrwxrwxrwx 1 root root           4 8月  17 11:00 rtc -> rtc0
#l是链接

BlockDevice;事先必须存在的块设备文件路径。

""∶ 空字符串,默认配置,在关联 hostPath 存储卷之前不进行任何检查。

这类pod控制器适合daemoSet控制器,因为你不管未来再怎么更新未来它必然有一个daemoSet这种控制器,每个节点都会有,相当于每个节点目录都可以用得上,所以持久化和文件共享的方式,都利用上了

apiVersion: v1
kind: Pod
metadata:
  name: hostpath
spec:
  volumes:
  - name: index  #起个名字 containers要引用
    hostPath:  #hostPath 存储卷
      path: /apps/data/html/  #宿主机提供的可挂载目录
  - name: logs #再来一个
    hostPath:
      path: /apps/data/logs/  #另一个提供挂载的目录
  containers:
  - name: hostpath-nginx
    image: images.guoguo.com/apps/nginx:1.22.1
    volumeMounts:
    - name: index #使用上面起的名字
      mountPath: /apps/nginx/html/ #挂载到容器的一个目录里面
    - name: logs
      mountPath: /apps/nginx/logs/  #挂载到容器里面的一个目录
    ports:
    - containerPort: 80

创建后查看他存在的节点在哪里 然后去node节点查看路径

[root@k8s-master1 hostPath]# kubectl get pods -owide
NAME                                READY   STATUS    RESTARTS        AGE    IP               NODE                 
hostpath                            1/1     Running   0               15m    192.26.131.162   k8s-node1.guoguo.com

去node1节点查看宿主机提供的可挂载的两个目录

[root@k8s-node1 html]# pwd
/apps/data/html
[root@k8s-node1 html]# echo "node-1" > index.html

然后用命令行访问一下pod

[root@k8s-node1 html]# curl 192.26.131.162
node-1

这样就成功了

再看下node1提供挂载的目录日志有没有变化

[root@k8s-node1 html]# cd /apps/data/logs/
[root@k8s-node1 logs]# ll
总用量 8
-rw-r--r-- 1 root root 86 8月  17 16:22 access.log
-rw-r--r-- 1 root root  0 8月  17 16:05 error.log
-rw-r--r-- 1 root root  2 8月  17 16:05 nginx.pid
[root@k8s-node1 logs]# tail -f access.log
172.17.8.11 - - [17/Aug/2023:08:22:51 +0000] "GET / HTTP/1.1" 200 7 "-" "curl/7.29.0"

如果说没有在node节点创建这个目录,会自动创建

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论