如何更改持久卷以使用主机上的路径?

How to change a Persistent Volume to use a path on the Host machine?


我完全通过 Web 控制台创建了一个 Persistent Volume Claim 并附加到我的 Web 服务器的 /www/log 安装路径。 我看到 Persistent Volume Claim 就像我重新启动 Pod 一样工作,日志被保留了下来。

不过,我想在主机上为持久卷使用 本地挂载,这样我就可以轻松跟踪日志。 在 OKD Web 控制台上只能创建新的 PVC,不能创建指向本地挂载的 PV。 您能否建议如何更新自动拾取的持久卷以使用本地挂载(例如 /mnt/data)?

这可以通过在命令行中使用 OpenShift 运行 中的 hostPath 选项 Persistent Volume YAML 来解决。但是,如果你想走这条路,有一些事情需要注意。

  1. 路径/mnt/data中创建的所有文件和目录只能由root用户写入。在这种情况下,您必须 运行 一个 特权容器 或给予适当的 权限 到 hostPath 是可写的。

  2. 下面的 YAML 具有持久卷中 hostPath 选项的语法。

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: test-pv
    spec:
      capacity:
        storage: 1Mi
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Recycle
      hostPath:
        path: /mnt/data
    
  3. 运行下面命令在具体项目中创建持久卷。 oc create -f test-pv.yaml -n <project>

  4. 一旦持久卷可用,创建一个持久卷声明边界到上面创建的持久卷音量.

  5. 如果要将 hostPath 直接挂载到 Pod 上,请确保在部署配置中使用 节点选择器 作为 pods 是 短暂的 并且它们可以在任何时间点重新创建。如果带有 hostPath 的 pods 被安排在可能没有 /mnt/data 路径可用的另一台主机上,它可能会导致 CrashLoopBackOff 错误。

  6. 下面的 YAMLPod[=51] 上的 hostPath 的示例=].

    apiVersion: v1
    kind: Pod
    metadata:
      name: test-pod
    spec:
      containers:
      - image: tomcat
        name: test-container
        volumeMounts:
        - mountPath: /www/log
          name: test-volume
      volumes:
      - name: test-volume
        hostPath:
          # directory location on host
          path: /mnt/data
          # this field is optional
          type: Directory