Red Hat Training

A Red Hat training course is available for OpenShift Container Platform

27.22.2. 在 Pod 规格中配置 hostPath 卷

您可以使用 hostPath 卷访问节点上的 读写 文件。这对可以从内部配置和监控主机的 pod 很有用。您还可以使用 hostPath 卷使用 mountPropagation 在主机上挂载卷。

警告

使用 hostPath 卷可能会具有危险性,因为它们允许 pod 读取和写入主机上的任何文件。请小心操作。

建议您直接在 Pod 规格中指定 hostPath 卷,而不是在 PersistentVolume 对象中指定。这很有用,因为 pod 已经知道它在配置节点时访问的路径。

流程

  1. 创建特权 pod:

      apiVersion: v1
      kind: Pod
      metadata:
        name: pod-name
      spec:
        containers:
        ...
          securityContext:
            privileged: true
        volumeMounts:
        - mountPath: /host/etc/motd.confg 1
          name: hostpath-privileged
      ...
      volumes:
        - name: hostpath-privileged
          hostPath:
              path: /etc/motd.confg 2
    1
    用于在特权 pod 中挂载 hostPath 共享的路径。
    2
    用于共享特权 pod 的主机上的路径。

在本例中,pod 可以看到 /etc/motd.confg 中作为 /host/etc/motd.confg 的主机路径。因此,可以在不直接访问主机的情况下配置 motd