You can use ack-kubernetes-elastic-workload to create elastic workloads. The ack-kubernetes-elastic-workload component monitors a Kubernetes workload, and replicates and generates workloads based on the predefined scheduling policies of elastic units. If the number of replicated pods in an elastic workload exceeds a threshold, the numbers of replicated pods scheduled to the source workload and elastic units are adjusted. This topic describes how to install and use ack-kubernetes-elastic-workload in a Container Service for Kubernetes (ACK) cluster.

Prerequisites

Deploy ack-kubernetes-elastic-workload

  1. Log on to the ACK console.
  2. In the left-side navigation pane of the ACK console, choose Marketplace > App Catalog.
  3. On the Marketplace page, click the App Catalog tab. Find and click ack-kubernetes-elastic-workload.
  4. On the ack-kubernetes-elastic-workload page, click Deploy.
  5. In the Deploy wizard, select a cluster and namespace, and then click Next.
  6. On the Parameters wizard page, set the parameters and click OK.
    After you deploy ack-kubernetes-elastic-workload, go to the details page of the cluster. In the left-side navigation pane, choose Applications > Helm. You can find that ack-kubernetes-elastic-workload is deployed in the cluster.

Use an elastic workload

You must resolve the following issues when you use Kubernetes to schedule pods and manage the lifecycle of pods:
  • How to modify the pod scheduling policy when the number of replicated pods reaches a threshold.
  • How to prioritize specific pods when Kubernetes manages the lifecycle of pods.

You can use elastic workloads to resolve the preceding issues.

Create an application by using a Deployment based on the following template:
apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
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
Create an elastic workload for the Deployment.
# 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
    max: 4
  replicas: 6
  elasticUnit:
  - name: virtual-kubelet
    labels:
      alibabacloud.com/eci: "true"
    # min: 0 You can specify the maximum and minimum numbers of replicated pods for each elastic unit. 
    # max: 10

The preceding template shows how to create an elastic workload for the Deployment. Similar to Horizontal Pod Autoscaler (HPA), ack-kubernetes-elastic-workload is deployed as an external plug-in without intrusion to your workloads.

Typically, the configurations of an elastic workload include the following parts:
  1. SourceTarget: defines the type of the source workload, and the maximum and minimum numbers of replicated pods.
  2. elasticUnit: an array that defines the scheduling policy for an elastic unit. To define scheduling policies for multiple elastic units, specify related parameters in the order shown in the template.
In this example, the number of replicated pods provisioned for the source workload ranges from 2 to 4. If the elastic workload has 2 to 4 replicated pods, all the pods are scheduled to the source workload. If the elastic workload contains more than 4 pods, the remaining pods are scheduled to the virtual-kubelet elastic unit. Replicated pods are scheduled based on the scheduling policy of virtual-kubelet, such as the label, annotation, nodeSelector, affinity, and toleration parameters. Scheduling

You can use ack-kubernetes-elastic-workload to create elastic workloads. The ack-kubernetes-elastic-workload component monitors a Kubernetes workload, and replicates and generates workloads based on the predefined scheduling policies of elastic units. If the number of replicated pods in an elastic workload exceeds a threshold, the numbers of replicated pods scheduled to the source workload and elastic units are adjusted.

After you deploy the elastic workload with the preceding template, you can run the following command to query the status of the elastic workload. The value of the Desired Replicas field in the Status section of the output indicates the number of replicated pods that are scheduled to the elastic unit.

Run the following command to query the status of the elastic workload:
kubectl describe ew elasticworkload-sample   # same as kubectl get elasticworkload
Expected output:
Name:         elasticworkload-sample
Namespace:    default
Labels:       <none>
Annotations:  <none>
API Version:  autoscaling.alibabacloud.com/v1beta1
Kind:         ElasticWorkload
Metadata:
  Creation Timestamp:  2021-04-30T06:58:03Z
  Generation:          2
  Resource Version:  1126368056
  Self Link:         /apis/autoscaling.alibabacloud.com/v1beta1/namespaces/default/elasticworkloads/elasticworkload-sample
  UID:               a465de9e-1253-4cbe-8cd5-98393393e990
Spec:
  Elastic Unit:
    Labels:
      alibabacloud.com/eci:  true
    Name:                    virtual-kubelet
  Replicas:                  6
  Source Target:
    API Version:  apps/v1
    Kind:         Deployment
    Max:          4
    Min:          2
    Name:         nginx-deployment-basic
Status:
  Elastic Units Status:
    Desired Replicas:  2
    Name:              nginx-deployment-basic-unit-virtual-kubelet
    Update Timestamp:  2021-04-30T06:58:03Z
  Replicas:            6
  Selector:            app=nginx
  Source Target:
    API Version:       apps/v1
    Desired Replicas:  4
    Kind:              Deployment
    Name:              nginx-deployment-basic
    Update Timestamp:  2021-04-30T06:58:03Z
Events:
  Type    Reason                 Age                  From             Message
  ----    ------                 ----                 ----             -------
  Normal  UnitCreation           110s                 ElasticWorkload  ElasticWorkloadUnit nginx-deployment-basic-unit-virtual-kubelet created
  Normal  ElasticWorkloadUpdate  110s (x2 over 110s)  ElasticWorkload  ElasticWorkload update
  Normal  UnitUpdate             110s                 ElasticWorkload  ElasticWorkloadUnit virtual-kubelet has been updated

After you deploy the elastic workload with the preceding template, you can run the following command to query the status of the pods: The output shows that new Deployments and pods are cloned from the source workload. You can also find that the number of replicated pods in each Deployment complies with the scheduling policy.

Run the following command to query the status of the pods:
kubectl get pod -o wide
Expected output:
NAME                                                           READY   STATUS    RESTARTS   AGE   IP              NODE                            NOMINATED NODE   READINESS GATES
nginx-deployment-basic-769f84b5cf-hdmlw                        1/1     Running   0          46m   172.26.240.69   cn-hangzhou.10.1.84.111         <none>           <none>
nginx-deployment-basic-769f84b5cf-lmd99                        1/1     Running   0          46m   172.26.240.6    cn-hangzhou.10.1.84.112         <none>           <none>
nginx-deployment-basic-769f84b5cf-nbp5c                        1/1     Running   0          30m   172.26.240.75   cn-hangzhou.10.1.84.111         <none>           <none>
nginx-deployment-basic-769f84b5cf-scj68                        1/1     Running   0          30m   172.26.240.11   cn-hangzhou.10.1.84.112         <none>           <none>
nginx-deployment-basic-unit-virtual-kubelet-594f86b5c9-8z876   1/1     Running   0          10m   10.1.84.119     virtual-kubelet-cn-hangzhou-i   <none>           <none>
nginx-deployment-basic-unit-virtual-kubelet-594f86b5c9-drxvq   1/1     Running   0          10m   10.1.84.118     virtual-kubelet-cn-hangzhou-i   <none>           <none>
You can also use HPA together with ack-kubernetes-elastic-workload to scale an elastic workload, as shown in the preceding figure. If a Kubernetes workload is scaled by HPA, ack-kubernetes-elastic-workload adjusts the number of replicated pods scheduled to each elastic unit. For example, if HPA reduces the number of replicated pods in an elastic workload from 6 to 4, ack-kubernetes-elastic-workload preferably reduces the replicated pods of elastic units instead of reducing the number of replicated pods in the source workload.
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
Note ack-kubernetes-elastic-workload creates Deployments by cloning source Deployments and overwriting scheduling policies. This allows you to manage scheduling policies. ack-kubernetes-elastic-workload also adjusts the replicated pods scheduled to source workloads and elastic units. This allows you to prioritize specific pods.