애플리케이션 로깅 및 모니터링

이 페이지에서는 사용자 애플리케이션의 커스텀 로그와 측정항목이 Cloud Logging 및 Cloud Monitoring으로 전송되도록 VMware용 Anthos 클러스터(GKE On-Prem)의 사용자 클러스터를 구성하는 방법을 설명합니다.

1.12부터 Google Cloud Managed Service for Prometheus를 사용하여 워크로드를 미리보기 기능으로 모니터링할 수 있습니다. Prometheus 측정항목에 대한 Google Cloud의 완전 관리형 스토리지 및 쿼리 서비스입니다. 이 기능을 사용하려면 아래 단계에 따라 Managed Service for Prometheus 및 Cloud Logging을 사용 설정하세요.

사용자 애플리케이션에 Prometheus용 관리형 서비스 사용 설정(미리보기)

Managed Service for Prometheus의 구성은 stackdriver라는 Stackdriver 객체에 보관됩니다.

  1. 수정할 stackdriver 객체를 엽니다.

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

    USER_CLUSTER_KUBECONFIG를 사용자 클러스터 kubeconfig 파일 경로로 바꿉니다.

  2. spec에서 enableGMPForApplicationstrue로 설정합니다.

      apiVersion: addons.gke.io/v1alpha1
      kind: Stackdriver
      metadata:
        name: stackdriver
        namespace: kube-system
      spec:
        projectID: ...
        clusterName: ...
        clusterLocation: ...
        proxyConfigSecretName: ...
        enableGMPForApplications: true
        enableVPC: ...
        optimizedMetrics: true
    
  3. 수정된 파일을 닫습니다. 이렇게 하면 클러스터에서 Google 관리 Prometheus(GMP) 구성요소가 실행되기 시작합니다.

  4. 상태를 확인하려면 다음 명령어를 실행하세요.

    kubectl --kubeconfig=USER_CLUSTER_KUBECONFIG --namespace gmp-system get pods
    

    이 명령어 결과는 다음과 비슷합니다.

     NAME                                 READY   STATUS    RESTARTS        AGE
     collector-abcde                      2/2     Running   1 (5d18h ago)   5d18h
     collector-fghij                      2/2     Running   1 (5d18h ago)   5d18h
     collector-klmno                      2/2     Running   1 (5d18h ago)   5d18h
     gmp-operator-68d49656fc-abcde        1/1     Running   0               5d18h
     rule-evaluator-7c686485fc-fghij      2/2     Running   1 (5d18h ago)   5d18h
    

Managed Service for Prometheus는 규칙 평가 및 알림을 지원합니다. 규칙 평가를 설정하려면 규칙 평가를 참조하세요.

예시 애플리케이션 실행

이 섹션에서는 Prometheus 측정항목을 내보내는 애플리케이션을 만들고 Google 관리형 Prometheus를 사용하여 측정항목을 수집합니다. 자세한 내용은 Google Cloud Managed Service for Prometheus를 참조하세요.

예시 애플리케이션 배포

  1. 예시 애플리케이션의 일부로 만드는 리소스에 대해 gmp-test Kubernetes 네임스페이스를 만듭니다.

    kubectl --kubeconfig=USER_CLUSTER_KUBECONFIG create ns gmp-test
    
  2. 관리형 서비스는 metrics 포트로 Prometheus 측정항목을 내보내는 예시 애플리케이션의 매니페스트를 제공합니다. 이 애플리케이션에는 3개의 복제본이 사용됩니다.

    예시 애플리케이션을 배포하려면 다음 명령어를 실행합니다.

    kubectl --kubeconfig USER_CLUSTER_KUBECONFIG -n gmp-test apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.4.1/examples/example-app.yaml
    

PodMonitoring 리소스 구성

예시 애플리케이션이 내보내는 측정항목 데이터를 수집하려면 대상 스크래핑을 사용합니다. 관리형 서비스는 PodMonitoring 커스텀 리소스(CR)를 사용하여 대상 스크래핑 및 측정항목 수집을 구성합니다. 기존 prometheus 연산자 리소스를 PodMonitoring CR로 변환할 수 있습니다.

PodMonitoring CR은 CR이 배포된 네임스페이스에서만 대상을 스크래핑합니다. 여러 네임스페이스에서 대상을 스크래핑하려면 각 네임스페이스에 동일한 PodMonitoring CR을 배포합니다. 다음 명령어를 실행하여 PodMonitoring 리소스가 의도한 네임스페이스에 설치되었는지 확인할 수 있습니다.

 kubectl --kubeconfig USER_CLUSTER_KUBECONFIG get podmonitoring -A

모든 Managed Service for Prometheus CR에 대한 참조 문서는 prometheus-engine/doc/api 참조를 확인하세요.

다음 매니페스트는 gmp-test 네임스페이스에서 PodMonitoring 리소스 prom-example을 정의합니다. 리소스는 네임스페이스에서 값이 prom-exampleapp 라벨이 있는 모든 포드를 찾습니다. 일치하는 포드는 /metrics HTTP 경로에서 30초 간격으로 metrics라는 포트로 스크래핑됩니다.

apiVersion: monitoring.googleapis.com/v1
kind: PodMonitoring
metadata:
  name: prom-example
spec:
  selector:
    matchLabels:
      app: prom-example
  endpoints:
  - port: metrics
    interval: 30s

이 리소스를 적용하려면 다음 명령어를 실행합니다.

kubectl --kubeconfig USER_CLUSTER_KUBECONFIG -n gmp-test apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.4.1/examples/pod-monitoring.yaml

이제 Prometheus용 관리형 서비스가 일치하는 포드를 스크래핑합니다.

측정항목 데이터 쿼리

Prometheus 데이터 내보내기가 수행되는지 확인하는 가장 간단한 방법은 Google Cloud 콘솔에서 측정항목 탐색기의 PromQL 쿼리를 사용하는 것입니다.

PromQL 쿼리를 실행하려면 다음을 수행하세요.

  1. Google Cloud Console에서 Monitoring 페이지로 이동하거나 다음 버튼을 클릭합니다.

    Monitoring으로 이동

  2. 탐색창에서 측정항목 탐색기를 선택합니다.

  3. Prometheus Query Language(PromQL)를 사용하여 차트에 표시할 데이터를 지정합니다.

    1. 측정항목 선택 창의 툴바에서 코드 편집기를 선택합니다.

    2. 언어 전환 버튼에서 PromQL을 선택합니다. 언어 전환은 코드 편집기 창 하단에 있습니다.

    3. 쿼리 편집기에 쿼리를 입력합니다. 예를 들어 이전 1시간 동안 각 모드에서 CPU가 소비한 평균 시간(초)을 차트로 표시하려면 다음 쿼리를 사용합니다.

      avg(rate(kubernetes_io:anthos_container_cpu_usage_seconds_total
      {monitored_resource="k8s_node"}[1h]))
      

    PromQL 사용에 대한 자세한 내용은 Cloud Monitoring의 PromQL을 참조하세요.

다음은 anthos_container_cpu_usage_seconds_total 측정항목을 표시하는 차트를 보여주는 스크린샷입니다.

`anthos_container_cpu_usage_seconds_total` 측정항목에 대한 Prometheus용 관리형 서비스 차트입니다.

많은 데이터를 수집하는 경우 비용 절감을 위해 내보낸 측정항목을 필터링해야 할 수 있습니다.

사용자 애플리케이션에 Cloud Logging 사용 설정(미리보기)

Logging 구성은 stackdriver라는 Stackdriver 객체에 보관됩니다.

  1. 수정할 stackdriver 객체를 엽니다.

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

    USER_CLUSTER_KUBECONFIG를 사용자 클러스터 kubeconfig 파일 경로로 바꿉니다.

  2. spec에서 enableCloudLoggingForApplicationstrue로 설정합니다.

      apiVersion: addons.gke.io/v1alpha1
      kind: Stackdriver
      metadata:
        name: stackdriver
        namespace: kube-system
      spec:
        projectID: ...
        clusterName: ...
        clusterLocation: ...
        proxyConfigSecretName: ...
        enableCloudLoggingForApplications: true
        enableVPC: ...
        optimizedMetrics: true
    
  3. 수정된 파일을 닫습니다.

예시 애플리케이션 실행

이 섹션에서는 커스텀 로그를 작성하는 애플리케이션을 만듭니다.

  1. 다음 배치 매니페스트를 my-app.yaml이라는 파일에 저장합니다.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: "monitoring-example"
      namespace: "default"
      labels:
        app: "monitoring-example"
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: "monitoring-example"
      template:
        metadata:
          labels:
            app: "monitoring-example"
        spec:
          containers:
          - image: gcr.io/google-samples/prometheus-dummy-exporter:latest
            name: prometheus-example-exporter
            imagePullPolicy: Always
            command:
            - /bin/sh
            - -c
            - ./prometheus-dummy-exporter --metric-name=example_monitoring_up --metric-value=1 --port=9090
            resources:
              requests:
                cpu: 100m
    
  2. 배포를 만듭니다.

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

애플리케이션 로그 보기

Console

  1. Google Cloud 콘솔의 로그 탐색기로 이동합니다.

    로그 탐색기로 이동

  2. 리소스를 클릭합니다. ALL_RESOURCE_TYPES에서 Kubernetes Container를 선택합니다.

  3. CLUSTER_NAME에서 사용자 클러스터의 이름을 선택합니다.

  4. NAMESPACE_NAME에서 default를 선택합니다.

  5. 추가를 클릭한 후 쿼리 실행을 클릭합니다.

  6. 쿼리 결과에서 monitoring-example 배포의 로그 항목을 볼 수 있습니다. 예를 들면 다음과 같습니다.

    {
      "textPayload": "2020/11/14 01:24:24 Starting to listen on :9090\n",
      "insertId": "1oa4vhg3qfxidt",
      "resource": {
        "type": "k8s_container",
        "labels": {
          "pod_name": "monitoring-example-7685d96496-xqfsf",
          "cluster_name": ...,
          "namespace_name": "default",
          "project_id": ...,
          "location": "us-west1",
          "container_name": "prometheus-example-exporter"
        }
      },
      "timestamp": "2020-11-14T01:24:24.358600252Z",
      "labels": {
        "k8s-pod/pod-template-hash": "7685d96496",
        "k8s-pod/app": "monitoring-example"
      },
      "logName": "projects/.../logs/stdout",
      "receiveTimestamp": "2020-11-14T01:24:39.562864735Z"
    }
    

gcloud

  1. 다음 명령어를 실행합니다.

    gcloud logging read 'resource.labels.project_id="PROJECT_ID" AND \
        resource.type="k8s_container" AND resource.labels.namespace_name="default"'
    

    PROJECT_ID로깅 모니터링 프로젝트의 ID로 바꿉니다.

  2. 출력에서 monitoring-example 배포의 로그 항목을 볼 수 있습니다. 예를 들면 다음과 같습니다.

    insertId: 1oa4vhg3qfxidt
    labels:
      k8s-pod/app: monitoring-example
      k8s- pod/pod-template-hash: 7685d96496
    logName: projects/.../logs/stdout
    receiveTimestamp: '2020-11-14T01:24:39.562864735Z'
    resource:
      labels:
        cluster_name: ...
        container_name: prometheus-example-exporter
        location: us-west1
        namespace_name: default
        pod_name: monitoring-example-7685d96496-xqfsf
        project_id: ...
      type: k8s_container
    textPayload: |
      2020/11/14 01:24:24 Starting to listen on :9090
    timestamp: '2020-11-14T01:24:24.358600252Z'
    

사용자 애플리케이션의 Logging 및 Monitoring 사용 설정

이 섹션에서는 Managed Service for Prometheus를 사용하지 않는 경우 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에서 enableStackdriverForApplicationstrue로 설정합니다.

      apiVersion: addons.gke.io/v1alpha1
      kind: Stackdriver
      metadata:
        name: stackdriver
        namespace: kube-system
      spec:
        projectID: ...
        clusterName: ...
        clusterLocation: ...
        proxyConfigSecretName: ...
        enableStackdriverForApplications: true
        enableVPC: ...
        optimizedMetrics: true
    
  3. 수정된 파일을 닫습니다.

워크로드 주석 달기

애플리케이션에서 커스텀 측정항목 수집을 사용 설정하려면 prometheus.io/scrape: "true" 주석을 애플리케이션의 서비스 또는 포드 매니페스트에 추가하거나 포드에 전달되도록 동일한 주석을 배포 또는 DaemonSet 매니페스트의 spec.template 섹션에 추가합니다.

측정항목 가비지 컬렉션을 방지하려면 측정항목 스크랩 간격을 1분으로 설정하는 것이 좋습니다.

예시 애플리케이션 실행

이 섹션에서는 커스텀 로그를 작성하고 커스텀 측정항목을 노출하는 애플리케이션을 만듭니다.

  1. 다음 서비스 및 배포 매니페스트를 my-app.yaml 파일에 저장합니다. 서비스에는 prometheus.io/scrape: "true" 주석이 있습니다.

    kind: Service
    apiVersion: v1
    metadata:
      name: "monitoring-example"
      namespace: "default"
      annotations:
        prometheus.io/scrape: "true"
    spec:
      selector:
        app: "monitoring-example"
      ports:
        - name: http
          port: 9090
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: "monitoring-example"
      namespace: "default"
      labels:
        app: "monitoring-example"
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: "monitoring-example"
      template:
        metadata:
          labels:
            app: "monitoring-example"
        spec:
          containers:
          - image: gcr.io/google-samples/prometheus-dummy-exporter:latest
            name: prometheus-example-exporter
            imagePullPolicy: Always
            command:
            - /bin/sh
            - -c
            - ./prometheus-dummy-exporter --metric-name=example_monitoring_up --metric-value=1 --port=9090
            resources:
              requests:
                cpu: 100m
    
  2. 배포와 서비스를 만듭니다.

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

애플리케이션 로그 보기

Console

  1. Google Cloud 콘솔의 로그 탐색기로 이동합니다.

    로그 탐색기로 이동

  2. 리소스를 클릭합니다. ALL_RESOURCE_TYPES에서 Kubernetes Container를 선택합니다.

  3. CLUSTER_NAME에서 사용자 클러스터의 이름을 선택합니다.

  4. NAMESPACE_NAME에서 default를 선택합니다.

  5. 추가를 클릭한 후 쿼리 실행을 클릭합니다.

  6. 쿼리 결과에서 monitoring-example 배포의 로그 항목을 볼 수 있습니다. 예를 들면 다음과 같습니다.

    {
      "textPayload": "2020/11/14 01:24:24 Starting to listen on :9090\n",
      "insertId": "1oa4vhg3qfxidt",
      "resource": {
        "type": "k8s_container",
        "labels": {
          "pod_name": "monitoring-example-7685d96496-xqfsf",
          "cluster_name": ...,
          "namespace_name": "default",
          "project_id": ...,
          "location": "us-west1",
          "container_name": "prometheus-example-exporter"
        }
      },
      "timestamp": "2020-11-14T01:24:24.358600252Z",
      "labels": {
        "k8s-pod/pod-template-hash": "7685d96496",
        "k8s-pod/app": "monitoring-example"
      },
      "logName": "projects/.../logs/stdout",
      "receiveTimestamp": "2020-11-14T01:24:39.562864735Z"
    }
    

gcloud

  1. 다음 명령어를 실행합니다.

    gcloud logging read 'resource.labels.project_id="PROJECT_ID" AND \
        resource.type="k8s_container" AND resource.labels.namespace_name="default"'
    

    PROJECT_ID로깅 모니터링 프로젝트의 ID로 바꿉니다.

  2. 출력에서 monitoring-example 배포의 로그 항목을 볼 수 있습니다. 예를 들면 다음과 같습니다.

    insertId: 1oa4vhg3qfxidt
    labels:
      k8s-pod/app: monitoring-example
      k8s- pod/pod-template-hash: 7685d96496
    logName: projects/.../logs/stdout
    receiveTimestamp: '2020-11-14T01:24:39.562864735Z'
    resource:
      labels:
        cluster_name: ...
        container_name: prometheus-example-exporter
        location: us-west1
        namespace_name: default
        pod_name: monitoring-example-7685d96496-xqfsf
        project_id: ...
      type: k8s_container
    textPayload: |
      2020/11/14 01:24:24 Starting to listen on :9090
    timestamp: '2020-11-14T01:24:24.358600252Z'
    

Google Cloud 콘솔에서 애플리케이션 측정항목 보기

예시 애플리케이션은 example_monitoring_up이라는 커스텀 측정항목을 노출합니다. Google Cloud 콘솔에서 해당 측정항목의 값을 볼 수 있습니다.

  1. Google Cloud 콘솔의 측정항목 탐색기로 이동합니다.

    측정항목 탐색기로 이동

  2. 리소스 유형에서 Kubernetes Pod 또는 Kubernetes Container를 선택합니다.

  3. 측정항목에서 external.googleapis.com/prometheus/example_monitoring_up을 선택합니다.

  4. 차트에서 example_monitoring_up의 반복되는 값이 1인 것을 알 수 있습니다.