简体   繁体   English

Kubernetes AntiAffinity - 限制每个节点的最大相同 pod 数

[英]Kubernetes AntiAffinity - limit max number of same pods per node

I have a kubernetes cluster with 4 nodes.我有一个有 4 个节点的 kubernetes 集群。 I have a pod deployed as a deployment, with 8 replicas.我有一个部署为部署的 pod,有 8 个副本。 When I deployed this, kubernetes sometimes schedule 4 pods in node1, and the rest of the 4 pods in node2.当我部署这个时,kubernetes 有时会在 node1 中调度 4 个 Pod,而在 node2 中调度 4 个 Pod 的 rest。 In this case node3 and node4 don't have this container running (but other containers running there)在这种情况下,node3 和 node4 没有运行这个容器(但其他容器在那里运行)

I do understand Pod affinity and anti-affinity , where they have the Zookeeper example for pod-anti-affinity, which is great.我确实了解 Pod affinity 和anti-affinity ,他们有Zookeeper的 pod-anti-affinity 示例,这很棒。 This would make sure that no 2 pods would deploy on the same node.这将确保没有 2 个 pod 会部署在同一个节点上。

This is fine, however my requirement is slightly different where I want to restrict the maximum number of the pods k8s can deploy to one node with node anti-affinity.这很好,但是我的要求略有不同,我想限制 k8s 可以部署到具有节点反关联性的一个节点的最大 pod 数量。

I need to make sure that not more than 3 instance of same pods are deployed on a node in my above example.在上面的示例中,我需要确保在一个节点上部署的相同 pod 实例不超过 3 个。 I thought of setting a memory/cpu limit on pods but that seemed like a bad idea as I have nodes with different configuration.我想过在 pod 上设置内存/cpu 限制,但这似乎是个坏主意,因为我有不同配置的节点。 Is there any way to achieve this?有什么办法可以做到这一点?

( Update 1 ) - I understand that my questions wasn't clear enough. (更新 1) - 我知道我的问题不够清楚。 To clarify further, what I want is to limit the instance of a pod to a maximum of 3 per node for a particular deployment.为了进一步澄清,我想要将特定部署的 pod 实例限制为每个节点最多 3 个。 Example, how do I tell k8s to not deploy more than 3 instances of nginx pod per node?例如,如何告诉 k8s 每个节点不要部署超过 3 个 nginx pod 实例? The restriction should only be applied to the nginx deployments and not other deployments.该限制应仅适用于 nginx 部署而不是其他部署。

( Update 2 ) - To further explain with a scenario. (更新 2) - 用一个场景进一步解释。 A k8s cluster, with 4 worker nodes.一个 k8s 集群,有 4 个工作节点。 2 Deployments 2 部署

  1. A nginx deployment -> replicas = 10 nginx 部署 -> 副本 = 10
  2. A custom user agent deployment -> Replicas 10自定义用户代理部署 -> 副本 10

Requirement - Hey kubernetes, I want to schedule 10 Pods of the "custom user agent" pod (Pod #2 in this example) in 4 nodes, but I want to make sure that each node may have only a maximum of 3 pods of the 'custom user agent'.要求 - 嘿 kubernetes,我想在 4 个节点中安排 10 个“自定义用户代理”Pod(本例中为 Pod #2)的 Pod,但我想确保每个节点最多只能有 3 个 Pod '自定义用户代理'。 For the 'nginx' pod, there shouldnt' be any such restriction, which means I don't mind if k8s schedule 5 nginx in one node and the rest of the 5 in the second node.对于 'nginx' pod,不应该有任何这样的限制,这意味着我不介意 k8s 在一个节点中调度 5 nginx 而在第二个节点中调度 5 的 rest。

So setting a bare minimum number of pod for each node can be achieved by topologykey因此可以通过topologykey来为每个节点设置一个最小的 Pod 数量

Yes, you can achieve a pod to spawn on every node by a deployment object by pod affinity with topologykey set as "kubernetes.io/hostname" .是的,您可以通过部署 object 来实现在每个节点上生成一个 pod,方法是 pod 亲和力设置为 " kubernetes.io/hostname "

With the above example, you will have the following behaviour:使用上面的示例,您将具有以下行为:

拓扑键

I hope thats what you are looking for:我希望这就是你正在寻找的: 在此处输入图像描述

That feature is in alpha, I believe it is called topologyKey , depending on your Kubernetes version you may be able to use it.该功能处于 alpha 阶段,我相信它被称为topologyKey ,取决于您的 Kubernetes 版本,您可能可以使用它。 https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/ https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/

I myself didn't find official documentation for this.我自己没有找到这方面的官方文档。 but i think you can use podantiaffinity with preferredDuringSchedulingIgnoredDuringExecution option.但我认为您可以将 podantiaffinity 与preferredDuringSchedulingIgnoredDuringExecution选项一起使用。 this will prevent k8s from placing the same pods on a single node, but if that is not possible it will select the most eligible existing node.这将阻止 k8s 将相同的 pod 放置在单个节点上,但如果这不可能,它将 select 成为最符合条件的现有节点。 official doc here官方文档在这里

affinity:
    podAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - podAffinityTerm:
          labelSelector:
            matchLabels:
              name: deployment-name
          topologyKey: kubernetes.io/hostname
        weight: 100

I believe what you want to achieve can be done via maxSkew parameter of pod topology spread constraints.我相信您想要实现的目标可以通过 pod 拓扑扩展约束的maxSkew参数来完成。 Please check the original documentation https://kubernetes.io/docs/concepts/workloads/pods/pod-topology-spread-constraints/请查看原始文档https://kubernetes.io/docs/concepts/workloads/pods/pod-topology-spread-constraints/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM