如何在 Kubernetes 集群中修复 "pods is not balanced"

How to fix "pods is not balanced" in Kubernetes cluster

Pods 在节点池中不平衡。为什么不传播到每个节点?

我在 1 个节点池中有 9 个实例。过去,我曾尝试添加到 12 个实例。 Pods 不平衡。

image description here 想知道是否有任何解决方案可以帮助解决这个问题并在 1 个节点池中使用 9 个实例?

Pods 被 kube-scheduler 安排到节点上的 运行。一旦它们被安排好,它们就不会被重新安排,除非它们被移除。

因此,如果您添加更多节点,已经 运行 宁 pods 将不会重新安排。

孵化器中有一个项目正好解决了这个问题。

https://github.com/kubernetes-incubator/descheduler

Scheduling in Kubernetes is the process of binding pending pods to nodes, and is performed by a component of Kubernetes called kube-scheduler. The scheduler's decisions, whether or where a pod can or can not be scheduled, are guided by its configurable policy which comprises of set of rules, called predicates and priorities. The scheduler's decisions are influenced by its view of a Kubernetes cluster at that point of time when a new pod appears first time for scheduling. As Kubernetes clusters are very dynamic and their state change over time, there may be desired to move already running pods to some other nodes for various reasons:

  • Some nodes are under or over utilized.
  • The original scheduling decision does not hold true any more, as taints or labels are added to or removed from nodes, pod/node
    affinity requirements are not satisfied any more.
  • Some nodes failed and their pods moved to other nodes.
  • New nodes are added to clusters.

你应该调查一下 inter-pod anti-affinity。此功能允许您根据节点上 pods 运行 的标签限制不应在何处安排 pods。在您的情况下,鉴于您的应用程序具有标签 app-label,您可以使用它来确保 pods 不会被安排在具有 pods 且标签 app-label 的节点上。例如:

apiVersion: apps/v1
kind: Deployment
...
spec:
  selector:
    matchLabels:
      label-key: label-value
  template:
    metadata:
      labels:
        label-key: label-value
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: label-key
                operator: In
                values:
                - label-value
            topologyKey: "kubernetes.io/hostname"
...

PS:如果您使用requiredDuringSchedulingIgnoredDuringExecution,您最多可以拥有与节点数一样多的pods。如果您希望 pods 多于可用节点,则必须使用 preferredDuringSchedulingIgnoredDuringExecution,这使得反亲和性成为一种偏好,而不是一种义务。