k8s 高级调度 亲和力和反亲和力、绑定标签、污点容忍污点
看例子之前先看一下名词解释:
requiredDuringSchedulingIgnoredDuringExecution 必须满足,标签变化时不处理
requiredDuringSchedulingRequiredDuringExecution 必须满足,标签变化时重新选择(暂未支持)
preferredDuringSchedulingIgnoredDuringExecution 优先满足,标签变化时不处理
- 通过标签绑定
spec: nodeSelector: bigdata-node: bigdata containers: - env:
pod只能运行在有bigdata-node: bigdata 标签的node节点
- 通过node name绑定
spec: nodeName: test-oc08 containers: - env:
pod只能运行在名为test-oc08节点上
- node的亲和力
apiVersion: v1 kind: Pod metadata: name: with-node-affinity spec: affinity: nodeAffinity: #节点选择 requiredDuringSchedulingIgnoredDuringExecution: #定义必要规则 nodeSelectorTerms: - matchExpressions: - key: e2e-az-NorthSouth #必须匹配的键/值对(标签) operator: In values: - e2e-az-North #必须匹配的键/值对(标签 - e2e-az-South containers: - name: with-node-affinity image: docker.io/ocpqe/hello-pod
该规则要求将pod放置在节点上,且节点的标签的关键字是e2e-az-NorthSouth
,其值是e2e-az-North
或者e2e-az-South
apiVersion: v1 kind: Pod metadata: name: with-node-affinity spec: affinity: nodeAffinity: preferredDuringSchedulingIgnoredDuringExecution: #定义首选规则 - weight: 1 #权重 preference: matchExpressions: - key: e2e-az-EastWest operator: In values: - e2e-az-East - e2e-az-West containers: - name: with-node-affinity image: docker.io/ocpqe/hello-pod
该节点具有标签的关键字为e2e-az-EastWest
且其值为e2e-az-East
或者e2e-az-West
是该Pod的首选项的节点
- pod间亲和性和反亲和性
apiVersion: v1 kind: Pod metadata: name: with-pod-affinity spec: affinity: podAffinity: #亲和性 requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: security operator: In values: - S1 topologyKey: failure-domain.beta.kubernetes.io/zone containers: - name: with-pod-affinity image: docker.io/ocpqe/hello-pod
pod必须部署在一个节点上,这个节点上至少有一个正在运行的pod,这个pod中security=S1
apiVersion: v1 kind: Pod metadata: name: with-pod-antiaffinity spec: affinity: podAntiAffinity: #反亲和性 preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchExpressions: - key: security operator: In values: - S2 topologyKey: kubernetes.io/hostname containers: - name: with-pod-affinity image: docker.io/ocpqe/hello-pod
pod不太会部署在一个节点上,这个节点上正在运行的pod中有security=S2
- node的污点和
NoSchedule #新pod不会被调到该节点,已经存在的pod不会影响
PreferNoSchedule #新pod尽量避免不会调度到该节点,已经存在的pod不会影响
NoExecute #新pod不会被调到该节点,已经存在的pod将会被删除
spec: taints: - effect: NoSchedule key: key1 timeAdded: null value: value1
给节点添加污点
spec: tolerations: - key: "key1" operator: "Equal" value: "value1" effect: "NoSchedule"
pod容忍污点