All Products
Search
Document Center

Deploy and use ack-kubernetes-elastic-workload in an ACK cluster

Last Updated: Apr 14, 2021

This topic describes how to deploy and use ack-kubernetes-elastic-workload in a Container Service for Kubernetes (ACK) cluster.

Prerequisites

An ACK cluster is created and a virtual node is deployed in the cluster.

Background information

Kubernetes provides elasticity at the scheduling layer for pods and at the resource layer for nodes. Kubernetes can use Horizontal Pod Autoscaler (HPA), Cron Horizontal Pod Autoscaler(CronHPA), and Vertical Pod Autoscaling (VPA) models to scale pods and use Cluster Autoscaler and Virtual Kubelet to scale nodes. The scheduling and resource layers are decoupled by pods. This mechanism ensures that responsibilities are segregated between these layers. However, after the layers are decoupled, simple scheduling policies must be separately configured for each layer and can be used in combination. This makes it impossible to implement finer-grained scheduling policies. In Kubernetes, pod is the minimum lifecycle management unit. The pods managed by traditional Kubernetes load controllers such as Deployments and StatefulSets share scheduling policies. Therefore, to control how to allocate a workload across different resources in a find-grained manner, you can use ack-kubernetes-elastic-workload.

Deploy ack-kubernetes-elastic-workload

  1. Log on to the ACK console.

  2. In the left-side navigation pane, choose Marketplace > App Catalog.

  3. On the Alibaba Cloud Apps tab, find and click ack-kubernetes-elastic-workload.

    A large number of applications are displayed on the Alibaba Cloud Apps page. You can enter a keyword in the search box in the upper-right corner to search for ack-kubernetes-elastic-workload.

  4. In the Deploy section of the App Catalog - ack-kubernetes-elastic-workload page, select a cluster to deploy ack-kubernetes-elastic-workload and click Create.ack-kubernetes-elastic-workload 1

  5. Check whether ack-kubernetes-elastic-workload is deployed.

    1. In the left-side navigation pane, click Clusters.

    2. Click the ID of the cluster that you selected in the previous step.

    3. In the left-side navigation pane, choose Applications > Helm.

    4. Check whether ack-kubernetes-elastic-workload is in the Deployed state.Deploy ack-kubernetes-elastic-workload

Use an elastic workload

Assume that capacity is planned for an application and that the application is expected to have up to four replicas to run on ECS instances. Two replicas are retained during off-peak hours. If the number of replicas is greater than four, the extra replicas are scheduled to the virtual node. This prevents other applications on the ECS instances from being affected.

Kubernetes schedules workloads and manages the lifecycle of workloads. To implement the preceding scenario, you must perform the following operations:

  • Control how the scheduling policy changes when the number of replicas reaches a specific value.

  • Prioritize specific pods for lifecycle management.

This section describes how to use ack-kubernetes-elastic-workload to perform the preceding operations for an elastic workload.

  1. Create an application by using a Deployment.

    apiVersion: apps/v1 
    kind: Deployment
    metadata:
      name: nginx-deployment-basic
      labels:
        app: nginx
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
        #  nodeSelector:
        #    env: test-team
          containers:
          - name: nginx
            image: nginx:1.7.9 
            ports:
            - containerPort: 80
  2. Define an elastic workload.

    apiVersion: autoscaling.alibabacloud.com/v1beta1
    kind: ElasticWorkload
    metadata:
      name: elasticworkload-sample
    spec:
      sourceTarget:
        name: nginx-deployment-basic
        kind: Deployment
        apiVersion: apps/v1
        min: 2        #The minimum number of replicas.
        max: 4        #The maximum number of replicas.
      replicas: 6
      elasticUnit:
      - name: virtual-kubelet
        labels:
          virtual-kubelet: "true"
        annotations:
          virtual-kubelet: "true"
        nodeSelector:
          type: "virtual-kubelet"
        tolerations:
        - key: "virtual-kubelet.io/provider"
          operator: "Exists"
    

    ack-kubernetes-elastic-workload is used similarly to an HPA. ack-kubernetes-elastic-workload is deployed as an external add-on and does not affect your existing business.Deploy ack-kubernetes-elastic-workload 2

    You can use ack-kubernetes-elastic-workload to create elastic workloads. An elastic workload listens to a source workload, and can clone and generate replicas for elastic units based on the predefined scheduling policies. If the number of replicas in an elastic workload reaches the threshold, the numbers of replicas scheduled on the source workload and on the elastic unit are dynamically adjusted.

    Typically, an elastic workload consists of the following two parts:

    • SourceTarget: defines the type of the source workload and the range for the number of replicas.

    • elasticUnit: an array that defines the scheduling policy for the elastic unit. If you need to define scheduling policies for multiple elastic units, specify related parameters in the order shown in the template.

    In this example:

    • As defined in sourceTarget, the number of replicas for the source workload ranges from two to four. In this case, if the elastic workload has two to four replicas, replicas are scheduled to the source workload. If the number of replicas is greater than four, the extra replicas are scheduled to the elastic unit (Virtual Node virtual-kubelet).

    • In virtual-kubelet, you can define a scheduling policy that is exclusive to the elastic unit. The policy includes labels, annotations, nodeSelector, affinity, and tolerations.

  3. Check the deployment result.

    • View the status of the elastic workload.

      kubectl describe ew elasticworkload-sample

      A command output similar to the following one is returned. The value of Desired Replicas in the Status section indicates the number of replicas scheduled to the elastic unit:

      Name:         elasticworkload-sample
      Namespace:    default
      Labels:       <none>
      Annotations:  <none>
      API Version:  autoscaling.alibabacloud.com/v1beta1
      Kind:         ElasticWorkload
      Metadata:
        Creation Timestamp:  2020-05-06T03:43:41Z
        Generation:          27
        Resource Version:    20635284
        Self Link:           /apis/autoscaling.alibabacloud.com/v1beta1/namespaces/default/elasticworkloads/elasticworkload-sample
        UID:                 0e9205ff-38b8-43b7-9076-ffa130f26ef4
      Spec:
        Elastic Unit:
          Annotations:
            Virtual - Kubelet:  true
          Labels:
            Virtual - Kubelet:  true
          Name:                 demo
          Node Selector:
            Type:  virtual-kubelet
          Tolerations:
            Key:       virtual-kubelet.io/provider
            Operator:  Exists
        Replicas:      6
        Source Target:
          API Version:  apps/v1
          Kind:         Deployment
          Max:          2
          Min:          0
          Name:         nginx-deployment-basic
      Status:
        Elastic Units Status:
          Desired Replicas:  4
          Name:              nginx-deployment-basic-unit-virtual-kubelet
          Update Timestamp:  2020-05-07T12:38:27Z
        Replicas:            6
        Selector:            app=nginx
        Source Target:
          API Version:       apps/v1
          Desired Replicas:  2
          Kind:              Deployment
          Name:              nginx-deployment-basic
          Update Timestamp:  2020-05-07T12:38:27Z
      Events:                <none>
    • View the status of pods.

      kubectl get pod -o wide

      A command output similar to the following one is returned. The output indicates that the elastic workload has cloned Deployments and pods, and that pod replicas in these Deployments are dynamically scheduled based on the predefined scheduling policy.

      NAME                                               READY   STATUS    RESTARTS   AGE    IP             NODE                     NOMINATED NODE   READINESS GATES
      nginx-deployment-basic-7ff9955f89-djxwv            1/1     Running   0          138m   172.20.1.151   cn-hangzhou.10.0.5.212   <none>           <none>
      nginx-deployment-basic-7ff9955f89-hrw2z            1/1     Running   0          138m   172.20.1.27    cn-hangzhou.10.0.5.208   <none>           <none>
      nginx-deployment-basic-unit-demo-8bb586568-4f8xt   1/1     Running   0          138m   10.1.76.63     virtual-node-eci-1       <none>           <none>
      nginx-deployment-basic-unit-demo-8bb586568-bl5pd   1/1     Running   0          138m   10.1.76.65     virtual-node-eci-0       <none>           <none>
      nginx-deployment-basic-unit-demo-8bb586568-ndbp8   1/1     Running   0          138m   10.1.76.64     virtual-node-eci-0       <none>           <none>
      nginx-deployment-basic-unit-demo-8bb586568-vx9jx   1/1     Running   0          138m   10.1.76.62     virtual-node-eci-2       <none>           <none>

In addition, HPAs can be used for elastic workloads. Elastic workloads can dynamically adjust the number of replicas scheduled to each elastic unit based on the status of HPAs. For example, if the number of replicas in a Deployment changes from six to four for an elastic workload, the elastic workload preferentially reduces the replicas on elastic units. Sample code:

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: elastic-workload-demo
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: autoscaling.alibabacloud.com/v1beta1
    kind: ElasticWorkload
    name: elasticworkload-sample
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50

In conclusion, elastic workloads can generate Deployments by cloning and overriding scheduling policies. This allows you to manage scheduling policies. Elastic workloads can also adjust the number of replicas scheduled to source workloads and adjust the number of replicas scheduled to elastic units within the specified ranges. This allows you to prioritize specific pods for lifecycle management.