k8sK8s

k8s调度-亲和性和反亲和性

2022-04-05  本文已影响0人  fengzhihai

一、解释

# 官网中关于pod亲和性和非亲和性的相关介绍
https://kubernetes.io/zh/docs/concepts/scheduling-eviction/assign-pod-node/

一般来说,pod的调度过程会经历如下过程:

1、客户端(useraccount、serviceacount)下发pod的创建,并将任务交给api-server,写进etcd
2、controller manager检测(watch)api-server提供的watch http rest,并创建相关资源,经由api-server写入etcd
3、schedule检测(watch)api-server提供的watch http rest,并执行相关的调度任务,将pod和符合条件的k8s节点进行绑定,完成调度,后续就是kubelet的任务了,在此省略。

但有时候,这种调度需要人为干涉,以便完成更加符合预期的调度,eg:对于数据库我们总想将pod调度到SSD磁盘上面,这个时候我们就可以基于k8s提供的相关功能完成特定的调度,在此总结如下。

二、nodeSelector

[root@molk8s-master k8s-affinity]# cat 1-nodeselector.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx1
  namespace: dev
  labels:
    app: nginx1
spec:
  containers:
    - name: nginx
      image: nginx:latest
      imagePullPolicy: IfNotPresent
  nodeSelector:
    node: molk8s-node-02

nodeSelector是一种硬性限制,上述设置:nginx1将会被调度到打有标签:node=molk8s-node-02的k8s节点上。但这种做法较为单一且不灵活,很难满足复杂场景的调度设置。这些复杂场景在后续的例子中将会列出,你可以做下对比,看看基于nodeSelector是否能够满足我们的场景需求。

三、nodeAffinity

[root@molk8s-master k8s-affinity]# cat 2-nodeaffinity.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx2
  namespace: dev
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: type
            operator: In
            values:
            -  app
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: node
            operator: In
            values:
            - molk8s-node-01
  containers:
  - name: nginx2
    image: nginx:latest
nodeAffinity也是基于k8s节点标签的一种调度策略,有如下两种限制:
requiredDuringSchedulingIgnoredDuringExecution (硬限制,必须满足, 同nodeSelector,不满足则不进行调度。)
preferredDuringSchedulingIgnoredDuringExecution(软限制,尽量满足,不满足也可正常完成调度)
IgnoreDuringExecution表示如果在Pod运行期间Node的标签发生变化,导致亲和性策略不能满足,则继续运行当前的Pod。

上面的限制也同样适用于:podaffinity和podAntiAffinity。

3.1、nodeSelectorTerms中的元素有多个

[root@molk8s-master k8s-affinity]# cat 3-multi-matchexpression.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx3
  namespace: dev
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: node
            operator: In
            values:
            -  molk8s-node-01
        - matchExpressions:
          - key: node
            operator: In
            values:
            -  molk8s-node-02
  containers:
  - name: nginx3
    image: nginx:latest

这种情况下满足一个即可完成调度。

3.2、matchExpressions中的表达式有多个

[root@molk8s-master k8s-affinity]# cat 4-multi-key.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx4
  namespace: dev
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: node
            operator: In
            values:
            -  molk8s-node-01
          - key: node
            operator: In
            values:
            -  molk8s-node-02
  containers:
  - name: nginx4
    image: nginx:latest

这种情况将无法完成调度。

四、podaffinity

[root@molk8s-master k8s-affinity]# cat 6-pod-affinity.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx6
  namespace: dev
spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - nginx2
        topologyKey: "kubernetes.io/hostname"
  containers:
  - name: nginx6
    image: nginx:latest

因为k8s的节点"molk8s-node-01"有节点标签key"kubernetes.io/hostname"且这个节点上已经运行具有"app=nginx2"标签的pod实例,所以nginx6会被调度到此节点上。

五、podAntiAffinity

[root@molk8s-master k8s-affinity]# cat 5-pod-antiaffinity.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx5
  namespace: dev
  labels:
    app: nginx5
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx5
  template:
    metadata:
      labels:
        app: nginx5
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx5
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: nginx5
        image: nginx:latest

nginx5的两个pod会被调度不同的节点上面去。
假如你还想将nginx5的两个pod调度到特定标签的node上,那可以结合nodeAffinity和podAntiAffinity,见下:

[root@molk8s-master k8s-affinity]# cat 5-pod-antiaffinity.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx5
  namespace: dev
  labels:
    app: nginx5
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx5
  template:
    metadata:
      labels:
        app: nginx5
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: node
                operator: In
                values:
                -  molk8s-node-01
                -  molk8s-node-02
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx5
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: nginx5
        image: nginx:latest

nginx5的两个pod将被被调度到具有标签"node=molk8s-node-01"和"node=molk8s-node-02"的两个k8s节点上面,且能保证每个节点只运行一个pod。

六、topologyKey

# 6.1、既然 topologyKey 是拓扑域,那 Pod 之间怎样才是属于同一个拓扑域?
如果使用 k8s.io/hostname,则表示拓扑域为 Node 范围,那么 k8s.io/hostname 对应的值不一样就是不同的拓扑域。比如 Pod1 在 k8s.io/hostname=node1 的 Node 上,Pod2 在 k8s.io/hostname=node2 的 Node 上,Pod3 在 k8s.io/hostname=node1 的 Node 上,则 Pod2 和 Pod1、Pod3 不在同一个拓扑域,而Pod1 和 Pod3在同一个拓扑域。
如果使用 failure-domain.k8s.io/zone ,则表示拓扑域为一个区域。同样,Node 的标签 failure-domain.k8s.io/zone 对应的值不一样也不是同一个拓扑域,比如 Pod1 在 failure-domain.k8s.io/zone=beijing 的 Node 上,Pod2 在 failure-domain.k8s.io/zone=hangzhou 的 Node 上,则 Pod1 和 Pod2 不属于同一个拓扑域。
当然,topologyKey 也可以使用自定义标签。比如可以给一组 Node 打上标签 custom_topology,那么拓扑域就是针对这个标签了,则该标签相同的 Node 上的 Pod 属于同一个拓扑域。
# 6.2、注意事项
原则上,topologyKey 可以是任何合法的标签 Key。但是出于性能和安全原因,对 topologyKey 有一些限制:
6.2.1、对于亲和性和 requiredDuringSchedulingIgnoredDuringExecution 的 Pod 反亲和性,topologyKey 不能为空。
6.2.2、对于 requiredDuringSchedulingIgnoredDuringExecution 的 Pod 反亲和性,引入 LimitPodHardAntiAffinityTopology 准入控制器来限制 topologyKey 只能是 kubernetes.io/hostname。如果要使用自定义拓扑域,则可以修改准入控制器,或者直接禁用它。
6.2.3、对于 preferredDuringSchedulingIgnoredDuringExecution 的 Pod 反亲和性,空的 topologyKey 表示所有拓扑域。截止 v1.12 版本,所有拓扑域还只能是 kubernetes.io/hostname、failure-domain.beta.kubernetes.io/zone 和 failure-domain.beta.kubernetes.io/region 的组合。
6.2.4、除上述情况外,topologyKey 可以是任何合法的标签 key。

七、官网例子

下面deployment rc通过podAntiAffinity确保redis-cache被调度到不同的节点上。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-cache
spec:
  selector:
    matchLabels:
      app: store
  replicas: 3
  template:
    metadata:
      labels:
        app: store
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - store
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: redis-server
        image: redis:3.2-alpine

下面deployment rc通过podAntiAffinity确保web-server被调度到不同的节点上,同时通过podAffinity确保web-server和redis-cache被调度到同一个节点上,两者都是硬性要求。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-server
spec:
  selector:
    matchLabels:
      app: web-store
  replicas: 3
  template:
    metadata:
      labels:
        app: web-store
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - web-store
            topologyKey: "kubernetes.io/hostname"
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - store
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: web-app
        image: nginx:1.16-alpine
上一篇 下一篇

猜你喜欢

热点阅读