Kubernetes 워크플로에서 로그 수집

Google Distributed Cloud (GDC) 에어 갭의 로그 수집을 사용하면 모니터링 및 문제 해결을 위해 Kubernetes 워크플로에서 운영 이벤트를 집계할 수 있습니다. 관리 API 서버에 LoggingTarget 커스텀 리소스를 배포하여 Kubernetes 워커 노드 가상 머신 (VM)에서 프로젝트 로그를 가져오고, 색인을 생성하고, 라벨을 지정합니다. 이 파이프라인은 독립형 사용자 VM의 로그 수집을 지원하지 않습니다.

로그 수집을 구성한 후에는 Grafana 또는 Log Query API를 통해 로그를 쿼리하고 볼 수 있습니다. 계측 권장사항은 Kubernetes 로깅 가이드라인을 참조하세요.

시작하기 전에

LoggingTarget 커스텀 리소스를 관리하는 데 필요한 권한을 얻으려면 IAM 조직 관리자 또는 IAM 프로젝트 관리자에게 연결된 LoggingTarget 역할 중 하나를 부여해 달라고 요청하세요.

필요한 액세스 및 권한 수준에 따라 조직 또는 프로젝트에서 이 리소스에 대한 생성자, 편집자 또는 뷰어 역할을 얻을 수 있습니다. 자세한 내용은 IAM 권한 준비를 참조하세요.

LoggingTarget 커스텀 리소스 구성

LoggingTarget 커스텀 리소스는 GDC 프로젝트 내에서 로그를 수집하도록 로깅 파이프라인에 지시합니다. 로그를 수집할 대상, 로그 파서, 액세스 수준, 추가 설정을 지정할 수 있습니다.

이 리소스는 다음 구성을 정의합니다.

  • 대상: 로그를 수집할 포드 및 컨테이너를 선택하는 기준입니다. 클러스터 이름, 포드 이름 프리픽스, 컨테이너 이름 프리픽스를 지정할 수 있습니다.
  • 로그 분석기: 로그 항목을 해석하고, 로그 출력을 라벨에 매핑하고, 관련 필드를 추출하는 사전 정의된 파서입니다.
  • 서비스 식별: 더 쉽게 식별하고 필터링할 수 있도록 로그 항목에 라벨로 적용되는 서비스 이름입니다.
  • 승인: 로그 항목의 액세스 수준으로, 수집된 로그를 볼 수 있는 사용자를 제어합니다.

LoggingTarget 커스텀 리소스 내에서 이러한 매개변수를 구성하면 로그 수집 및 구성을 정확하게 제어할 수 있습니다.

운영 로그를 수집하려면 다음 단계를 따르세요.

  1. 로그를 수집할 GDC 프로젝트를 결정합니다.
  2. LoggingTarget 구성에서 운영 로그를 수집할 포드, 프로젝트 네임스페이스, 추가 설정을 지정합니다.

    다음 YAML 파일은 my-project-namespace의 LoggingTarget 구성 예시를 보여줍니다. 여기서 로그를 수집할 포드 이름 프리픽스는 my-pod-prefix이고, 로그 항목의 액세스 수준은 애플리케이션 운영자 (ao)에게 부여되며, 서비스 이름은 my-service-name이고, 로그 형식은 JSON입니다.

    # Configures a log scraping job
    apiVersion: logging.gdc.goog/v1
    kind: LoggingTarget
    metadata:
      # Choose a namespace that matches the namespace of the workload pods.
      namespace: my-project-namespace
      name: my-logging-target
    spec:
      selector:
        matchPodNames:
          - my-pod-prefix
      parser: json
      logAccessLevel: ao
      serviceName: my-service-name
    

    추가 필드 및 옵션은 전체 LoggingTarget 사양 및 API 참고 문서를 참조하세요.

  3. 대상 포드와 동일한 네임스페이스 내의 Management API 서버에 LoggingTarget 구성을 적용합니다.

    kubectl --kubeconfig KUBECONFIG_PATH apply -f LOGGING_TARGET_NAME.yaml
    

    다음을 바꿉니다.

    • KUBECONFIG_PATH: Management API 서버의 kubeconfig 파일 경로
    • LOGGING_TARGET_NAME: LoggingTarget 정의 파일의 이름입니다(예: my-logging-target).

    로깅 파이프라인이 프로젝트의 추가 구성요소에서 로그를 수집하기 시작합니다.

Grafana 사용자 인터페이스 또는 Log Query API를 사용하여 수집된 로그를 쿼리할 수 있습니다. 자세한 내용은 로그 쿼리 및 보기를 참조하세요.

Grafana를 사용하여 로그를 쿼리하는 경우 다양한 로그 수준에 기본 제공되는 색상 코딩 기능을 사용할 수 있습니다. 로그 수준 값 설정에 대한 자세한 내용은 https://grafana.com/docs/grafana/latest/explore/logs-integration/을 참조하세요.

LoggingTarget 사양 완료

다음 YAML 파일은 LoggingTarget 커스텀 리소스의 전체 사양에 대한 예시를 보여줍니다. 자세한 내용과 필드에 대한 전체 설명은 API 참고 문서를 참조하세요.

# Configures a log scraping job
apiVersion: logging.gdc.goog/v1
kind: LoggingTarget
metadata:
  # Choose a namespace that matches the namespace of the workload pods.
  namespace: PROJECT_NAMESPACE
  name: LOGGING_TARGET_NAME
spec:
  # Choose a matching pattern that identifies the pods for this job.
  # Optional.
  # Relationship between different selectors: 'AND'
  selector:
    # The clusters to collect logs from.
    # The default configuration is to collect logs from all clusters.
    # The relationship between different clusters is an 'OR' relationship.
    # Optional
    matchClusters:
    - my-cluster
    - another-cluster

    # The pod name prefixes to collect logs from.
    # The logging platform scrapes all pods with names
    # that start with the specified prefixes.
    # The values must contain '[a-z0-9-]' characters only.
    # The relationship between different list elements is an 'OR' relationship.
    # Optional.
    matchPodNames:
      - my-pod-prefix
      - another-pod-prefix

    # The container name prefixes to collect logs from.
    # The logging platform scrapes all containers with names
    # that start with the specified prefixes.
    # The values must contain '[a-z0-9-]' characters only.
    # The relationship between different list elements is an 'OR' relationship.
    # Optional.
    matchContainerNames:
      - my-container-prefix
      - another-container-prefix

  # Choose the predefined parser for log entries.
  # Use parsers to map the log output to labels and extract fields.
  # Specify the log format.
  # Optional.
  # Options: klog_text, klog_json, klogr, gdch_json, json
  parser: klog_text

  # Specify an access level for log entries.
  # The default value is 'ao'.
  # Optional.
  # Options: ao, pa, io
  logAccessLevel: ao

  # Specify a service name to be applied as a label.
  # For user workloads consider this field as a workload name.
  # Required.
  serviceName: my-service-name

  # The additional static fields to apply to log entries.
  # The field is a key-value pair, where the field name is the key and
  # the field value is the value.
  # Optional.
  additionalFields:
    app: workload2
    key: value

다음을 바꿉니다.

  • PROJECT_NAMESPACE: 프로젝트 네임스페이스입니다.
  • LOGGING_TARGET_NAME: LoggingTarget 정의 파일의 이름입니다.