You can use ack-kubernetes-elastic-workload to create elastic workloads. An elastic workload listens to a source workload and can clone elastic units for extra pod replicas based on the predefined scheduling policy. If the number of pod replicas in an elastic workload reaches the threshold, the numbers of pod replicas scheduled on the source workload and elastic units are adjusted. This topic describes how to deploy and use ack-kubernetes-elastic-workload in a cluster of Container Service for Kubernetes (ACK) cluster.
Prerequisites
- An ACK cluster is created. For more information, see Create a cluster of ACK Managed Edition.
- ack-virtual-node is deployed in the ACK cluster. For more information, see Use ECI in Container Service for Kubernetes.
Deploy ack-kubernetes-elastic-workload
Create and use an elastic workload
- Change the pod scheduling policy when the number of pod replicas reaches a threshold.
- Change the lifecycle to prioritize certain pods.
This section describes how to use ack-kubernetes-elastic-workload to perform the preceding operations for a workload.
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:
virtual-kubelet: "true"
annotations:
virtual-kubelet: "true"
nodeSelector:
type: "virtual-kubelet"
tolerations:
- key: "virtual-kubelet.io/provider"
operator: "Exists"
# min: 0 You can specify the maximum and minimum numbers of pod replicas in each elastic unit.
# max: 10
The preceding template shows how to create an elastic workload for the application. Similar to the use of Horizontal Pod Autoscaler (HPA), ack-kubernetes-elastic-workload is deployed as an external add-on and does not interfere with your workload.
- SourceTarget: defines the type of the source workload and the range for the number of pod replicas.
- elasticUnit: an array that defines the scheduling policies for the elastic unit. If you need to define multiple elastic units, specify related parameters in the same order as shown in the template.
In this example, the number of pod replicas for the source workload ranges from 2 to 4. This indicates that if the elastic workload has 2 to 4 pod replicas, pod replicas are scheduled to the source workload. If the number is greater than 4, extra pod replicas are scheduled to the elastic unit. Pod replicas are scheduled based on the scheduling policies specified in the preceding template, such as the label, annotation, nodeSelector, affinity, and toleration parameters.
An elastic workload listens to a source workload and can clone elastic units for extra pod replicas to be scheduled. If the number of pod replicas in an elastic workload reaches the threshold, the numbers of pod replicas scheduled on the source workload and elastic units are adjusted.
After you run the template for the elastic workload, you can run the following command to view the status of the elastic workload. The value of the Desired Replicas field in the Status section of the result indicates the number of pod replicas 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: 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>
After you run the template for the elastic workload, you can run the following command to view the status of pods: The following result shows that new Deployments and pods are cloned based on the source workload. You can also find that the number of pod replicas in these Deployments complies with scheduling policy.
kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-basic-7ff9955f89-djxwv 1/1 Running 0 138m 172.20. *.*** cn-hangzhou.10.0. *.*** <none> <none>
nginx-deployment-basic-7ff9955f89-hrw2z 1/1 Running 0 138m 172.2*. *.** cn-hangzhou.10.0. *.*** <none> <none>
nginx-deployment-basic-unit-demo-8bb586568-4f8xt 1/1 Running 0 138m 10.1. **.** virtual-node-eci-1 <none> <none>
nginx-deployment-basic-unit-demo-8bb586568-bl5pd 1/1 Running 0 138m 10.1. **.** virtual-node-eci-0 <none> <none>
nginx-deployment-basic-unit-demo-8bb586568-ndbp8 1/1 Running 0 138m 10.1. **.** virtual-node-eci-0 <none> <none>
nginx-deployment-basic-unit-demo-8bb586568-vx9jx 1/1 Running 0 138m 10.1. **.** virtual-node-eci-2 <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