You need to enable JavaScript to run this app.
Volcengine Kubernetes Engine

Volcengine Kubernetes Engine

Copy page
Download PDF
Horizontal Pod Autoscaler based on event-driven (KEDA)
Autoscaling based on APIG network metrics
Copy page
Download PDF
Autoscaling based on APIG network metrics
Volcengine Kubernetes Engine (VKE) supports workload autoscaling through KEDA, the event-driven autoscaler add-on, which helps users reduce costs. This topic describes how to achieve workload autoscaling based on network metrics provided by API Gateway (APIG).​
Background information​
KEDA functions as an event-driven autoscaling add-on supported by VKE, which can be connected to multiple data sources and scale out workloads from zero and scale in workloads to zero. By leveraging the network request metrics provided by APIG, KEDA can scale in workloads to zero or scale out workloads from zero. This effectively reduces cloud costs during the idle period, enhances elasticity, and improves service competitiveness.​
Prerequisites​
  • A cluster has been created and needs to be running on Kubernetes v1.24 or later. For details, see Creating clusters.​
  • The keda add-on has been installed in the cluster. For details, see Installing add-ons.​
  • The apig-controller add-on has been installed in the cluster to connect to APIG. For details, see Installing add-ons.​
  • A Volcengine Managed Service for Prometheus (VMP) workspace has been created, and basic authentication has been enabled. For more information, see Creating workspaces.​
Procedure​
Step 1: Create an application and a service​
Deploy an application and a service in the cluster. The following sample code demonstrates how to deploy NGINX.​
​
apiVersion: apps/v1​
kind: Deployment​
metadata:​
name: nginx​
namespace: default​
spec:​
replicas: 2​
selector:​
matchLabels:​
app: nginx​
template:​
metadata:​
labels:​
app: nginx​
spec:​
containers:​
- name: nginx​
image: doc-cn-beijing.cr.volces.com/vke/nginx-demo:v1.0​
ports:​
- containerPort: 80​
---​
apiVersion: v1​
kind: Service​
metadata:​
name: nginx​
namespace: default​
spec:​
internalTrafficPolicy: Cluster​
ipFamilies:​
- IPv4​
ipFamilyPolicy: SingleStack​
ports:​
- name: http​
port: 80​
protocol: TCP​
targetPort: 80​
selector:​
app: nginx​
sessionAffinity: None​
type: ClusterIP​
​
Step 2: Create an APIG Ingress​
Create an APIG Ingress in the cluster in the console. This topic takes the domain name example.com as an example. For detailed instructions, see Creating APIG Ingresses in the console.​
Step 3: Enable APIG instance monitoring​
  1. Log in to APIG console.​
  1. In the left navigation bar, click Instance management. On the page that appears, click the name of the target instance to go to the instance details page.​
  1. Click the Monitoring data tab, and click Bind now.​
  1. In the pop-up dialog box, select the created VMP workspace, and click Confirm. Enable APIG instance monitoring and save the monitoring data in the specified VMP workspace.​
warning​
The APIG instance and the VKE cluster must be bound to the same VMP workspace.​
Step 4: Create a KEDA scaling object​
  1. After you install the KEDA add-on, you need to use a secret to configure the basic authentication information for the VMP workspace. The example is as follows:​
​
apiVersion: v1​
kind: Secret​
metadata:​
name: keda-prom-secret​
namespace: default​
data:​
username: "YWRtaW4=" # The VMP workspace username after Base64 encoding. In this example, "YWRtaW4=" indicates admin.​
password: "YWRtaW4xMjM0" # The VMP workspace password after Base64 encoding. In this example, "YWRtaW4xMjM0" indicates admin1234.​
​
  1. Create an authentication CustomResourceDefinition (CRD) for KEDA to enable KEDA to access the VMP workspace. The example is as follows:​
​
apiVersion: keda.sh/v1alpha1​
kind: TriggerAuthentication​
metadata:​
name: keda-prom-creds​
namespace: default​
spec:​
secretTargetRef:​
- parameter: username​
name: keda-prom-secret​
key: username​
- parameter: password​
name: keda-prom-secret​
key: password​
​
  1. Create a KEDA autoscaling object ScaledObject.​
  • query indicates the query metric. In this example, the sum(rate(istio_requests_total[1m])) metric is selected, which indicates the total number of APIG requests. request_host indicates the external access address that corresponds to the APIG Ingress associated with APIG. In this example, example.com is used.​
​
apiVersion: keda.sh/v1alpha1​
kind: ScaledObject​
metadata:​
name: apig-scaledobject​
namespace: default​
spec:​
scaleTargetRef:​
name: nginx​
pollingInterval: 15​
# The critical definition for scaling from 0 to 1 with KEDA.​
idleReplicaCount: 0​
# The observation window for scaling in replicas from 1 to 0.​
cooldownPeriod: 30​
# The observation window that is triggered after cooldownPeriod is created for the first time.​
initialCooldownPeriod: 0​
# The minimum number of replicas (HPA).​
minReplicaCount: 1​
# The maximum number of replicas (HPA).​
maxReplicaCount: 2​
triggers:​
- type: prometheus​
metadata:​
serverAddress: http://query.prometheus-cn-beijing.ivolces.com/workspaces/5c0***​
threshold: '1'​
query: sum(rate(istio_requests_total{request_host="example.com"}[1m])) by (request_host)​
authModes: "basic"​
authenticationRef:​
name: keda-prom-creds​
​
Result validation​
Checking whether the workloads decline to zero​
  1. Log in to VKE console.​
  1. In the left navigation bar, click Cluster.​
  1. In the cluster list, click the name of the target cluster.​
  1. In the left navigation bar of the cluster management page, click Monitoring center > Dashboard.​
  1. In the dashboard list on the left side, click Workload monitoring > Deployment monitoring. After you select a namespace and a workload, you can view the monitoring dashboard for the specified workload.​
In the monitoring dashboard, you can see that when the workload is created, two replicas are configured. After the KEDA autoscaling object is created, the scaling metric for the workload is 0. Therefore, the number of replicas is changed to the value of idleReplicaCount in the autoscaling object ScaledObject of KEDA, which indicates that the replicas automatically decline to 0.​
Checking whether the workloads are scaled out from zero​
Use the following command to access applications in the cluster by using APIG Ingress. http://14.***.***.*** indicates the external IP address of the APIG instance. To obtain the IP address, see Obtaining gateway addresses.​
​
curl -H "Host: example.com" http://14.***.***.***​
​
Expected result: The corresponding application cannot be accessed and the system displays a backend error because the number of replicas for the backend workloads is zero.​
​
no healthy upstream​
​
In addition, when you view the observability dashboard or corresponding metrics in the APIG console or VMP console, you can view request quantity metrics.​
When KEDA in the cluster detects an increase in external metrics, it will scale out backend workloads from zero. Scaling from zero to ready incurs a 15-20 second delay, during which requests are not served.​
After the scale-out is complete, use the following command to access the applications in the cluster as expected.​
​
curl -H "Host: example.com" http://14.***.***.***​
​
Expected result: Workloads can be accessed as expected through the APIG Ingress.​
​
<!DOCTYPE html>​
<html>​
<head>​
<title>Welcome to nginx!</title>​
<style>​
html { color-scheme: light dark; }​
body { width: 35em; margin: 0 auto;​
font-family: Tahoma, Verdana, Arial, sans-serif; }​
</style>​
</head>​
<body>​
<h1>Welcome to nginx!</h1>​
<p>If you see this page, the nginx web server is successfully installed and​
working. Further configuration is required.</p>​
​
<p>For online documentation and support please refer to​
<a href="http://nginx.org/">nginx.org</a>.<br/>​
Commercial support is available at​
<a href="http://nginx.com/">nginx.com</a>.</p>​
​
<p><em>Thank you for using nginx.</em></p>​
</body>​
</html>​
​
After external requests are handled, the workloads decline to zero again, effectively reducing cloud costs during the idle period.​
View the autoscaling monitoring dashboard for information about workload scale-out and scale-in.​
​
Last updated: 2026.07.24 14:51:38