节点选择、亲和性调度

节点选择

  • nodeName
  • nodeSelector

这两个比较简单,所以不需要多说什么

节点亲和性调度

pod.spec.affinity

  • pod.spec.affinity.nodeAffinity: 节点亲和性
  • pod.spec.affinity.podAffinity: Pod亲和性
  • pod.spec.affinity.podAniAffinity: Pod反亲和性

pod.spec.affinity.nodeAffinity

  • pod.spec.affinity.nodeAffinity.perferredDuringSchedulingIgnoredDuringExecution: 软亲和性限制
  • pod.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution: 硬亲和性限制

硬亲和限制

apiVersion: v1
kind: Pod
metadata:
  name: myapp
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
  affinity:
    nodeAffinity:
      # 硬亲和性
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: idc
            # In关键字,判断是否拥有一下标签
            operator: In
            values: ["bj", "tj"]

如果此时没有节点拥有foo和bar这两个标签,此时创建的Pod会处于Pending状态

通过kubectl describe pod myapp时会看到如下信息,说明没有节点能够满足条件,所以不会调度到任何一个节点上

Events:
  Type     Reason            Age               From               Message
  ----     ------            ----              ----               -------
  Warning  FailedScheduling  2s (x7 over 14s)  default-scheduler  0/3 nodes are available: 3 node(s) didn't match node selector.

软亲和限制

apiVersion: v1
kind: Pod
metadata:
  name: myapp
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
  affinity:
    nodeAffinity:
      # 软亲和
      preferredDuringSchedulingIgnoredDuringExecution:
      - preference:
          matchExpressions:
          - key: zone
            operator: In
            values: ["foo", "bar"]
        # 权重    
        weight: 10

软亲和不会因为没有节点拥有foo和bar就处于Pending状态,而会忽略这个亲和性限制,继续创建Pods资源。

Pod 亲和性调度

节点亲和性调度和Pods 亲和性调度都可以实现,让一组相关的Pod运行在特定的一个或者一些主机上

但是,Pod 亲和性调度相比于节点亲和性调度更灵活,前者需要配合标签完成,而后者则不需要,所以更加灵活简便

apiVersion: v1
kind: Pod
metadata:
  name: pod-first
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-second
  labels:
    app: db
    tier: backend
spec:
  containers:
  - name: busybox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ["sh", "-c", "sleep 3600"]
  affinity:
    # Pod 亲和性
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          # 这里需要注意冒号后面要有空格
          matchExpressions:
          - {key: app, operator: In, values: ["myapp"]}
        # 位置标识,这是一个标签,可以通过自定义标签配置位置  
        topologyKey: kubernetes.io/hostname

不出意外,这两个Pod会运行在同一个节点上,因为topologyKey使用的是kubernetes.io/hostname标签,它会将两个Pod调度到值相同的节点上

所以,如果想让Pod运行在不同机器但是同一机房或者机柜,只需要预先定义标签,topologyKey选择对应标签即可。

results matching ""

    No results matching ""