Application Gateway のメトリックを使用した AKS ポッドの自動スケーリング (ベータ版)
着信トラフィックが増えると、需要に応じてアプリケーションをスケールアップすることが重要になります。
次のチュートリアルでは、Application Gateway の AvgRequestCountPerHealthyHost
メトリックを使用してアプリケーションをスケールアップする方法について説明します。 AvgRequestCountPerHealthyHost
は、特定のバックエンド プールに送信された平均要求と、バックエンドの HTTP 設定の組み合わせを測定します。
次の 2 つのコンポーネントを使用します。
Azure Kubernetes Metric Adapter
- メトリック サーバーを介して Application Gateway メトリックを公開するために、メトリック アダプターを使用します。 Azure Kubernetes Metric Adapter は、Application Gateway イングレス コントローラーと同じく、Azure のオープンソース プロジェクトです。Horizontal Pod Autoscaler
- Application Gateway メトリックを使用してスケーリング対象のデプロイを指定するために、HPA を使用します。
Note
Azure Kubernetes メトリック アダプターは保守されなくなりました。 Kubernetes Event-driven Autoscaling (KEDA) が代替手段となります。
Application Gateway for Containers の概要に関する記事も参照してください。
Azure Kubernetes Metric Adapter の設定
まず、Microsoft Entra サービス プリンシパルを作成し、Application Gateway のリソース グループに対する
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 Metric Adapter
をデプロイします。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
リソースを作成します。 このリソースは、myResourceGroup
リソース グループのmyApplicationGateway
リソースに対してAvgRequestCountPerHealthyHost
メトリックを公開するようにメトリック アダプターに指示します。filter
フィールドを使用すると、Application Gateway で特定のバックエンド プールやバックエンド HTTP 設定を対象にすることができます。apiVersion: azure.com/v1alpha2 kind: ExternalMetric metadata: name: appgw-request-count-metric spec: type: azuremonitor azure: resourceGroup: myResourceGroup # replace with your application gateway's resource group name resourceName: myApplicationGateway # replace with your application gateway'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
を公開できるようになったら、Horizontal Pod Autoscaler
を使用して対象のデプロイをスケールアップすることができます。
次の例では、サンプル デプロイ aspnet
を対象としています。 ポッドごとに appgw-request-count-metric
> 200 の場合、最大 10
ポッドまでスケールアップします。
対象のデプロイ名を置き換え、次の自動スケール構成を適用します。
apiVersion: autoscaling/v2beta1
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
apache ベンチのようなロード テスト ツールを使用してセットアップをテストします。
ab -n10000 http://<applicaiton-gateway-ip-address>/
次のステップ
- イングレス コントローラーの問題のトラブルシューティング: イングレス コントローラーに関する問題のトラブルシューティングを行います。