수평형 pod 자동 확장에 대한 사용자 정의 커스텀 측정항목 사용 설정

이 주제에서는 VMware용 Anthos 클러스터(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. 다음 서비스 및 배포 매니페스트를 my-app.yaml 파일에 저장합니다. 서비스에는 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. 배포와 서비스를 만듭니다.

    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에서 사용할 수 있는 {matchLabels: {app: example-monitoring}} 선택기로 example_monitoring_up 측정항목을 쿼리할 수 있습니다. 지정된 다른 선택기가 목록에 추가됩니다. 기본 선택기를 사용하지 않으려면 대상 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 및 20MB 메모리를 추가로 사용합니다.