k8s基礎概念之九 Affinity
阿新 • • 發佈:2021-12-16
官方建議:如果節點超過1000不建議用Affinity,因為排程計算會消耗大量時間,當然官方也有優化方案
NodeAffinity:節點親和力
requiredDuringSchedulingIgnoredDuringExecution: 硬親和力,即支援必須部署在指定的節點上,也支援必須不部署在指定節點上
preferredDuringSchedulingIgnoredDuringExecution: 軟親和力,儘量部署在滿足條件的節點上,或者是儘量不用部署在被匹配的節點上
pod親和力
A應用 B應用 C應用,將a應用根據某種策略部署在一塊 requiredDuringSchedulingIgnoredDuringExecution: 將a應用和b應用部署在一塊 preferredDuringSchedulingIgnoredDuringExecution: 儘量將a應用和b應用部署在一塊
requiredDuringSchedulingIgnoredDuringExecution: 不要將a應用與之匹配的應用部署在一塊
preferredDuringSchedulingIgnoredDuringExecution: 儘量不要將a應用與之匹配的應用部署在一塊
kubectl label nodes node01 disktype=ssd kubectl label nodes node02 disktype=ssd kubectl label nodes node01 processor=gpu
apiVersion: v1 kind: Pod metadata: name: nginx-gpu spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: #必須滿足 nodeSelectorTerms: - matchExpressions: #下面這些條件是可以單獨拿出來匹配出目標 - key: disktype #標籤disktype operator: In #等於 values: - ssd # ssd preferredDuringSchedulingIgnoredDuringExecution: #儘量滿足 - weight: 1 preference: matchExpressions: - key: processor #labels key operator: In #等於 values: - gpu # gpu containers: - name: nginx image: nginx imagePullPolicy: IfNotPresent
#pod親和力 kubectl label pod bosybox meme=bus #給需要繫結的pod加個唯一標籤 --- apiVersion: v1 kind: Pod metadata: name: nginx spec: #把改例項和kube-systemnamespaces下符合的albel為k8s-calico-kube-controllers的pod部署在同一個節點(拓撲域)上 affinity: podAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: meme operator: In values: - bus #如果寫了namespaces但是留空,他是匹配所有namespace下的指定label的pod,如果寫了那麼namespaces指定了值,就是匹配指定namespaces下單指定labels的pod namespaces: - kube-system topologyKey: kubernetes.io/hostname containers: - name: nginx image: nginx imagePullPolicy: IfNotPresent --- …… spec: affinity: podAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: k8s-con operator: In values: - calico containers: --- #反親和力 apiVersion: v1 kind: Pod metadata: name: nginx spec: affinity: podAntiAffinity: #就這裡加了個Anti requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: meme operator: In values: - bus namespaces: - kube-system topologyKey: kubernetes.io/hostname containers: - name: nginx image: nginx imagePullPolicy: IfNotPresent #就是儘量不部署在同一個伺服器上