kubernetes亲和性调度策略

环境描述

在这里插入图片描述

Node节点选择器

nodeSelector

  • nodeSelector基于node节点label选择匹配的节点
apiVersion: v1
kind: Pod
metadata:
  name: nodeselector-demo
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: nginx
  nodeSelector:
    app: node-2

在这里插入图片描述

  • 删除此pod修改nodeSelector为node-1
apiVersion: v1
kind: Pod
metadata:
  name: nodeselector-demo
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: nginx
  nodeSelector:
    app: node-1

在这里插入图片描述

  • 修改一个不存在的标签
apiVersion: v1
kind: Pod
metadata:
  name: nodeselector-demo
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: nginx
  nodeSelector:
    app: node-3

kubernetes亲和性调度策略_第1张图片

nodeName节点名称选择

apiVersion: v1
kind: Pod
metadata:
  name: nodeselector-demo
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: nginx
  nodeName: node-2

kubernetes亲和性调度策略_第2张图片

  • 修改一个不存在的node节点名称
apiVersion: v1
kind: Pod
metadata:
  name: nodeselector-demo
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: nginx
  nodeName: node-3

kubernetes亲和性调度策略_第3张图片
node节点选择器是一种硬亲和性的选择器

节点亲和性调度

nodeAffinity

requiredDuringSchedulingIgnoredDuringExecution(硬亲和性)

apiVersion: v1
kind: Pod
metadata:
  name: required-affinity-demo
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: nginx
    imagePullPolicy: IfNotPresent
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: app
            operator: In
            values:
            - node-2

在这里插入图片描述

  • 修改一个节点中不存在的标签
apiVersion: v1
kind: Pod
metadata:
  name: required-affinity-demo
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: nginx
    imagePullPolicy: IfNotPresent
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: app
            operator: In
            values:
            - node-3

kubernetes亲和性调度策略_第4张图片可以看出,没有匹配到节点标签中有app=node-3的节点

preferredDuringSchedulingIgnoredDuringExecution(软亲和性)

apiVersion: v1
kind: Pod
metadata:
  name: preferred-affinity-demo
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: nginx
    imagePullPolicy: IfNotPresent
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - preference:
          matchExpressions:
          - key: app
            operator: In
            values:
            - node-3
        weight: 60

在这里插入图片描述

Pod亲和性调度

podAffinity(pod亲和性)

requiredDuringSchedulingIgnoredDuringExecution(硬亲和性)

apiVersion: v1
kind: Pod
metadata:
  name: pod-first
  labels:
    app: myapk
spec:
  containers:
  - name: myapk
    image: nginx
---
apiVersion: v1
kind: Pod
metadata:
  name: required-affinity-demo
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: nginx
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - myapk
        topologyKey: kubernetes.io/hostname

在这里插入图片描述

  • 去除pod亲和性测试
apiVersion: v1
kind: Pod
metadata:
  name: pod-first
  labels:
    app: myapk
spec:
  containers:
  - name: myapk
    image: nginx
---
apiVersion: v1
kind: Pod
metadata:
  name: required-affinity-demo
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: nginx

在这里插入图片描述

preferredDuringSchedulingIgnoredDuringExecution(软亲和性)

apiVersion: v1
kind: Pod
metadata:
  name: pod-first
  labels:
    app: myapk
spec:
  containers:
  - name: myapk
    image: nginx
---
apiVersion: v1
kind: Pod
metadata:
  name: required-affinity-demo
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: nginx
  affinity:
    podAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: app
              operator: In
              values:
              - myapk
          topologyKey: zone
        weight: 60

软亲和性测试不明显

podAntiAffinity(pod反亲和性)

apiVersion: v1
kind: Pod
metadata:
  name: pod-first
  labels:
    app: myapk
spec:
  containers:
  - name: myapk
    image: nginx
---
apiVersion: v1
kind: Pod
metadata:
  name: required-affinity-demo
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: nginx
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - myapk
        topologyKey: zone
  • 为方便测试需要把node节点打上同样的标签
kubectl label nodes node-1 zone=foo
kubectl label nodes node-2 zone=foo

在这里插入图片描述

apiVersion: v1
kind: Pod
metadata:
  name: pod-first
  labels:
    app: myapk
spec:
  containers:
  - name: myapk
    image: nginx
---
apiVersion: v1
kind: Pod
metadata:
  name: required-affinity-demo
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: nginx
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: app
              operator: In
              values:
              - myapk
          topologyKey: zone
        weight: 60

你可能感兴趣的:(Kubernetes)