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

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

사용자 애플리케이션의 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.sigs.k8s.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" 주석을 애플리케이션의 서비스 또는 배포 매니페스트에 추가합니다.

예시 애플리케이션 실행

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

  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를 선택합니다.

  3. 측정항목으로 external/prometheus/example_monitoring_up을 선택합니다.

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