首页 > 其他分享 >每天一点基础K8S--K8S中的调度策略--节点亲和性、反亲和性

每天一点基础K8S--K8S中的调度策略--节点亲和性、反亲和性

时间:2022-11-29 17:47:22浏览次数:47  
标签:node -- 亲和性 worker affinity test pod K8S

K8S中的调度策略--节点亲和性、pod亲和性

上面实验了nodeName和nodeSelector,其中,nodeName是通过节点的名称进行区分,在一些特定场景下还是很有用的,如果将节点调度到某一高性能节点。但是nodeName还是显得有点过于严苛。nodeSelector则是通过node的标签进行选择。同样的,通过node的label进行pod资源调度的还有nodeAffinity、podAffinity、podantiAffinity
亲和性语法
kubectl explain pod.spec.affinity
requiredDuringSchedulingIgnoredDuringExecution: 调度器只有在规则被满足的时候才能执行调度。此功能类似于 nodeSelector, 但其语法表达能力更强。

preferredDuringSchedulingIgnoredDuringExecution: 调度器会尝试寻找满足对应规则的节点。如果找不到匹配的节点,调度器仍然会调度该 Pod。


IgnoredDuringExecution 意味着如果节点标签在 Kubernetes 调度 Pod 后发生了变更,Pod 仍将继续运行
节点亲和性nodeAffinity--强亲和性
requiredDuringSchedulingIgnoredDuringExecution
[root@master-worker-node-1 pod]# cat test-node-affinity.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: test-node-affinity-1
  labels:
    function: test-node-affinity
spec:
  containers:
  - name: test-node-affinity-1
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ['/bin/sh','-c','sleep 12345']
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: test-node-affinity
            operator: In
            values: 
            - target-node
创建pod
[root@master-worker-node-1 pod]# kubectl apply -f test-node-affinity.yaml 
pod/test-node-affinity-1 created

因为没有node满足这个label,故pod无法运行
[root@master-worker-node-1 pod]# kubectl get nodes --show-labels | grep target

[root@master-worker-node-1 pod]# kubectl get pods -w 
NAME                   READY   STATUS    RESTARTS   AGE
test-node-affinity-1   0/1     Pending   0          3m50s

pod处于pending状态是因为无法调度
[root@master-worker-node-1 pod]# kubectl describe pods test-node-affinity-1  |  tail -4
Events:
  Type     Reason            Age    From               Message
  ----     ------            ----   ----               -------
  Warning  FailedScheduling  4m42s  default-scheduler  0/4 nodes are available: 2 node(s) had untolerated taint {node-role.kubernetes.io/control-plane: }, 4 node(s) didn't match Pod's node affinity/selector. preemption: 0/4 nodes are available: 4 Preemption is not helpful for scheduling.
给某一节点添加label
[root@master-worker-node-1 pod]# kubectl label nodes only-worker-node-3 test-node-affinity=target-node
node/only-worker-node-3 labeled


一会发现后,pod运行正常
[root@master-worker-node-1 pod]# kubectl get pods -w 
NAME                   READY   STATUS    RESTARTS   AGE
test-node-affinity-1   0/1     Pending   0          3m50s
test-node-affinity-1   0/1     Pending   0          6m51s
test-node-affinity-1   0/1     ContainerCreating   0          6m51s
test-node-affinity-1   0/1     ContainerCreating   0          6m52s
test-node-affinity-1   1/1     Running             0          6m53s

节点亲和性nodeAffinity--弱亲和性
preferredDuringSchedulingIgnoredDuringExecution
先删除环境中所有node的label
[root@master-worker-node-1 pod]# kubectl label nodes only-worker-node-3 test-node-affinity-
node/only-worker-node-3 unlabeled
[root@master-worker-node-1 pod]# cat test-node-affinity-2.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: test-node-affinity-2
  labels:
    function: test-node-affinity
spec:
  containers:
  - name: test-node-affinity-2
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ['/bin/sh','-c','sleep 12345']
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 40
        preference:
          matchExpressions:
          - key: test-node-affinity
            operator: In
            values:
            - target-node-1
创建pod
[root@master-worker-node-1 pod]# kubectl apply -f test-node-affinity-2.yaml 
pod/test-node-affinity-2 created

虽然没有任何一个node满足pod prefer的label要求,但是pod仍然可以正常运行
[root@master-worker-node-1 pod]# kubectl get pods -o wide
NAME                   READY   STATUS    RESTARTS   AGE   IP            NODE                 NOMINATED NODE   READINESS GATES
test-node-affinity-2   1/1     Running   0          26s   10.244.54.8   only-worker-node-4   <none>           <none>
弱亲和性里面的weight字段需要有两个选项才有意思
[root@master-worker-node-1 pod]# cat test-node-affinity-3.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: test-node-affinity-3
  labels:
    function: test-node-affinity
spec:
  containers:
  - name: test-node-affinity-3
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ['/bin/sh','-c','sleep 12345']
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 40    # 有两个可选项的时候,weight才用意义。
        preference:
          matchExpressions:
          - key: test-node-affinity
            operator: In
            values:
            - target-node-1
      - weight: 80
        preference:
          matchExpressions:
          - key: test-node-affinity
            operator: In
            values:
            - target-node-2
给node添加对应的label
[root@master-worker-node-1 pod]# kubectl label nodes only-worker-node-3 test-node-affinity=target-node-1
node/only-worker-node-3 labeled
[root@master-worker-node-1 pod]# kubectl label nodes only-worker-node-4 test-node-affinity=target-node-2
node/only-worker-node-4 labeled

因为target-node-2的weight的权重更大,按理应该会被调度到only-worker-node-4上

[root@master-worker-node-1 pod]# kubectl apply -f test-node-affinity-3.yaml 
pod/test-node-affinity-3 created
[root@master-worker-node-1 pod]# kubectl get pods -o wide
NAME                   READY   STATUS    RESTARTS   AGE   IP            NODE                 NOMINATED NODE   READINESS GATES
test-node-affinity-2   1/1     Running   0          18m   10.244.54.8   only-worker-node-4   <none>           <none>
test-node-affinity-3   1/1     Running   0          12s   10.244.54.9   only-worker-node-4   <none>           <none>
节点反亲和性
节点反亲和性没有专门的语法,可以通过operator中的
NotIn、DoesNotExist和node污点实现。
小结

node节点亲和性分为强亲和性(required)和弱亲和性(preferred)

required是只有满足了才会被调度,perferred是尽力满足,无法满足也能调度

node反亲和性可通过NotIn、DoesNotExist和node污点实现

标签:node,--,亲和性,worker,affinity,test,pod,K8S
From: https://www.cnblogs.com/woshinidaye123/p/16936028.html

相关文章

  • NAT基础
    1.静态NAT过程如果希望一台主机优先使用某个关联地址,或者想要外部网络使用一个指定的公网地址访问内部服务器时,可以使用静态NAT。但是在大型网络中,这种一对一的IP地......
  • Go 语言基本数据类型
    packagemainimport( "fmt" "math" "strings" "unsafe")funcmain(){ /* Golang数据类型分为基本数据类型和复合数据类型 基本数据类型有:整形、浮点型、布......
  • 背包问题——分组背包
    分组背包1.定义分组背包,通俗的讲就是,给你N组物品,然后每一组你至多选择一个物品(也可以不选),每个物品都有自己的体积和价值,现在给你一个容里为M的背包,让你用这个背包装物......
  • 一篇文章,带你彻底掌握接口测试!
    一、什么是接口测试?所谓接口,是指同一个系统中模块与模块间的数据传递接口、前后端交互、跨系统跨平台跨数据库的对接。而接口测试,则是通过接口的不同情况下的输入,去对比输......
  • 常量和枚举的区别
    常量和枚举最大的不同是,枚举是可以穷举的“常量”,比如性别,只有那几种;而常量则是可以有无限多种,一般是用来处理魔法值的,让魔法值限定在某个类里,比如错误通知内容,短信通知内......
  • 反转 (开关问题)
    [USACO07MAR]FaceTheRightWayG$N$头牛排成一列$1\leN\le5000$。每头牛或者向前或者向后。为了让所有牛都面向前方,农夫每次可以将$K$头连续的牛转向$1\leK......
  • try-cathch- finally 捕获错误 throw抛出异常
    语法结构强壮代码try{可能会错的代码}catch(err){捕获错误}finally{不管语法正确错误都会执行不会影响后面代码的执行} <body><p>123</p><script>......
  • 前端一键复制,隐藏元素亦可用
    varrange=document.createRange();range.selectNode(document.querySelector('#元素id'));varselection=window.getSelection(......
  • Rockwell EDI 855 采购订单确认报文详解
    罗克韦尔自动化与国内12家授权分销商,124家认可的系统集成商,30多家亚太区的Encompass战略合作伙伴和全球战略联盟,共同为制造业企业提供广泛的世界一流的产品、解决方案与......
  • 什么是自签名证书?以及如何创建它
    自签名SSL证书是一种数字证书,未经公开信任的​​证书颁发机构(CA)​​签名。自签名证书被认为不同于传统的CA签名证书,因为它们是由负责与证书关联的网站或软件的公司......