Kubernetes-Affinity、Antiffinity

Affinity

Affinity: # 亲和力
NodeAffinity: # 节点亲和力

  • RequireDuringSchedulinglgnoredDuringExecution: # 硬亲和力,即支持必须部署在指定的节点上,也支持必须不部署在指定的节点上
  • PreferredDuringSchedulinglgnoredDuringExecution: # 软亲和力,尽量部署在满足条件的节点上,或者是尽量不要部署在被匹配的节点
spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: beta.kubernetes.io/os
                    operator: In
                    values:
                      - linux
                  - key: beta.kubernetes.io/arch
                    operator: In
                    values:
                      - amd64

nodeAffinity和nodeSelector基本一样,根据节点上的Label 标签约束来绝对pod调度到哪些节点

  • 硬亲和性
    约束条件必须满足
  • 软亲和性
    尝试满足,不保证

operator: In
支持常用操作符:

  • In: # 部署在满足多个条件的节点上
  • NotIn: # 不要部署在满足这些条件的节点上
  • Exists: # 部署在具有某个存在key为指定值的node节点
  • DoesNotExists: # 与Exists相反
  • Gt: # 大于指定的条件
  • Lt: # 小于指定的条件
apiVersion: v1
kind: Pod
metadata:
  name: with-node-affinity
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: kubernetes.io/e2e-az-name
            operator: In
            values:
            - e2e-az1
            - e2e-az2
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: another-node-label-key
            operator: In
            values:
            - another-node-label-value
  containers:
  - name: with-node-affinity
    image: k8s.gcr.io/pause:2.0

分别在node1、node2、master节点上打上如下的label

$ kubectl label node k8s-node1 kubernetes.io/e2e-az-name=e2e-az1
$ kubectl label node k8s-node2 kubernetes.io/e2e-az-name=e2e-az2
$ kubectl label node k8s-node1 another-node-label-key=another-node-label-value
$ kubectl label node k8s-master another-node-label-key=another-node-label-value

在满足e2e-az-name=e2e-az1和e2e-az-name=e2e-az2的情况下尽量部署到满足another-node-label-key=another-node-label-value的node节点

$kubectl get pods -owide
NAME                    READY   STATUS    RESTARTS   AGE     IP              NODE        NOMINATED NODE   READINESS GATES
nginx-669b9c77f-8mhcd   1/1     Running   0          2m19s   172.16.36.116   k8s-node1   <none>           <none>
nginx-669b9c77f-xzsxx   1/1     Running   0          2m38s   172.16.36.115   k8s-node1   <none>           <none>
PodAffinity: Pod亲和力
  A应用B应用C应用,将A应用根据某种策略尽量部署在一起         Label
  * RequireDuringSchedulinglgnoredDuringExecution: 
    将A应用和B应用部署在一起                             Label
  * PreferredDuringSchedulinglgnoredDuringExecution:
    尽量将A应用和B应用部署在一起                          Label
PodAntiffinity: Pod反亲和力
  A应用B应用C应用,将A应用根据某种策略尽量或不部署在一起      Label
  * RequireDuringSchedulinglgnoredDuringExecution: 
    不要将A与与之匹配的应用部署在一起                      Label
  * PreferredDuringSchedulinglgnoredDuringExecution:
	尽量不要将A与与之匹配的应用部署在一起					  Label
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
posted @ 2021-09-07 13:33  Cai_HL  阅读(90)  评论(0编辑  收藏  举报
>