Kubernetes - 如何在 PVC 中提及 hostPath?

Kubernetes - How do I mention hostPath in PVC?

我需要使用 PVC 来指定 PV 的规格,我还需要确保它在 PV 中使用自定义本地存储路径。

我不知道如何在 PVC 中提及主机路径?

这是 PVC 配置:

apiVersion: v1 
kind: PersistentVolumeClaim
metadata: 
  name: mongo-pvc 
spec: 
  accessModes: 
    - ReadWriteOnce
  resources: 
    requests: 
      storage: 1Gi

这是 mongodb 部署:

spec:
    replicas: 1
    selector:
        matchLabels:
            app: mongo
    template:
        metadata:
            labels: 
                app: mongo
        spec:
            volumes: 
                - name: mongo-volume 
                  persistentVolumeClaim: 
                    claimName: mongo-pvc 
            containers:
                - name: mongo
                  image: mongo
                  ports:
                    - containerPort: 27017
                  volumeMounts: 
                    - name: mongo-volume 
                      mountPath: /data/db 

Howwhere 我提到要安装的 hostPath在这里?

文档说您在创建 PV 时设置 hostPath(创建 PVC 之前的步骤)。

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

创建 PersistentVolumeClaim 后,Kubernetes 控制平面会查找满足声明要求的 PersistentVolume。如果控制平面找到具有相同 StorageClass 的合适 PersistentVolume,它将声明绑定到该卷。

请看https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/

您不会(也不能)在 PersistentVolumeClaim 中强制指定主机路径。

通常情况下,Kubernetes 集群将配置 dynamic volume provisioner and that will create the matching PersistentVolume for you. Depending on how your cluster was installed that could be an Amazon EBS volume, a Google Cloud Platform persistent disk, an iSCSI volume, or some other type of storage; as an application author you don't really control that. (You tagged this question for GKE, and the GKE documentation has a section on dynamic volume provisioning。)您无需指定卷可能安装在主机上的位置,并且无法在 PersistentVolumeClaim 中提供此详细信息。

根据您显示的 YAML,以及它在 GKE 上的上下文,我希望 Google 能够自动配置 GCE 永久磁盘。如果 pod 被重新安排在不同的节点上,持久磁盘将跟随 pod 到新节点。您无需担心正在使用的特定主机目录; Kubernetes 将为您管理。

在大多数情况下,您会希望避免使用 hostPath 存储。您不直接控制您的 pods 将 运行 在哪个节点上,因此您无法保证 Pod 将实际调度到具有数据量的节点上。它适用于像 DaemonSet 中的日志收集器 运行ning 之类的东西,您可以在其中保证每个节点上的该路径中都有有趣的内容,但不适用于您的通用应用程序数据库存储。