指定ECS和ECI的资源分配

ACK集群中部署服务时,您可以使用容忍度和节点亲和性来声明只使用ECSECI资源,或者在ECS资源不足时自动申请ECI资源。通过配置调度策略,您可以在不同工作负载场景下实现对弹性资源的不同需求。

相关概念

  • 污点(Taints):节点上设置的规则,用于排斥某些Pod被调度到该节点上。

  • 容忍度(Tolerations):应用于Pod,允许该Pod调度时忽略节点上的污点。

  • 节点亲和性(Node affinity):Pod的属性,用于将Pod调度到特定标签的节点。包括以下两类:

    • requiredDuringSchedulingIgnoredDuringExecution:硬性要求,调度时必须满足的规则。调度器只有在规则被满足的时候才能执行调度。

    • preferredDuringSchedulingIgnoredDuringExecution:优先偏好,调度时优先考虑的规则。调度器会尝试寻找满足对应规则的节点。如果找不到匹配的节点,调度器仍然会调度该Pod。

更多信息,请参见Taints and TolerationsNode affinity

前提条件

  • 集群类型为ACK托管集群Pro,且版本为v1.22版本及以上。

  • 集群中已部署ack-virtual-node组件,且版本为v2.10.0及以上。关于如何安装和升级ack-virtual-node组件,请参见虚拟节点

  • 集群中的kube-scheduler组件的版本为5.9及以上,并且已开启虚拟节点调度策略。

    组件管理页面找到Kube Scheduler,单击配置,然后确认已选中开启虚拟节点调度

配置示例

ACK集群中部署的虚拟节点默认带有污点:virtual-kubelet.io/provider=alibabacloud:NoSchedule,以避免在您不知情的情况下使用ECI资源。因此,您需要配置对该污点的容忍度,才能将Pod调度到虚拟节点,使用ECI资源。

      tolerations:
      - key: virtual-kubelet.io/provider
        operator: Equal
        value: alibabacloud
        effect: NoSchedule

结合使用容忍度和节点亲和性,可以实现优先级与强制性的混合调度策略,满足不同业务对于资源的不同需求。

  • 优先使用ECS:优先使用ECS资源,当集群内ECS资源不足时,使用ECI资源。

  • 只使用ECI:只使用ECI资源,不使用集群的ECS资源。

  • 只使用ECS:只使用集群现有的ECS资源。

优先使用ECS

由于虚拟节点默认带有污点,您需要配置对于该污点的容忍度,才能将Pod调度到虚拟节点。同时,配置节点亲和性,通过preferredDuringSchedulingIgnoredDuringExecution可以指定Pod调度的偏好:优先调度到未带有type: virtual-kubelet标签的节点(即ECS节点)。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ecs-prefer
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      tolerations:
      - key: virtual-kubelet.io/provider
        operator: Equal
        value: alibabacloud
        effect: NoSchedule
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            preference:
              matchExpressions:
              - key: type
                operator: NotIn
                values:
                - virtual-kubelet
      containers:
      - name: my-container
        image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6
说明

配置节点亲和性时,请注意以下信息:

  • 如果在nodeSelectorTerms中配置多个条件(即多个matchExpressions),则只要其中一个条件满足(各个条件按逻辑或操作组合)时,Pod就可以被调度到对应节点上。如果在单个matchExpressions中配置多个表达式,则只有当所有表达式都满足(各表达式按逻辑与操作组合)时,Pod才能被调度到对应节点上。

  • 使用preferredDuringSchedulingIgnoredDuringExecution实现优先ECS调度时,不能保证仅在ECS资源不足时才调度到ECI上,也会存在ECS资源充足时但Pod被调度到ECI上的情况。如果您有更高的需求,建议结合requiredDuringSchedulingIgnoredDuringExecution配置更细致的强制规则。

如果希望将Pod优先部署在带有指定标签(如label_1=key_1)的ECS节点上,在该类节点资源不足时,使用虚拟节点进行弹性伸缩,您可以通过以下YAML配置。

展开查看YAML详情

apiVersion: apps/v1
kind: Deployment
metadata:
  name: some-ecs-prefer
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      tolerations:
      - key: virtual-kubelet.io/provider
        operator: Equal
        value: alibabacloud
        effect: NoSchedule
      affinity:
        nodeAffinity:
    # 指定Pod必须调度到带有label_1:key_1或type:virtual-kubelet标签的节点上。
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: label_1
                operator: In
                values:
                - key_1
            - matchExpressions:
              - key: type
                operator: In
                values:
                - virtual-kubelet
    # 指定Pod优先调度到带有label_1:key_1标签的节点上,其次调度到带有type:virtual-kubelet标签的节点上。
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            preference:
              matchExpressions:
              - key: label_1
                operator: In
                values:
                - key_1
          - weight: 1
            preference:
              matchExpressions:
              - key: type
                operator: In
                values:
                - virtual-kubelet
      containers:
      - name: my-container
        image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6

只使用ECI

由于虚拟节点默认带有污点,您需要配置对于该污点的容忍度,才能将Pod调度到虚拟节点。同时,配置节点亲和性,通过requiredDuringSchedulingIgnoredDuringExecution可以指定Pod必须调度到带有type: virtual-kubelet标签的节点(即虚拟节点)。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: eci-only
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      tolerations:
      - key: virtual-kubelet.io/provider
        operator: Equal
        value: alibabacloud
        effect: NoSchedule
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: type
                operator: In
                values:
                - virtual-kubelet
      containers:
      - name: my-container
        image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6
            

只使用ECS

由于虚拟节点默认带有污点,因此只要您未配置对于该污点的容忍度,Pod将只会调度到ECS节点。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ecs-only
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6