mountPath在Kubernetes中应该是绝对的,是吗? [英] The mountPath should be absolute in Kubernetes, is it?

查看:372
本文介绍了mountPath在Kubernetes中应该是绝对的,是吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

花了几个小时后,我发现Kubernetes中不支持将相对路径作为已安装路径.我在这里找到了引用 mountpath应该是绝对的,如果是,那么为什么呢?没有相对路径的功能,有人可以进一步解释吗?

     Example code:
    apiVersion: v1
    kind: Pod
    metadata:
      name: task-pv-pod
    spec:
      volumes:
        - name: task-pv-storage
          hostPath:
            # directory location on host
            # path: "./code" # this is not supporting
            path: "/var/www/html/kubernetes/code"  # this is supporting
            # this field is optional
            type: DirectoryOrCreate
      containers:
        - name: task-pv-container
          image: nginx
          ports:
            - containerPort: 80
              name: "http-server"
          volumeMounts:
            - mountPath: "/usr/share/nginx/html"
              name: task-pv-storage
 

在上面的代码示例的code目录下,我只有一个index.html页面

项目结构的屏幕截图:

如果我使用path: "./code",则错误显示如下:

Error response from daemon: create ./code: "./code" includes invalid
characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.
If you intended to pass a host directory, use absolute path.

提前谢谢!!

解决方案

我相信,造成混乱的原因是您正在运行像Minikube这样的单节点集群.

  • 流程链(大致概括)为:

Kubectl> Kube-API(主站)> Kubelet代理(节点)>创建Pod(在Yaml清单中指定).

在单个节点群集中,所有这些代理都在同一台计算机上,这就是/var/www/html/kubernetes/code中的文件被挂载到Pod的原因.

  • 我将通过以下示例对其进行说明:
    • 您有一个包含3个节点的集群.
    • 您可以使用笔记本中的kubectl远程管理节点.

使用hostPath时,文件必须存在于node而不是笔记本上,因为不是计算机上的kubectl会触发Pod的创建并挂载文件/目录. /p>

这是 kubelet 将创建容器并应用其清单的节点的代理.这就是为什么您需要指定要挂载的文件/目录的完整路径的原因.


根据 PersistentVolumes 文档:

Kubernetes支持hostPath 在单节点群集上进行开发和测试. hostPath PersistentVolume使用节点上的文件或目录来模拟网络附加存储.

在生产集群中,您不会使用hostPath .取而代之的是,集群管理员将提供网络资源,例如Google Compute Engine永久磁盘,NFS共享或Amazon Elastic Block Store卷.群集管理员还可以使用 StorageClasses 设置动态配置.

当使用 hostPath 类型时要小心,因为:

  • 由于节点上的文件不同,具有相同配置的Pod(例如从podTemplate创建的Pod)在不同的节点上的行为可能会有所不同.
  • 当Kubernetes按计划添加资源感知调度时,它将无法考虑hostPath使用的资源.
  • 在基础主机上创建的文件或目录只能由root写入.您需要以root用户身份在特权容器中运行进程,或修改主机上的文件权限,以便能够写入hostPath

如果您有任何问题,请在评论中告诉我.

After spending a couple of hours I found that the relative path is not supported as a mounted path in Kubernetes. I found the reference here mountpath should be absolute if it is yes then why it doesn't have that capability of the relative path could anyone please explain a bit deeper?

    Example code:
    apiVersion: v1
    kind: Pod
    metadata:
      name: task-pv-pod
    spec:
      volumes:
        - name: task-pv-storage
          hostPath:
            # directory location on host
            # path: "./code" # this is not supporting
            path: "/var/www/html/kubernetes/code"  # this is supporting
            # this field is optional
            type: DirectoryOrCreate
      containers:
        - name: task-pv-container
          image: nginx
          ports:
            - containerPort: 80
              name: "http-server"
          volumeMounts:
            - mountPath: "/usr/share/nginx/html"
              name: task-pv-storage

Under the code directory in the above code example, I just have an index.html page

Screenshot of the project structure:

If I use path: "./code" then the error shows like this:

Error response from daemon: create ./code: "./code" includes invalid
characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.
If you intended to pass a host directory, use absolute path.

Thanks in advance!!

解决方案

I believe the source of your confusion is that you are running a single node Cluster, like Minikube.

  • The process chain (roughly summarized) is:

Kubectl > Kube-API(master) > Kubelet agent (node) > Pod creation (as specified on the yaml manifest).

In a single node cluster all these agents are on the same computer, that's why the files in /var/www/html/kubernetes/code were mounted to the pod.

  • I'll clarify it with this example:
    • You have a cluster with 3 nodes.
    • You manage your nodes remotely, with kubectl from your notebook.

When you use hostPath the files must exist on the node, not on your notebook, because it's not the kubectl on your computer that will trigger the creation of the pod and mount the files/directories.

This is the job of the kubelet agent of the node that will create the pod and apply it's manifest. This is why you need to specify the full path of the file/dir you want to mount.


According to PersistentVolumes documentation:

Kubernetes supports hostPath for development and testing on a single-node cluster. A hostPath PersistentVolume uses a file or directory on the Node to emulate network-attached storage.

In a production cluster, you would not use hostPath. Instead a cluster administrator would provision a network resource like a Google Compute Engine persistent disk, an NFS share, or an Amazon Elastic Block Store volume. Cluster administrators can also use StorageClasses to set up dynamic provisioning.

Watch out when using hostPath type, because:

  • Pods with identical configuration (such as created from a podTemplate) may behave differently on different nodes due to different files on the nodes.
  • when Kubernetes adds resource-aware scheduling, as is planned, it will not be able to account for resources used by a hostPath.
  • the files or directories created on the underlying hosts are only writable by root. You either need to run your process as root in a privileged Container or modify the file permissions on the host to be able to write to a hostPath volume

If you have any question let me know in the comments.

这篇关于mountPath在Kubernetes中应该是绝对的,是吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆