node节点亲和性

目录

1. 硬亲和性

2. 软亲和性


[root@master01 ~]# kubectl explain pods.spec.affinity.nodeAffinity
KIND:     Pod
VERSION:  v1

RESOURCE: nodeAffinity <Object>

DESCRIPTION:
     Describes node affinity scheduling rules for the pod.

     Node affinity is a group of node affinity scheduling rules.

FIELDS:
   preferredDuringSchedulingIgnoredDuringExecution      <[]Object>
     The scheduler will prefer to schedule pods to nodes that satisfy the
     affinity expressions specified by this field, but it may choose a node that
     violates one or more of the expressions. The node that is most preferred is
     the one with the greatest sum of weights, i.e. for each node that meets all
     of the scheduling requirements (resource request, requiredDuringScheduling
     affinity expressions, etc.), compute a sum by iterating through the
     elements of this field and adding "weight" to the sum if the node matches
     the corresponding matchExpressions; the node(s) with the highest sum are
     the most preferred.

   requiredDuringSchedulingIgnoredDuringExecution       <Object>
     If the affinity requirements specified by this field are not met at
     scheduling time, the pod will not be scheduled onto the node. If the
     affinity requirements specified by this field cease to be met at some point
     during pod execution (e.g. due to an update), the system may or may not try
     to eventually evict the pod from its node.

上述:

prefered 表示有节点尽量满足这个位置定义的亲和性,这不是一个必须的条件,软亲和性

require 表示必须有节点满足这个位置定义的亲和性,这是个硬性条件,硬亲和性

1. 硬亲和性

给node01节点打一个标签:

[root@master01 test]# kubectl label nodes node01 test=require
node/node01 labeled
[root@master01 test]# cat node_require.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-node-affinity-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
    - name: myapp
      image: nginx
  affinity:    #用于控制Pod调度的亲和性和反亲和性规则。
    nodeAffinity:   #控制Pod如何调度到节点上。
      requiredDuringSchedulingIgnoredDuringExecution:   # 表示在调度时必须满足的条件,在Pod执行期间可以忽略。
        nodeSelectorTerms:   #包含一个或多个匹配表达式,用于描述节点必须满足的条件。
          - matchExpressions:   #包含一组键、操作符和值的列表,用于描述节点的标签。
              - key: test   #节点标签的键。
                operator: In   #操作符,可以是 In, NotIn, Exists, DoesNotExist, Gt, Lt
                values:      ##一个或多个字符串值。
                  - require
[root@master01 test]# kubectl apply -f node_require.yaml
pod/pod-node-affinity-demo created
[root@master01 test]# kubectl get pod pod-node-affinity-demo -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES
pod-node-affinity-demo   1/1     Running   0          49s   10.244.196.189   node01   <none>           <none>

可以看到pod只能调度到node01上。因为我们只对node01节点打了test=require标签。

做完测试删除标签:

[root@master01 test]# kubectl label node node01 test-
node/node01 labeled

2. 软亲和性

[root@master01 test]# cat node_preferred.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-node-affinity-demo-2
  namespace: default
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
    - name: myapp
      image: ikubernetes/myapp:v1
  affinity:    ##用于控制Pod调度的亲和性和反亲和性规则。
    nodeAffinity:   ##控制Pod如何调度到节点上
      preferredDuringSchedulingIgnoredDuringExecution:   ##表示在调度时偏好但不强制的条件,在Pod执行期间可以忽略。
        - preference:    ##包含一个或多个匹配表达式,用于描述节点应该满足的条件
            matchExpressions:   ##包含一组键、操作符和值的列表,用于描述节点的标签
              - key: test   #节点标签的键
                operator: In    #操作符,可以是 In, NotIn, Exists, DoesNotExist, Gt, Lt
                values:
                  - preferred
          weight: 60    #用于描述偏好的权重,范围从 1 到 100。权重越高,调度器越倾向于将Pod调度到匹配的节点上。

[root@master01 test]# kubectl apply -f node_preferred.yaml
pod/pod-node-affinity-demo-2 created
[root@master01 test]# kubectl get pod  pod-node-affinity-demo-2
NAME                       READY   STATUS    RESTARTS   AGE
pod-node-affinity-demo-2   1/1     Running   0          74s
[root@master01 test]# kubectl get node node01 --show-labels
NAME     STATUS   ROLES    AGE   VERSION   LABELS
node01   Ready    worker   27d   v1.20.6   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux,node-role.kubernetes.io/worker=worker

上面说明软亲和性是可以运行 pod 的,尽管没有运行这个 pod 的节点定义的 test=preferred标签。

Node 节点亲和性针对的是 pod 和 node 的关系,Pod 调度到 node 节点的时候匹配的条件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

峰峰--

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值