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 VNode is deployed in the cluster.
For more information about how to create an ACK cluster, see Create an ACK managed cluster.
For more information about how to deploy a VNode, see Step 1: Deploy ack-virtual-node in ACK clusters.
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 fine-grained manner, you can use ack-kubernetes-elastic-workload.
Install ack-kubernetes-elastic-workload
Log on to the ACK console.
In the left-side navigation pane, choose Marketplace > Marketplace.
Find the ack-kubernetes-elastic-workload application and click the app.
A large number of applications are displayed on the App Catalog tab. You can enter a keyword in the search box to search for ack-kubernetes-elastic-workload.
In the upper-right corner of the application details page, click Deploy.
In the Deploy wizard, configure the basic information and parameters.
In the Basic Information step, select a cluster and namespace, and specify a name for the release. Then, click Next.
In the Parameters step, configure parameters based on your requirements and click OK.
In the left-side navigation pane, click Clusters. In the left-side navigation pane of the Clusters page, choose Applications > Helm to view the status of ack-kubernetes-elastic-workload. If Deployed is displayed in the Status column, ack-kubernetes-elastic-workload is installed.
Use an elastic workload
For example, capacity is planned for an application and that the application is expected to have up to four replicas to run on Elastic Compute Service (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.
You must resolve the following issues when you use Kubernetes to schedule pods and manage the lifecycle of pods:
Control how the scheduling policy changes when the number of replicas reaches a specific value.
How to prioritize specific pods when Kubernetes manages the lifecycle of pods.
This section describes how to use ack-kubernetes-elastic-workload to perform the preceding operations for an elastic workload.
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: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80 resources: requests: # If you want to use HPA for the application, you must specify requests. cpu: "500m" memory: "1024Mi"
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: alibabacloud.com/eci: "true"
ack-kubernetes-elastic-workload is used in a similar manner as an HPA. ack-kubernetes-elastic-workload is deployed as an external add-on and does not affect your existing business.
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 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 an elastic unit. To define scheduling policies for multiple elastic units, specify scheduling policies 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 elasticUnit, the elastic unit is defined as the virtual-kubelet virtual node, which corresponds to the
labels: alibabacloud.com/eci=true
scheduling policy.
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: 2021-05-21T01:53:58Z Generation: 4 Managed Fields: API Version: autoscaling.alibabacloud.com/v1beta1 Fields Type: FieldsV1 fieldsV1: f:spec: .: f:elasticUnit: f:replicas: f:sourceTarget: .: f:apiVersion: f:kind: f:max: f:min: f:name: Manager: Apache-HttpClient Operation: Update Time: 2021-05-21T01:53:58Z API Version: autoscaling.alibabacloud.com/v1beta1 Fields Type: FieldsV1 fieldsV1: f:status: .: f:elasticUnitsStatus: f:replicas: f:selector: f:sourceTarget: .: f:apiVersion: f:desiredReplicas: f:kind: f:name: f:updateTimestamp: Manager: manager Operation: Update Time: 2021-05-21T01:56:45Z Resource Version: 8727 Self Link: /apis/autoscaling.alibabacloud.com/v1beta1/namespaces/default/elasticworkloads/elasticworkload-sample UID: c4a508aa-2702-4d17-ac25-e6a207c0761a 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-05-21T01:56:45Z Replicas: 6 Selector: app=nginx Source Target: API Version: apps/v1 Desired Replicas: 4 Kind: Deployment Name: nginx-deployment-basic Update Timestamp: 2021-05-21T01:56:45Z Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SourceUpdate 12m ElasticWorkload Source Target scale from 2 to 4 Normal UnitCreation 12m ElasticWorkload ElasticWorkloadUnit nginx-deployment-basic-unit-virtual-kubelet created Normal ElasticWorkloadUpdate 9m27s (x9 over 12m) ElasticWorkload ElasticWorkload update Normal UnitUpdate 9m27s (x8 over 12m) ElasticWorkload ElasticWorkloadUnit virtual-kubelet has been updated
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-5bf87f5f59-22jnw 1/1 Running 0 16m 10.34.0.131 cn-beijing.172.16.0.1 <none> <none> nginx-deployment-basic-5bf87f5f59-gfp24 1/1 Running 0 13m 10.34.0.133 cn-beijing.172.16.0.1 <none> <none> nginx-deployment-basic-5bf87f5f59-pw2zx 1/1 Running 0 13m 10.34.0.134 cn-beijing.172.16.0.1 <none> <none> nginx-deployment-basic-5bf87f5f59-qvh7m 1/1 Running 0 16m 10.34.0.132 cn-beijing.172.16.0.1 <none> <none> nginx-deployment-basic-unit-virtual-kubelet-65fb6f4cd7-48ssb 1/1 Running 0 13m 172.16.22.157 virtual-kubelet-cn-beijing-e <none> <none> nginx-deployment-basic-unit-virtual-kubelet-65fb6f4cd7-gjqhm 1/1 Running 0 13m 172.16.22.158 virtual-kubelet-cn-beijing-e <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.