通过亲和性控制 Pod 调度

今天同事提了一个需求,要将几个应用调度到一个node上,之前测试环境采用的方法是,给其中某个node添加taint和label,通过taint使没有污点容忍的Pod无法被调度上去,再通过label将对应的Pod强制调度到该node。

但是这种方法也有个弊端,因为只给一个node添加了taint和label,万一该node挂掉了,那么这些Pod 都无法被调度到其他节点。

下面是参考的官方文档

https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/assign-pods-nodes-using-node-affinity/

那么是否有办法解决这个问题呢?

你可能会说,那就多给几个node加上taint和label不就行了嘛?

这样确实可以保证在某个node挂掉的时候这些应用可以被重新调度,但是却没有办法保证所有的应用都在某一个node上。完全有可能一半的应用在第一个节点,剩下的一半再另外的节点。

那么是否还有别的办法呢?

通过 kubectl explain Deployment.spec.template.spec.affinity 命令,我们可以获得以下信息:

GROUP:      apps
KIND:       Deployment
VERSION:    v1

FIELD: affinity <Affinity>

DESCRIPTION:
    If specified, the pod's scheduling constraints
    Affinity is a group of affinity scheduling rules.
    
FIELDS:
  nodeAffinity        <NodeAffinity>
    Describes node affinity scheduling rules for the pod.

  podAffinity        <PodAffinity>
    Describes pod affinity scheduling rules (e.g. co-locate this pod in the same
    node, zone, etc. as some other pod(s)).

  podAntiAffinity        <PodAntiAffinity>
    Describes pod anti-affinity scheduling rules (e.g. avoid putting this pod in
    the same node, zone, etc. as some other pod(s)).

可以看到,有一个 podAffinity 的规则。看不懂英文的话可以借助下翻译软件。这个字段的作用就是将一个Pod(Pod B)与另外一个 Pod(Pod A)的某个标签关联,从而使得两个 Pod会强度到一起。

再看他的详细解释,执行kubectl explain Deployment.spec.template.spec.affinity.podAffinity 命令。

GROUP:      apps
KIND:       Deployment
VERSION:    v1

FIELD: podAffinity <PodAffinity>

DESCRIPTION:
    Describes pod affinity scheduling rules (e.g. co-locate this pod in the same
    node, zone, etc. as some other pod(s)).
    Pod affinity is a group of inter pod affinity scheduling rules.
    
FIELDS:
  preferredDuringSchedulingIgnoredDuringExecution        <[]WeightedPodAffinityTerm>
    The scheduler will prefer to schedule pods to nodes that satisfy the
    affinity expressions specified by this field, but it may choose a node that
    violates one or more of the expressions. The node that is most preferred is
    the one with the greatest sum of weights, i.e. for each node that meets all
    of the scheduling requirements (resource request, requiredDuringScheduling
    affinity expressions, etc.), compute a sum by iterating through the elements
    of this field and adding "weight" to the sum if the node has pods which
    matches the corresponding podAffinityTerm; the node(s) with the highest sum
    are the most preferred.

  requiredDuringSchedulingIgnoredDuringExecution        <[]PodAffinityTerm>
    If the affinity requirements specified by this field are not met at
    scheduling time, the pod will not be scheduled onto the node. If the
    affinity requirements specified by this field cease to be met at some point
    during pod execution (e.g. due to a pod label update), the system may or may
    not try to eventually evict the pod from its node. When there are multiple
    elements, the lists of nodes corresponding to each podAffinityTerm are
    intersected, i.e. all terms must be satisfied.

这里可以再次借助下翻译软件的力量。

可以看到两个字段的区别,第一个是 preferred,可以理解为“倾向于”,第二个是 required,理解为 “必须”。那我们通过这个字段就可以按照我们的需求将Pod进行有控制的调度了。

下面给出参考的 yaml。

Pod A Yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
      - name: frontend
        image: frontend-image

Pod B Yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: backend
    spec:
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchLabels:
                app: frontend
            topologyKey: kubernetes.io/hostname
      containers:
      - name: backend
        image: backend-image

当然,写完要再检查一下书写是否正确,避免一些拼写的错误,有时候拼写上的错误真的是太难检查了!