k8s 親和性和反親和性

親和性和反親和性

node 親和性

node親和性策略表示pod部署到符合某些條件的node上.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: name
                operator: In
                values:
                - nginx1
                - nginx2
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            preference:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx
      containers:
      - name: nginx-server
        image: nginx:latest

上面的這個例子表示pod必須部署到滿足nodeSelectorTerms中的條件,盡量滿足preference中的條件.

  • nodeAffinity表示pod和node的親和性策略
  • nodeSelectorTerms 表示打了這些標(biāo)簽的node上才能部署pod,這里的key values都可以指定多個值,可以實現(xiàn)類似nodeSelector的或的操作.
  • preference 表示最好部署到滿足這些條件的node上.

策略

  • requiredDuringSchedulingIgnoredDuringExecution 硬策略表示調(diào)度過程必須滿足執(zhí)行過程忽略

  • preferredDuringSchedulingIgnoredDuringExecution 軟策略表示調(diào)度過程盡量滿足執(zhí)行過程忽略

表達式

label 的值可選的操作符有:

  • In:label 的值在某個列表中
  • NotIn:label 的值不在某個列表中
  • Exists:某個 label 存在
  • DoesNotExist: 某個 label 不存在
  • Gt:label 的值大于某個值(字符串比較)
  • Lt:label 的值小于某個值(字符串比較

pod 親和性和非親和性

pod親和性和非親和性表示pod部署到或不部署到滿足某些label的pod所在的node上.

  1. 使用pod非親和性硬策略,實現(xiàn)相同lable pod一定不能調(diào)度到同一節(jié)點
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: nginx-server
        image: nginx:latest
  • podAntiAffinity 表示pod的非親和性策略
  • matchExpressions 表示符合label條件的pod
  • topologyKey 表示作用域key,這里使用 kubernetes.io/hostname表示所有的節(jié)點,因為所有的節(jié)點默認都會打上這個標(biāo)簽

這個例子表示所有有app:nginx標(biāo)簽的pod非親和性,就是不能部署在同一節(jié)點,并且在調(diào)度過程中是硬策略.

  1. 使用pod非親和性軟策略實現(xiàn)相同label pod盡量不要調(diào)度到同一節(jié)點
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      nodeSelector:
        app: nginx
      affinity:  
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - nginx
              topologyKey: kubernetes.io/hostname
      containers:
      - name: nginx
        image: nginx

  • podAntiAffinity 親和性策略
    表示盡量把有l(wèi)abel app:nginx的pod部署在同一節(jié)點
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容