随着传入流量的增加,根据需求纵向扩展应用程序至关重要。
本文介绍如何使用 AvgRequestCountPerHealthyHost Azure 应用程序网关中的指标来扩展应用程序的 Azure Kubernetes 服务 (AKS) Pod。 该 AvgRequestCountPerHealthyHost 指标用于衡量发送到特定后端池与后端 HTTP 设置组合的平均请求数。
使用以下两个组件:
- Azure Kubernetes 指标适配器:使用此组件通过指标服务器公开应用程序网关指标。 它是 Azure 下的开源项目,类似于应用程序网关入口控制器。
- 水平 Pod 自动缩放程序:您可以使用此组件来应用 Application Gateway 的指标,并将缩放目标设定为某个部署。
注释
不再维护 Azure Kubernetes 指标适配器。 Kubernetes 事件驱动的自动缩放(KEDA)是一种替代方法。
设置 Azure Kubernetes 指标适配器
创建 Microsoft Entra 服务主体,并在应用程序网关部署的资源组中为其分配
Monitoring Reader的访问权限。applicationGatewayGroupName="<application-gateway-group-id>" applicationGatewayGroupId=$(az group show -g $applicationGatewayGroupName -o tsv --query "id") az ad sp create-for-rbac -n "azure-k8s-metric-adapter-sp" --role "Monitoring Reader" --scopes applicationGatewayGroupId使用之前创建的 Microsoft Entra 服务主体部署 Azure Kubernetes 指标适配器:
kubectl create namespace custom-metrics # use values from service principal created previously to create secret kubectl create secret generic azure-k8s-metrics-adapter -n custom-metrics \ --from-literal=azure-tenant-id=<tenantid> \ --from-literal=azure-client-id=<clientid> \ --from-literal=azure-client-secret=<secret> kubectl apply -f kubectl apply -f https://raw.githubusercontent.com/Azure/azure-k8s-metrics-adapter/master/deploy/adapter.yaml -n custom-metrics创建名称为
appgw-request-count-metric的ExternalMetric资源。 此资源指示指标适配器公开AvgRequestCountPerHealthyHost资源组中myApplicationGateway资源的指标myResourceGroup。 可以使用该filter字段在应用程序网关部署中定位特定的后端池和后端 HTTP 设置。apiVersion: azure.com/v1alpha2 kind: ExternalMetric metadata: name: appgw-request-count-metric spec: type: azuremonitor azure: resourceGroup: myResourceGroup # replace with your Application Gateway deployment's resource group name resourceName: myApplicationGateway # replace with your Application Gateway deployment's name resourceProviderNamespace: Microsoft.Network resourceType: applicationGateways metric: metricName: AvgRequestCountPerHealthyHost aggregation: Average filter: BackendSettingsPool eq '<backend-pool-name>~<backend-http-setting-name>' # optional
现在可以向指标服务器发出请求,以查看是否公开了新指标:
kubectl get --raw "/apis/external.metrics.k8s.io/v1beta1/namespaces/default/appgw-request-count-metric"
# Sample Output
# {
# "kind": "ExternalMetricValueList",
# "apiVersion": "external.metrics.k8s.io/v1beta1",
# "metadata":
# {
# "selfLink": "/apis/external.metrics.k8s.io/v1beta1/namespaces/default/appgw-request-count-metric",
# },
# "items":
# [
# {
# "metricName": "appgw-request-count-metric",
# "metricLabels": null,
# "timestamp": "2019-11-05T00:18:51Z",
# "value": "30",
# },
# ],
# }
使用新指标纵向扩展部署
通过指标服务器公开 appgw-request-count-metric 后,即可使用 水平 Pod 自动扩缩器 横向扩展目标部署。
以下示例针对名为 aspnet 的部署示例。 当每个 Pod 的 appgw-request-count-metric 为 200 时,你可以纵向扩展 Pod,最多可达到 10 个 Pod。
替换目标部署名称并应用以下自动缩放配置:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: deployment-scaler
spec:
scaleTargetRef:
apiVersion: networking.k8s.io/v1
kind: Deployment
name: aspnet # replace with your deployment's name
minReplicas: 1
maxReplicas: 10
metrics:
- type: External
external:
metricName: appgw-request-count-metric
targetAverageValue: 200
使用 ApacheBench 等负载测试工具测试设置:
ab -n10000 http://<application-gateway-ip-address>/