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
- An ACK cluster is created. For more information, see Create an ACK managed cluster.
- If the created ACK cluster is not a serverless Kubernetes (ASK) cluster, you must deploy ack-virtual-node in the cluster. You must use ack-virtual-node v2.0.0.102-045a06eb4-aliyun or later versions. For more information, see Use ECI in Container Service for Kubernetes.
Deploy ack-kubernetes-elastic-workload
Use an elastic workload
- 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.
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
# 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.
- SourceTarget: defines the type of the source workload, and the maximum and minimum numbers of replicated pods.
- 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.

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.
kubectl describe ew elasticworkload-sample # same as kubectl get elasticworkload
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.
kubectl get pod -o wide
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>
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