Kubernetes常见优化之Node/Pod亲和

为了使kubernetes更高效更稳定,往往需要针对操作系统层或Kubernetes本身在内存调度策略、磁盘使用策略、网络策略等方面进行优化,本节内容将讲述如何针对节点亲和性和Pod亲和性进行优化。

亲和性调度可以分成软策略和硬策略两种方式:

  • 软策略是指在没有满足调度要求的节点的时候,pod将忽略这条规则,继续完成调度过程,即:能满足条件最好,不满足也无所谓
  • 硬策略是指必须存在满足条件的节点,否则pod将无法部署(为pendding状态),直到发现满足条件的节点,即:必须满足指定要求

软硬策略实例:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: affinity
  labels:
    app: affinity
spec:
  replicas: 5
  revisionHistoryLimit: 15
  template:
    metadata:
      labels:
        app: affinity
        role: test
    spec:
      containers:
      - name: nginx
        image: nginx:1.16.1
        ports:
        - containerPort: 80
          name: webproxy
      affinity:
        nodeAffinity:
          # 硬策略
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: NotIn
                values:
                - node03
          # 软策略
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            preference:
              matchExpressions:
              - key: com
                operator: In
                values:
                - highmemorynodes

节点亲和性(nodeAffinity)

节点亲和性是用于控制pod部署在指定的node的机制,以label值为匹配逻辑,Kubernetes提供:

操作符 说明
In label值在指定列表中
NotIn label值不在指定列表中
Gt label值大于指定值
Lt label值小于指定值
Exists label存在
DoesNotExist label不存在

此外,还可以将指定的node标记为“污点(taints)”,除非pod也被标识为可以容忍污节点,否则该Taints节点上将不会部署pod,标记污节点的命令:

kubectl taint nodes node01 test=node01:NoSchedule

此时,如果需要将某pod部署到node1,则需配置为:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: taint
  labels:
    app: taint
spec:
  replicas: 5
  revisionHistoryLimit: 15
  template:
    metadata:
      labels:
        app: taint
    spec:
      containers:
      - name: nginx
        image: nginx:1.16.1
        ports:
        - name: http
          containerPort: 80
      tolerations:
      - key: "node-role.kubernetes.io/master"
        operator: "Exists"
        effect: "NoSchedule"

Pod亲和性(podAffinity)

Pod亲和性用于控制pod可以和指定的pod可以部署在同一个拓扑域中,另外一个概念是pod反亲和性,则是表示控制pod不能和指定的pod部署在同一个拓扑域中。

举个pod亲和例子:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: affinity
  labels:
    app: affinity
spec:
  replicas: 5
  revisionHistoryLimit: 15
  template:
    metadata:
      labels:
        app: affinity
        role: test
    spec:
      containers:
      - name: nginx
        image: nginx:1.16.1
        ports:
        - containerPort: 80
          name: webproxy
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - highperformancenodes
            topologyKey: kubernetes.io/hostname

举个pop反亲和例子:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: affinity
  labels:
    app: affinity
spec:
  replicas: 5
  revisionHistoryLimit: 15
  template:
    metadata:
      labels:
        app: affinity
        role: test
    spec:
      containers:
      - name: nginx
        image: nginx:1.61.1
        ports:
        - containerPort: 80
          name: webproxy
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - lowmemorynodes
            topologyKey: kubernetes.io/hostname

原创文章禁止转载:技术学堂 » Kubernetes常见优化之Node/Pod亲和

精彩评论

8+8=

感谢您的支持与鼓励

支付宝扫一扫打赏

微信扫一扫打赏