Node Affinity

NodeSelector不能实现Or或者Not逻辑

image-20200321065708440

由于多种原因,可能需要在特定工作节点上运行 pod。

原因可能有多种,其中节点硬件是常见原因之一。

Node Affinity是调度程序用来确定 pod 可以放置在哪里的一组规则。

Node Affinity在概念上类似“nodeSelector - 它允许您根据节点上的标签来限制您的 pod 可以被调度到哪些节点上

它有两种模式:

Sr Types Description
1 requiredDuringSchedulingIgnoredDuringExecution Hard Preference
2 preferredDuringSchedulingIgnoredDuringExecution Soft Preference

实践

requiredDuringSchedulingIgnoredDuringExecution:

apiVersion: v1
kind: Pod
metadata:
  name: node-affinity
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
            - key: disk
              operator: In
              values:
                - ssd
  containers:
    - name: node-affinity
      image: nginx

建立的pod在disk=ssd的node上:

node-affinity 1/1 Running 0 4m51s 192.168.217.125 ip-192-168-209-134.us-west-2.compute.internal <none> <none>

也可以使用NotIn语法:

apiVersion: v1
kind: Pod
metadata:
  name: node-affinity
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
            - key: disk
              operator: NotIn
              values:
                - ssd
  containers:
    - name: node-affinity
      image: nginx

建立的pod如下,会在另外两个节点上选一个:

node-affinity 1/1 Running 0 86s 192.168.165.38 ip-192-168-175-56.us-west-2.compute.internal <none> <none>

如果匹配不到label(disk:ssddd),创建的pod为pending状态:

apiVersion: v1
kind: Pod
metadata:
  name: node-affinity
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
            - key: disk
              operator: In
              values:
                - ssddd
  containers:
    - name: node-affinity
      image: nginx

结果:

[@BDSZYF000132741:pod]$ kubectl describe pod node-affinity
Name: node-affinity
Namespace: default
Priority: 0
Node: <none>
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"node-affinity","namespace":"default"},"spec":{"affinity":{"nodeAffini...
              kubernetes.io/psp: eks.privileged
Status: Pending
IP:           
Containers:
  node-affinity:
    Image: nginx
    Port: <none>
    Host Port: <none>
    Environment: <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-k2gmx (ro)
Conditions:
  Type Status
  PodScheduled False 
Volumes:
  default-token-k2gmx:
    Type: Secret (a volume populated by a Secret)
    SecretName: default-token-k2gmx
    Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type Reason Age From Message
  ---- ------ ---- ---- -------
  Warning FailedScheduling 11s default-scheduler 0/3 nodes are available: 3 node(s) didn't match node selector.

preferredDuringSchedulingIgnoredDuringExecution:

如果匹配不到key:value,会从所有节点中选择一个建立pod。

apiVersion: v1
kind: Pod
metadata:
  name: node-affinity
spec:
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: disk
            operator: In
            values:
            - ssddd
  containers:
    - name: node-affinity
      image: nginx