Mengumpulkan metrik dari beban kerja Anda

Halaman ini menguraikan proses untuk meng-scrape metrik dari workload di lingkungan air-gapped Google Distributed Cloud (GDC) untuk memfasilitasi pemantauan dan kemampuan observasi data.

Anda dapat meng-scrape dan mengumpulkan metrik yang dihasilkan komponen Anda dari waktu ke waktu. Platform pemantauan menawarkan API kustom untuk meng-scrape metrik dari beban kerja yang berjalan dalam namespace project Distributed Cloud Anda. Untuk meng-scrape metrik, Anda men-deploy resource kustom MonitoringTarget ke namespace project di server Management API. Setelah men-deploy resource ini, platform pemantauan akan memulai pengumpulan data.

Resource kustom MonitoringTarget mengarahkan pipeline pemantauan untuk meng-scrape pod yang ditetapkan dalam project Anda. Pod ini harus mengekspos endpoint HTTP yang memberikan metrik dalam format eksposisi Prometheus, seperti OpenMetrics. Metrik yang di-scrap kemudian ditampilkan di instance Grafana project Anda, sehingga memberikan insight tentang status operasional aplikasi Anda.

Untuk mengonfigurasi resource kustom MonitoringTarget, Anda harus menentukan pod dalam namespace project untuk pengumpulan metrik. Anda dapat menyesuaikan berbagai setelan, termasuk frekuensi scraping, titik akhir metrik pod, label, dan anotasi.

Sebelum memulai

Untuk mendapatkan izin yang Anda perlukan untuk mengelola MonitoringTargetresource kustom, minta Admin IAM Organisasi atau Admin IAM Project Anda untuk memberi Anda salah satu MonitoringTargetperan terkait.

Bergantung pada tingkat akses dan izin yang Anda perlukan, Anda mungkin mendapatkan peran pembuat, editor, atau pelihat untuk resource ini di organisasi atau project. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan izin IAM.

Mengonfigurasi resource kustom MonitoringTarget

Resource kustom MonitoringTarget memberi tahu platform pemantauan tempat pengumpulan metrik. Anda dapat menentukan pod yang metriknya dikumpulkan, endpoint metrik pod tersebut, frekuensi scrapping, dan setelan tambahan.

Resource ini menentukan konfigurasi berikut:

  • Target: Pod dan endpointnya dalam project Anda yang mengekspos metrik.
  • Interval pengambilan data: Seberapa sering Anda ingin menarik metrik dari endpoint yang dipilih.
  • Penyesuaian label: Aturan opsional dengan modifikasi label apa pun untuk metrik.

Pilih salah satu metode berikut untuk menentukan endpoint metrik di MonitoringTarget resource kustom:

  • Endpoint statis: Anda secara eksplisit mendeklarasikan endpoint (port, jalur, skema) dalam konfigurasi MonitoringTarget.
  • Anotasi: Informasi endpoint metrik pod diambil dari anotasi dalam file Deployment container. Metode ini menawarkan lebih banyak fleksibilitas jika setiap pod memiliki endpoint yang berbeda.

Endpoint statis

Ikuti langkah-langkah berikut untuk mengekspos metrik dari pod yang dipilih di endpoint yang ditentukan secara statis:

  1. Tentukan project Distributed Cloud tempat Anda ingin mengumpulkan metrik untuk pemantauan.

  2. Dalam spesifikasi pod, deklarasikan port yang menyajikan metrik di kolom containerPort. Contoh berikut menunjukkan cara mendeklarasikan port 2112 dalam spesifikasi pod:

    # ...
    spec:
      template:
        spec:
          containers:
          - name: your-container-name
            ports:
            - containerPort: 2112
    # ...
    
  3. Dalam konfigurasi MonitoringTarget, tentukan detail endpoint (port, jalur, skema) di bagian podMetricsEndpoints agar sesuai dengan port yang Anda ekspos dalam spesifikasi pod.

    File YAML berikut menunjukkan contoh konfigurasi MonitoringTarget di mana setiap pod yang dipilih harus mengekspos metrik di endpoint yang sama, http://your-container-name:2112/metrics:

    apiVersion: monitoring.gdc.goog/v1
    kind: MonitoringTarget
    metadata:
      # Choose the same namespace as the workload pods.
      namespace: your-project-namespace
      name: your-container-name
    spec:
      selector:
          # Choose pod labels to consider for this job.
          # Optional: Map of key-value pairs.
          # Default: No filtering by label.
          # To consider every pod in the project namespace, remove selector fields.
        matchLabels:
          app: your-app-label
      podMetricsEndpoints:
        port:
          value: 2112
        path:
          # Choose any value for your endpoint.
          # The /metrics value is an example.
          value: /metrics
        scheme:
          value: http
    
  4. Terapkan konfigurasi MonitoringTarget ke server Management API dalam namespace yang sama dengan pod target Anda:

    kubectl --kubeconfig KUBECONFIG_PATH apply -f MONITORING_TARGET_NAME.yaml
    

    Ganti kode berikut:

    • KUBECONFIG_PATH: jalur ke file kubeconfig untuk server Management API.
    • MONITORING_TARGET_NAME: nama file definisi MonitoringTarget.

Platform pemantauan mulai mengumpulkan metrik.

Anotasi

Ikuti langkah-langkah berikut untuk mengekspos metrik menggunakan anotasi jika setiap pod memiliki endpoint yang berbeda:

  1. Tentukan project Distributed Cloud tempat Anda ingin mengumpulkan metrik untuk pemantauan.

  2. Tambahkan anotasi berikut ke bagian annotations file Deployment penampung Anda:

    • prometheus.io/path
    • prometheus.io/port
    • prometheus.io/scheme

    Contoh berikut menunjukkan anotasi untuk metrik di port 2112:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: your-container-name
      namespace: your-project-namespace
      labels:
        app: your-app-label
      annotations:
        # These annotations are not required. They demonstrate selecting
        # pod metric endpoints through annotations.
        prometheus.io/path: /metrics
        prometheus.io/port: \"2112\"
        prometheus.io/scheme: http
    
  3. Dalam konfigurasi MonitoringTarget, tentukan anotasi yang Anda tambahkan ke file Deployment penampung di bagian podMetricsEndpoints. Spesifikasi ini memberi tahu resource kustom untuk mengumpulkan informasi endpoint metrik dari anotasi pada pod yang dipilih.

    File YAML berikut menunjukkan contoh konfigurasi MonitoringTarget menggunakan anotasi:

    apiVersion: monitoring.gdc.goog/v1
    kind: MonitoringTarget
    metadata:
    metadata:
      # Choose the same namespace as the workload pods.
      namespace: your-project-namespace
      name: your-container-name
    spec:
      selector:
        matchLabels:
          app: your-app-label
      podMetricsEndpoints:
        port:
          annotation: prometheus.io/port
        path:
          annotation: prometheus.io/path
        scheme:
          annotation: prometheus.io/scheme
    
  4. Terapkan konfigurasi MonitoringTarget ke server Management API dalam namespace yang sama dengan pod target Anda:

    kubectl --kubeconfig KUBECONFIG_PATH apply -f MONITORING_TARGET_NAME.yaml
    

    Ganti kode berikut:

    • KUBECONFIG_PATH: jalur ke file kubeconfig untuk server Management API.
    • MONITORING_TARGET_NAME: nama file definisi MonitoringTarget.

Platform pemantauan mulai mengumpulkan metrik.

Lihat spesifikasi MonitoringTarget lengkap dan dokumentasi referensi API untuk mengetahui kolom dan opsi tambahan.

Spesifikasi MonitoringTarget lengkap

File YAML berikut menunjukkan contoh untuk spesifikasi lengkap resource kustom MonitoringTarget. Untuk mengetahui informasi selengkapnya dan deskripsi lengkap kolom, lihat dokumentasi referensi API.

apiVersion: monitoring.gdc.goog/v1
kind: MonitoringTarget
metadata:
  # Choose the same namespace as the workload pods.
  namespace: PROJECT_NAMESPACE
  name: MONITORING_TARGET_NAME
spec:
  # Choose matching pattern that identifies pods for this job.
  # Optional
  # Relationship between different selectors: AND
  selector:
    # Choose clusters to consider for this job.
    # Optional: List
    # Default: All clusters applicable to this project.
    # Relationship between different list elements: OR
    matchClusters:
    - string

    # Choose pod labels to consider for this job.
    # Optional: Map of key-value pairs.
    # Default: No filtering by label.
    # Relationship between different pairs: AND
    matchLabels:
      key1: value1

    # Choose annotations to consider for this job.
    # Optional: Map of key-value pairs
    # Default: No filtering by annotation
    # Relationship between different pairs: AND
    matchAnnotations:
      key1: value1

  # Configure the endpoint exposed for this job.
  podMetricsEndpoints:
    # Choose a port either through static value or annotation.
    # Optional
    # Annotation takes priority.
    # Default: static port 80
    port:
      value: integer
      annotation: string

    # Choose a path either through static value or annotation.
    # Optional
    # Annotation takes priority
    # Default: static path /metrics
    path:
      value: string
      annotation: string

    # Choose a scheme either through a static value (http or https) or annotation.
    # Optional
    # Annotation takes priority
    # Default: static scheme http
    scheme:
      value: string
      annotation: string

    # Choose the frequency to scrape the metrics endpoint defined in podMetricsEndpoints
    # Optional
    # Default: 60s
    scrapeInterval: string

    # Dynamically rewrite the label set of a target before it gets scraped.
    # https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
    # Optional
    # Default: No filtering by label
    metricsRelabelings:
    - sourceLabels:
      - string
      separator: string
      regex: string
      action: string
      targetLabel: string
      replacement: string

Ganti kode berikut:

  • PROJECT_NAMESPACE: namespace project Anda.
  • MONITORING_TARGET_NAME: nama file definisi MonitoringTarget.