自学内容网 自学内容网

k8s的节点亲和性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 Selector和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 label nodes k8s-worker2 special-node=true

生效Pod yaml
kubectl apply -f nodeAffinity.yaml

检查Pod所在node
root@k8s-master:/home/vagrant# kubectl get po -o wide
NAME            READY   STATUS    RESTARTS   AGE     IP             NODE          NOMINATED NODE   READINESS GATES
node-affinity   1/1     Running   0          3m47s   172.16.126.2   k8s-worker2   <none>           <none>



原文地址:https://blog.csdn.net/weixin_65562581/article/details/144387497

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!