【问题标题】:Pod affinity to host node of bound local persistent volumePod 与绑定本地持久卷的主机节点的亲和性
【发布时间】:2019-04-09 13:43:22
【问题描述】:

我正在开发 k8s 的裸机实例,并且正在尝试使用 k8s v1.14 的本地持久卷。目的是允许我使用postgres-operator 创建 HA postgres 部署。由于我使用的是裸机,因此我无法使用动态 PVC,这在教程中似乎很正常。

首先,我创建了一些绑定到主机节点上手动创建的卷的 PV。 PV 使用nodeAffinity 分配给特定节点。即primary-vol PV被分配给node1,replica-vol-1被分配给node2等等。

然后我使用 PVC 将我的 pod 绑定到 PV,如 here 所记录的那样。

我发现 k8s 调度程序已将我的 pod(绑定到 node1 上的 PV)放在 node2 上,而不是我预期的 node1。

有没有办法在 pod 上使用关联性,以确保 pod 与绑定的 PV 在相同的节点上创建?

编辑:简化问题(向世界各地的艺术家和建筑师道歉)

当 Pod 甚至不知道它绑定到哪个 PV 时,它如何知道 PV 分配到哪个节点?

【问题讨论】:

标签: kubernetes


【解决方案1】:

是的,您可以使用 PV 定义中的 claimRef 来实现它。通过在 PV 中使用 claimRef,您将具有特定名称的 PVC 绑定到该 PV,您可以在 persistentVolumeClaim 的 pod 定义中使用该 PVC 名称。

你应该有如下的 PV 定义:

{
  "kind": "PersistentVolume",
  "apiVersion": "v1",
  "metadata": {
    "name": "pv-data-vol-0",
    "labels": {
      "type": "local"
    }
  },
  "spec": {
    "capacity": {
      "storage": "10Gi"
    },
    "accessModes": [
      "ReadWriteOnce"
    ],
    "storageClassName": "local-storage",
    "local": {
      "path": "/prafull/data/pv-0"
    },
    "claimRef": {
      "namespace": "default",
      "name": "data-test-sf-0"
    },
    "nodeAffinity": {
      "required": {
        "nodeSelectorTerms": [
          {
            "matchExpressions": [
              {
                "key": "kubernetes.io/hostname",
                "operator": "In",
                "values": [
                  "ip-10-0-1-46.ec2.internal"
                ]
              }
            ]
          }
        ]
      }
    }
  }
}

在上面的 json 文件中,claimRef、name 应该是你想要绑定那个 PV 的 PVC 的名称,namespace 应该是 PVC 所在的命名空间。

注意:namespace 是必填字段,因为 PV 独立于命名空间,PVC 绑定在命名空间中,因此 PV 应该知道它应该在哪个命名空间中查找 PVC。

因此,一旦您能够将特定 PV 绑定到特定 PVC,您就可以将该特定 PVC 绑定到特定 POD,因此该 pod 将始终位于 PV 所在的同一节点上。

作为参考,请看我下面的回答:

Is it possible to mount different pods to the same portion of a local persistent volume?

希望对你有帮助

【讨论】:

  • 谢谢,普拉富尔。我明天(澳大利亚东部标准时间)必须检查一下并报告。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2020-12-08
  • 2019-05-26
  • 1970-01-01
  • 2018-05-28
  • 2018-12-19
  • 1970-01-01
  • 2020-11-08
  • 1970-01-01
相关资源
最近更新 更多