水平 Pod 自動スケーリング用にユーザー定義のカスタム指標を有効にする

このトピックでは、Anthos clusters on VMware(GKE On-Prem)で水平 Pod 自動スケーリング(HPA)のユーザー定義指標を構成する方法について説明します。

ユーザー アプリケーションの Logging と Monitoring の有効化

Logging と Monitoring の構成は、stackdriver という名前の Stackdriver オブジェクトに保持されます。

  1. stackdriver オブジェクトを編集用に開きます。

    kubectl --kubeconfig=USER_CLUSTER_KUBECONFIG --namespace kube-system edit stackdriver stackdriver
    

    USER_CLUSTER_KUBECONFIG は、ユーザー クラスタ kubeconfig ファイルのパスに置き換えます。

  2. spec の下にある enableStackdriverForApplicationsenableCustomMetricsAdapter をどちらも true に設定します。

    apiVersion: addons.sigs.k8s.io/v1alpha1
    kind: Stackdriver
    metadata:
    name: stackdriver
    namespace: kube-system
    spec:
    projectID: project-id
    clusterName: cluster-name
    clusterLocation: cluster-location
    proxyConfigSecretName: secret-name
    enableStackdriverForApplications: true
    enableCustomMetricsAdapter: true
    enableVPC: stackdriver-enable-VPC
    optimizedMetrics: true
    
  3. 編集したファイルを保存して閉じます。

以上の手順が完了すると、すべてのユーザー アプリケーション ログは Cloud Logging に送信されます。

次の手順では、指標の収集について、ユーザー アプリケーションにアノテーションを付けます。

指標を収集するユーザー アプリケーションにアノテーションを付ける

収集対象のユーザー アプリケーションと Cloud Monitoring に送信されるログにアノテーションを付けるには、対応する annotations をサービス、Pod、エンドポイントのメタデータに追加する必要があります。

  metadata:
    name: "example-monitoring"
    namespace: "default"
    annotations:
      prometheus.io/scrape: "true"
      prometheus.io/path: "" - Overriding metrics path (default "/metrics")
  

サンプルのユーザー アプリケーションをデプロイする

このセクションでは、ログと prometheus 互換の指標の両方を持つサンプル アプリケーションをデプロイします。

  1. 次の Service マニフェストと Deployment マニフェストを my-app.yaml という名前のファイルに保存します。Service に prometheus.io/scrape: "true" というアノテーションが付いていることに注意してください。
  kind: Service
  apiVersion: v1
  metadata:
    name: "example-monitoring"
    namespace: "default"
    annotations:
      prometheus.io/scrape: "true"
  spec:
    selector:
      app: "example-monitoring"
    ports:
      - name: http
        port: 9090
  ---
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: "example-monitoring"
    namespace: "default"
    labels:
      app: "example-monitoring"
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: "example-monitoring"
    template:
      metadata:
        labels:
          app: "example-monitoring"
      spec:
        containers:
        - image: gcr.io/google-samples/prometheus-example-exporter:latest
          name: prometheus-example-exporter
          imagePullPolicy: Always
          command:
          - /bin/sh
          - -c
          - ./prometheus-example-exporter --metric-name=example_monitoring_up --metric-value=1 --port=9090
          resources:
            requests:
              cpu: 100m
  1. Deployment と Service を作成します。

    kubectl --kubeconfig USER_CLUSTER_KUBECONFIG apply -f my-app.yaml
    

HPA でカスタム指標を使用する

HPA オブジェクトをデプロイして、前のステップで公開された指標を使用します。さまざまな種類のカスタム指標の詳細については、複数の指標とカスタム指標の自動スケーリングをご覧ください。

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: example-monitoring-hpa
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: example-monitoring
  minReplicas: 1
  maxReplicas: 5
  metrics:
  - type: Pods
    pods:
      metric:
        name: example_monitoring_up
      target:
        type: AverageValue
        averageValue: 20

Pod タイプの指標には、ターゲット Pod のラベルのデフォルトの指標セレクタがあり、これが kube-controller-maneger の機能です。この例では、ターゲット Pod で利用できる example_monitoring_up 指標を {matchLabels: {app: example-monitoring}} というセレクタでクエリします。指定したその他のセレクタは、リストに追加されます。デフォルト セレクタを使用しないようにするには、ターゲット Pod のラベルをすべて削除するか、オブジェクト タイプの指標を使用します。

ユーザー定義のアプリケーション指標を HPA が使用していることを確認する

ユーザー定義のアプリケーション指標を HPA が使用していることを確認します。

kubectl --kubeconfig=USER_CLUSTER_KUBECONFIG describe hpa example-monitoring-hpa

出力は次のようになります。

Name:                             example-monitoring-hpa
Namespace:                        default
Labels:                           
Annotations:                      CreationTimestamp:  Mon, 19 Jul 2021 16:00:40 -0800
Reference:                        Deployment/example-monitoring
Metrics:                          ( current / target )
  "example_monitoring_up" on pods:  1 / 20
Min replicas:                     1
Max replicas:                     5
Deployment pods:                  1 current / 1 desired
Conditions:
  Type            Status  Reason              Message


AbleToScale True ReadyForNewScale recommended size matches current size ScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from pods metric example_monitoring_up ScalingLimited False DesiredWithinRange the desired count is within the acceptable range

料金

HPA にカスタム指標を使用しても、追加料金は発生しません。ユーザーは、アプリケーションの指標とログに対してのみ課金されます。詳細については、Google Cloud のオペレーション スイートの料金をご覧ください。カスタム指標を有効にする Pod は、追加の 15m の CPU と 20 MB のメモリを消費します。