其他分享
首页 > 其他分享> > k8s 亲和性

k8s 亲和性

作者:互联网

k8s 亲和性

1.1 节点亲和性

pod.spec.nodeAffinity

键值运算关系

如果nodeSelectorTerms下面有多个选项的话,满足任何一个条件就可以了;如果matchExpressions有多个选项的话,则必须同时满足这些条件才能正常调度 POD

1.1.1 preferredDuringSchedulingIgnoredDuringExecution

apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: with-node-affinity
    image: wangyanglinux/myapp:v1
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1			//权重,当有2-3个pod,更愿意跟谁在一起
        preference:
          matchExpressions:
          - key: source
            operator: In
            values:
            - qikqiak

1.1.2 requiredDuringSchedulingIgnoredDuringExecution

apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: with-node-affinity
    image: wangyanglinux/myapp:v1
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:		//节点选择
        - matchExpressions:
          - key: kubernetes.io/hostname			//匹配lable里key为kubernetes.io/hostname的
            operator: NotIn
            values:
            - k8s-node02		//匹配lable里key为kubernetes.io/hostname且value为k8s-node02的

1.1.3 合体

apiVersion: v1
kind: Pod
metadata:
  name: affinity
  labels:
    app: node-affinity-pod
spec:
  containers:
  - name: with-node-affinity
    image: wangyanglinux/myapp:v1
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: kubernetes.io/hostname
            operator: NotIn
            values:
            - k8s-node02
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: source
            operator: In
            values:
            - qikqiak
//对满足source=qikqiak,且kubernetes.io/hostname不等于k8s-node02 的特征节点选择

2.1 Pod 亲和性

pod.spec.affinity.podAffinity/podAntiAffinity

apiVersion: v1
kind: Pod
metadata:
  name: pod-3
  labels:
    app: pod-3
spec:
  containers:
  - name: pod-3
    image: wangyanglinux/myapp:v1
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - pod-1
        topologyKey: kubernetes.io/hostname
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: app
              operator: In
              values:
              - pod-2
          topologyKey: kubernetes.io/hostname

亲和性/反亲和性调度策略比较如下:
image

标签:matchExpressions,name,亲和性,v1,affinity,key,pod,k8s
来源: https://www.cnblogs.com/liwenchao1995/p/16529495.html