Nginx

Dokumen ini menjelaskan cara mengonfigurasi deployment Google Kubernetes Engine sehingga Anda dapat menggunakan Google Cloud Managed Service for Prometheus untuk mengumpulkan metrik dari Nginx. Dokumen ini menunjukkan cara melakukan hal berikut:

  • Menyiapkan pengekspor untuk Nginx guna melaporkan metrik.
  • Mengakses dasbor standar di Cloud Monitoring untuk melihat metrik.
  • Mengonfigurasi aturan pemberitahuan untuk memantau metrik.

Petunjuk ini hanya berlaku jika Anda menggunakan koleksi terkelola dengan Managed Service for Prometheus. Jika Anda menggunakan koleksi yang di-deploy sendiri, lihat repositori sumber untuk pengekspor Nginx guna mengetahui informasi penginstalan.

Petunjuk ini diberikan sebagai contoh dan diharapkan berfungsi di sebagian besar lingkungan Kubernetes. Jika Anda mengalami masalah saat menginstal aplikasi atau pengekspor karena kebijakan organisasi atau keamanan yang ketat, sebaiknya lihat dokumentasi open source untuk mendapatkan dukungan.

Untuk mengetahui informasi tentang Nginx, lihat Nginx.

Prasyarat

Untuk mengumpulkan metrik dari Nginx menggunakan Managed Service for Prometheus dan koleksi terkelola, deployment Anda harus memenuhi persyaratan berikut:

  • Cluster Anda harus menjalankan Google Kubernetes Engine versi 1.28.15-gke.2475000 atau yang lebih baru.
  • Anda harus menjalankan Managed Service for Prometheus dengan koleksi terkelola yang diaktifkan. Untuk mengetahui informasi selengkapnya, lihat Mulai menggunakan koleksi terkelola.

  • Untuk menggunakan dasbor yang tersedia di Cloud Monitoring untuk integrasi Nginx, Anda harus menggunakan nginx-prometheus-exporter versi v0.10.0 atau yang lebih baru.

    Untuk mengetahui informasi selengkapnya tentang dasbor yang tersedia, lihat Melihat dasbor.

Menginstal pengekspor Nginx

Sebaiknya instal pengekspor Nginx, nginx-prometheus-exporter, sebagai sidecar ke workload Nginx Anda. Untuk mengetahui informasi tentang penggunaan sidecar, lihat Aplikasi yang diperluas di Kubernetes dengan pod multi-container pod.

Untuk menginstal nginx-prometheus-exporter sebagai sidecar ke Nginx, ubah konfigurasi Nginx Anda seperti yang ditunjukkan dalam contoh berikut:

# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx
data:
  default.conf: |
    server {
        listen       80 default_server;
        server_name  _;
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
+       location /status {
+           stub_status on;
+           access_log off;
+           allow 127.0.0.1;
+           allow 10.0.0.0/8;
+           allow 172.16.0.0/12;
+           allow 192.168.0.0/16;
+           deny all;
+       }
    }
---
apiVersion: apps/v1
kind: Deployment
spec:
  ...
  template:
    metadata:
      labels:
+       app.kubernetes.io/name: nginx
    spec:
      containers:
+     - name: nginx-exporter
+       image: nginx/nginx-prometheus-exporter:0.10.0
+       args:
+       - "-nginx.scrape-uri=http://localhost/status"
+       ports:
+       - containerPort: 9113
+         name: prometheus
+       readinessProbe:
+         httpGet:
+           path: /metrics
+           port: prometheus
+       livenessProbe:
+         httpGet:
+           path: /metrics
+           port: prometheus
      - name: nginx
        image: nginx:1.14.2
+       ports:
+       - containerPort: 80
+         name: http
+       readinessProbe:
+         httpGet:
+           path: /status
+           port: http
+       livenessProbe:
+         httpGet:
+           path: /status
+           port: http
+       volumeMounts:
+       - mountPath: /etc/nginx/conf.d/default.conf
+         subPath: default.conf
+         name: config
+     volumes:
+     - name: config
+       configMap:
+         name: nginx
+         items:
+         - key: default.conf
+           path: default.conf

Anda harus menambahkan baris yang diawali dengan simbol + ke konfigurasi Anda.

Untuk menerapkan perubahan konfigurasi dari file lokal, jalankan perintah berikut:

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

Anda juga dapat menggunakan Terraform untuk mengelola konfigurasi.

Menentukan resource PodMonitoring

Untuk penemuan target, Operator Managed Service for Prometheus memerlukan resource PodMonitoring yang sesuai dengan pengekspor Nginx di namespace yang sama.

Anda dapat menggunakan konfigurasi PodMonitoring berikut:

# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: monitoring.googleapis.com/v1
kind: PodMonitoring
metadata:
  name: nginx
  labels:
    app.kubernetes.io/name: nginx
    app.kubernetes.io/part-of: google-cloud-managed-prometheus
spec:
  endpoints:
  - port: prometheus
    scheme: http
    interval: 30s
    path: /metrics
  selector:
    matchLabels:
      app.kubernetes.io/name: nginx

Pastikan pemilih label dan port cocok dengan pemilih dan port yang digunakan di Menginstal pengekspor Nginx.

Untuk menerapkan perubahan konfigurasi dari file lokal, jalankan perintah berikut:

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

Anda juga dapat menggunakan Terraform untuk mengelola konfigurasi.

Menentukan aturan dan pemberitahuan

Anda dapat menggunakan konfigurasi Rules berikut untuk menentukan pemberitahuan pada metrik Nginx:

# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: monitoring.googleapis.com/v1
kind: Rules
metadata:
  name: nginx-rules
  labels:
    app.kubernetes.io/component: rules
    app.kubernetes.io/name: nginx-rules
    app.kubernetes.io/part-of: google-cloud-managed-prometheus
spec:
  groups:
  - name: nginx
    interval: 30s
    rules:
    - alert: NginxDown
      annotations:
        description: |-
          Nginx instance is down
            VALUE = {{ $value }}
            LABELS: {{ $labels }}
        summary: Nginx down (instance {{ $labels.instance }})
      expr: nginx_up{job="nginx"} == 0
      for: 5m
      labels:
        severity: critical
    - alert: NginxDroppedConnections
      annotations:
        description: |-
         Too many dropped connections (> 5%)
            VALUE = {{ $value }}
            LABELS: {{ $labels }}
        summary: Number of dropped connections is high (instance {{ $labels.instance }})
      expr: (rate(nginx_connections_accepted{job="nginx"}[1m]) - rate(nginx_connections_handled{job="nginx"}[1m])) / rate(nginx_connections_accepted{job="nginx"}[1m]) > 0.05
      for: 5m
      labels:
        severity: warning
    - alert: NginxTrafficSpike
      annotations:
        description: |-
          Increase in total number of HTTP requests received (> 20%)
            VALUE = {{ $value }}
            LABELS: {{ $labels }}
        summary: Spike in HTTP traffic (instance {{ $labels.instance }})
      expr: rate(nginx_http_requests_total{job="nginx"}[1m])/ rate(nginx_http_requests_total{job="nginx"}[1m] offset 5m) > 1.2
      for: 5m
      labels:
        severity: warning
    - alert: NginxTrafficDrop
      annotations:
        description: |-
           Decrease in total number of HTTP requests received (> 20%)
            VALUE = {{ $value }}
            LABELS: {{ $labels }}
        summary: Drop in HTTP traffic (instance {{ $labels.instance }})
      expr: rate(nginx_http_requests_total{job="nginx"}[1m])/ rate(nginx_http_requests_total{job="nginx"}[1m] offset 5m) < 0.8
      for: 5m
      labels:
        severity: warning

Untuk menerapkan perubahan konfigurasi dari file lokal, jalankan perintah berikut:

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

Anda juga dapat menggunakan Terraform untuk mengelola konfigurasi.

Untuk mengetahui informasi selengkapnya tentang cara menerapkan aturan ke cluster, lihat Evaluasi dan pemberitahuan aturan terkelola.

Anda dapat menyesuaikan nilai minimum pemberitahuan agar sesuai dengan aplikasi Anda.

Memverifikasi konfigurasi

Anda dapat menggunakan Metrics Explorer untuk memverifikasi bahwa Anda telah mengonfigurasi pengekspor Nginx dengan benar. Cloud Monitoring mungkin memerlukan waktu satu atau dua menit untuk menyerap metrik Anda.

Untuk memverifikasi bahwa metrik telah diserap, lakukan hal berikut:

  1. Di Google Cloud konsol, buka halaman  Metrics explorer:

    Buka Metrics explorer

    Jika Anda menggunakan kotak penelusuran untuk menemukan halaman ini, pilih hasil yang subjudulnya adalah Monitoring.

  2. Di toolbar panel pembuat kueri, pilih tombol yang namanya  PromQL.
  3. Masukkan dan jalankan kueri berikut:
    up{job="nginx", cluster="CLUSTER_NAME", namespace="NAMESPACE_NAME"}
    

Melihat dasbor

Integrasi Cloud Monitoring mencakup dasbor Nginx Prometheus Overview. Dasbor akan otomatis diinstal saat Anda mengonfigurasi integrasi. Anda juga dapat melihat pratinjau statis dasbor tanpa menginstal integrasi.

Untuk melihat dasbor yang terinstal, lakukan hal berikut:

  1. Di Google Cloud konsol, buka halaman  Dashboards:

    Buka Dashboards

    Jika Anda menggunakan kotak penelusuran untuk menemukan halaman ini, pilih hasil yang subjudulnya adalah Monitoring.

  2. Pilih tab Dashboard List.
  3. Pilih kategori Integrations.
  4. Klik nama dasbor, misalnya, Nginx Prometheus Overview.

Untuk melihat pratinjau statis dasbor, lakukan hal berikut:

  1. Di Google Cloud konsol, buka halaman  Integrations:

    Buka Integrations

    Jika Anda menggunakan kotak penelusuran untuk menemukan halaman ini, pilih hasil yang subjudulnya adalah Monitoring.

  2. Klik filter platform deployment Kubernetes Engine.
  3. Temukan integrasi Nginx, lalu klik View Details.
  4. Pilih tab Dashboards.

Pemecahan masalah

Untuk mengetahui informasi tentang cara memecahkan masalah penyerapan metrik, lihat Masalah dengan pengumpulan dari pengekspor di Memecahkan masalah sisi penyerapan.