对于目标发现,Managed Service for Prometheus Operator 需要与同一命名空间中的 Ingress NGINX 控制器对应的 PodMonitoring 资源。
您可以使用以下 PodMonitoring 配置:
# 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/v1kind:PodMonitoringmetadata:name:ingress-nginxlabels:app.kubernetes.io/name:ingress-nginxapp.kubernetes.io/part-of:google-cloud-managed-prometheusspec:endpoints:-port:10254scheme:httpinterval:30spath:/metricsselector:matchLabels:app.kubernetes.io/name:ingress-nginx
确保 port 和 matchLabels 字段的值与要监控的 Ingress NGINX Pod 的值匹配。默认情况下,Ingress NGINX 控制器会公开端口 10254 上的指标,且具有 app.kubernetes.io/name: ingress-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/v1kind:Rulesmetadata:name:ingress-nginx-ruleslabels:app.kubernetes.io/component:rulesapp.kubernetes.io/name:ingress-nginx-rulesapp.kubernetes.io/part-of:google-cloud-managed-prometheusspec:groups:-name:ingress-nginxinterval:30srules:-alert:NGINXIngressDroppedConnectionsannotations:description:|-NGINX Ingress dropped connectionsVALUE = {{ $value }}LABELS: {{ $labels }}summary:NGINX Ingress dropped connections (instance {{ $labels.instance }})expr:(nginx_ingress_controller_nginx_process_connections_total{state="accepted"} - nginx_ingress_controller_nginx_process_connections_total{state="handled"}) > 0for:5mlabels:severity:critical-alert:NGINXIngressHighRequestRateannotations:description:|-NGINX Ingress high request rateVALUE = {{ $value }}LABELS: {{ $labels }}summary:NGINX Ingress high request rate (instance {{ $labels.instance }})expr:rate(nginx_ingress_controller_requests[5m]) > 100for:5mlabels:severity:warning-alert:NGINXIngressLowRequestRateannotations:description:|-NGINX Ingress low request rateVALUE = {{ $value }}LABELS: {{ $labels }}summary:NGINX Ingress low request rate (instance {{ $labels.instance }})expr:rate(nginx_ingress_controller_requests[5m]) < 10for:5mlabels:severity:warning
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-30。"],[],[],null,["\u003cbr /\u003e\n\nThis document describes how to configure your Google Kubernetes Engine deployment\nso that you can use Google Cloud Managed Service for Prometheus to collect metrics from\n\nIngress NGINX Controller. This document shows you how to do the following:\n\n- Set up Ingress NGINX to report metrics.\n- Access a dashboard in Cloud Monitoring to view the metrics.\n- Configure alerting rules to monitor the metrics.\n\n\u003cbr /\u003e\n\nThese instructions apply only if you are using [managed collection](/stackdriver/docs/managed-prometheus/setup-managed)\nwith Managed Service for Prometheus.\nIf you are using self-deployed collection, then see the\n\n[Ingress NGINX documentation](https://kubernetes.github.io/ingress-nginx/user-guide/monitoring/)\n\nfor installation information.\n\nThese instructions are provided as an example and are expected to work in\nmost Kubernetes environments.\n\nIf you are having trouble installing an\napplication or exporter due to restrictive security or organizational policies,\nthen we recommend you consult open-source documentation for support.\n\nFor information about Ingress NGINX Controller, see [Ingress NGINX](https://kubernetes.github.io/ingress-nginx).\n\nPrerequisites\n\nTo collect metrics from\n\nthe Ingress NGINX Controller\n\nby using\nManaged Service for Prometheus and managed collection, your deployment must\nmeet the following requirements:\n\n- Your cluster must be running Google Kubernetes Engine version 1.28.15-gke.2475000 or later.\n- You must be running Managed Service for Prometheus with managed collection enabled. For more information, see [Get started with managed collection](/stackdriver/docs/managed-prometheus/setup-managed).\n\n \u003cbr /\u003e\n\n\u003cbr /\u003e\n\nIngress NGINX exposes Prometheus-format metrics automatically; you don't have to install it separately. To verify that the Ingress NGINX Controller is exposing metrics, run the following command: \n\n```\n kubectl exec -n NAMESPACE_NAME deploy/ingress-nginx-controller -- curl http://localhost:10254/metrics\n \n```\n\nDefine a PodMonitoring resource\n\nFor target discovery, the Managed Service for Prometheus Operator\nrequires a PodMonitoring resource that corresponds to\nthe Ingress NGINX Controller in the same namespace.\n\nYou can use the following PodMonitoring configuration: \n\n # Copyright 2022 Google LLC\n #\n # Licensed under the Apache License, Version 2.0 (the \"License\");\n # you may not use this file except in compliance with the License.\n # You may obtain a copy of the License at\n #\n # https://www.apache.org/licenses/LICENSE-2.0\n #\n # Unless required by applicable law or agreed to in writing, software\n # distributed under the License is distributed on an \"AS IS\" BASIS,\n # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n # See the License for the specific language governing permissions and\n # limitations under the License.\n\n apiVersion: monitoring.googleapis.com/v1\n kind: PodMonitoring\n metadata:\n name: ingress-nginx\n labels:\n app.kubernetes.io/name: ingress-nginx\n app.kubernetes.io/part-of: google-cloud-managed-prometheus\n spec:\n endpoints:\n - port: 10254\n scheme: http\n interval: 30s\n path: /metrics\n selector:\n matchLabels:\n app.kubernetes.io/name: ingress-nginx\n\nEnsure that the values of the `port` and `matchLabels` fields match those of the Ingress NGINX pods that you want to monitor. By default, the Ingress NGINX Controller exposes metrics on port 10254 and has the label `app.kubernetes.io/name: ingress-nginx`.\n\nTo apply configuration changes from a local file, run the following command:\n\n```\nkubectl apply -n NAMESPACE_NAME -f FILE_NAME\n```\n\n\u003cbr /\u003e\n\nYou can also\n[use Terraform](/stackdriver/docs/managed-prometheus/setup-managed#terraform-scrape)\nto manage your configurations.\n\nDefine rules and alerts\n\nYou can use the following `Rules` configuration to define\nalerts on your Ingress NGINX metrics: \n\n # Copyright 2022 Google LLC\n #\n # Licensed under the Apache License, Version 2.0 (the \"License\");\n # you may not use this file except in compliance with the License.\n # You may obtain a copy of the License at\n #\n # https://www.apache.org/licenses/LICENSE-2.0\n #\n # Unless required by applicable law or agreed to in writing, software\n # distributed under the License is distributed on an \"AS IS\" BASIS,\n # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n # See the License for the specific language governing permissions and\n # limitations under the License.\n\n apiVersion: monitoring.googleapis.com/v1\n kind: Rules\n metadata:\n name: ingress-nginx-rules\n labels:\n app.kubernetes.io/component: rules\n app.kubernetes.io/name: ingress-nginx-rules\n app.kubernetes.io/part-of: google-cloud-managed-prometheus\n spec:\n groups:\n - name: ingress-nginx\n interval: 30s\n rules:\n - alert: NGINXIngressDroppedConnections\n annotations:\n description: |-\n NGINX Ingress dropped connections\n VALUE = {{ $value }}\n LABELS: {{ $labels }}\n summary: NGINX Ingress dropped connections (instance {{ $labels.instance }})\n expr: (nginx_ingress_controller_nginx_process_connections_total{state=\"accepted\"} - nginx_ingress_controller_nginx_process_connections_total{state=\"handled\"}) \u003e 0\n for: 5m\n labels:\n severity: critical\n - alert: NGINXIngressHighRequestRate\n annotations:\n description: |-\n NGINX Ingress high request rate\n VALUE = {{ $value }}\n LABELS: {{ $labels }}\n summary: NGINX Ingress high request rate (instance {{ $labels.instance }})\n expr: rate(nginx_ingress_controller_requests[5m]) \u003e 100\n for: 5m\n labels:\n severity: warning\n - alert: NGINXIngressLowRequestRate\n annotations:\n description: |-\n NGINX Ingress low request rate\n VALUE = {{ $value }}\n LABELS: {{ $labels }}\n summary: NGINX Ingress low request rate (instance {{ $labels.instance }})\n expr: rate(nginx_ingress_controller_requests[5m]) \u003c 10\n for: 5m\n labels:\n severity: warning\n\nTo apply configuration changes from a local file, run the following command:\n\n```\nkubectl apply -n NAMESPACE_NAME -f FILE_NAME\n```\n\n\u003cbr /\u003e\n\nYou can also\n[use Terraform](/stackdriver/docs/managed-prometheus/setup-managed#terraform-scrape)\nto manage your configurations.\n\nFor more information about applying rules to your cluster, see\n[Managed rule evaluation and alerting](/stackdriver/docs/managed-prometheus/rules-managed).\nYou can adjust the alert thresholds to suit your application.\n\nVerify the configuration\n\nYou can use Metrics Explorer to verify that you correctly configured\nthe Ingress NGINX Controller. It might take one or two minutes for\nCloud Monitoring to ingest your metrics.\n\nTo verify the metrics are ingested, do the following:\n\n1. In the Google Cloud console, go to the\n *leaderboard* **Metrics explorer** page:\n\n [Go to **Metrics explorer**](https://console.cloud.google.com/monitoring/metrics-explorer)\n\n \u003cbr /\u003e\n\n If you use the search bar to find this page, then select the result whose subheading is\n **Monitoring**.\n2. In the toolbar of the query-builder pane, select the button whose name is either *code* **MQL** or *code* **PromQL**.\n3. Verify that **PromQL** is selected in the **Language** toggle. The language toggle is in the same toolbar that lets you format your query.\n4. Enter and run the following query: \n\n ```\n up{job=\"ingress-nginx\", cluster=\"CLUSTER_NAME\", namespace=\"NAMESPACE_NAME\"}\n ```\n\n\u003cbr /\u003e\n\nView dashboards\n\nThe Cloud Monitoring integration includes\n\nthe **Ingress NGINX Prometheus Overview** dashboard.\n\nDashboards are automatically installed when you configure the integration.\nYou can also view static previews of dashboards without installing the\nintegration.\n\n\n| **Note:** Some charts in the dashboard may appear unpopulated upon initial setup because Ingress NGINX only exports metrics like `nginx_ingress_controller_requests` after your Ingress resource starts receiving requests.\n\nTo view an installed dashboard, do the following:\n\n1. In the Google Cloud console, go to the **Dashboards** page:\n\n [Go to **Dashboards**](https://console.cloud.google.com/monitoring/dashboards)\n\n \u003cbr /\u003e\n\n If you use the search bar to find this page, then select the result whose subheading is\n **Monitoring**.\n2. Select the **Dashboard List** tab.\n3. Choose the **Integrations** category.\n4. Click the name of the dashboard, for example, **Ingress NGINX Prometheus Overview**.\n\n\u003cbr /\u003e\n\nTo view a static preview of the dashboard, do the following:\n\n1. In the Google Cloud console, go to the\n **Integrations**\n page:\n\n [Go to **Integrations**](https://console.cloud.google.com/monitoring/integrations)\n\n \u003cbr /\u003e\n\n If you use the search bar to find this page, then select the result whose subheading is\n **Monitoring**.\n2. Click the **Kubernetes Engine** deployment-platform filter.\n3. Locate the Ingress NGINX Controller integration and click **View Details**.\n4. Select the **Dashboards** tab.\n\n\u003cbr /\u003e\n\nTroubleshooting\n\nFor information about troubleshooting metric ingestion problems, see\n[Problems with collection from exporters](/stackdriver/docs/managed-prometheus/troubleshooting#exporter-problems) in [Troubleshooting ingestion-side problems](/stackdriver/docs/managed-prometheus/troubleshooting#ingest-problems)."]]