节点选择(NodeSelector、NodeAffinity)

节点选择器NodeSelector

NodeSelector会将Pod根据定义的标签选定到匹配的Node上去。
示例:

cat > nodeselector.yaml <

应用yaml

kubectl apply -f nodeselector.yaml

查看Pod状态

kubectl describe po nginx-ssd

给Node打标签

kubectl label node rocky9-2 disktype=ssd

查看Node Iabel

kubectl get node --show-labels

查看Pod信息

kubectl describe po nginx-ssd |grep -i node
kebectl get po -o wide

节点亲和性NodeAffinity

关键词:

  • requiredDuringSchedulingIgnoredDuringExecution:表示强匹配,必须要满足
  • preferredDuringSchedulingIgnoredDuringExecution:表示弱匹配,尽可能满足,但不保证

示例:

apiVersion: v1
kind: Pod
metadata:
  name: with-node-affinity
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:  ##必须满足下面匹配规则
        nodeSelectorTerms:
        - matchExpressions:
          - key: env
            operator: In  ##逻辑运算符支持:In,NotIn,Exists,DoesNotExist,Gt,Lt
            values:
            - test
            - dev
      preferredDuringSchedulingIgnoredDuringExecution: ##尽可能满足,但不保证
      - weight: 1
        preference:
          matchExpressions:
          - key: project
            operator: In
            values:
            - aminglinux
  containers:
  - name: with-node-affinity
    image: redis:6.0.6

说明:
匹配逻辑:

  • 同时指定Node Stlector和Node Affinity,两者必须同时满足;
  • Node Affinity中指定多组nodeSelectorTerms,只需要一组满足就可以;
  • 当在nodeSelectorTerms中包含多组matchExpressions,必须全部满足才可以;

演示示例:
编辑pod的yaml

cat > nodeAffinity.yaml << EOF
apiVersion: v1
kind: Pod
metadata:
  name: node-affinity
spec:
  containers:
    - name: my-container
      image: nginx:1.23.2
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
              - key: special-node
                operator: Exists
EOF

给其中一个节点定义标签

kubectl labjl nodes rocky9-3 special-node=true

生效Pod yaml

kubectl apply -f nodeAffinity.yaml

检查Pod所在node

kubectl get po -o wide

你可能感兴趣的:(kubernetes,容器,云原生)