对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

部署用于 Prometheus 的资源

可以使用 mongodb-prometheus-sample.yaml 文件在 Kubernetes 集群中部署 MongoDB 资源,并使用 ServiceMonitor 指示 Prometheus 如何使用其中的指标数据。

该样本指定具有一个用户的简单 MongoDB 资源,以及具有基本 HTTP 身份验证且无 TLSspec.prometheus 属性。该样本可让您测试 MongoDB 发送到 Prometheus 的指标。

注意

您不能将 Prometheus 与多 Kubernetes 集群部署一起使用。

我们使用 0.54 版本的 Prometheus Operator测试了此设置。

  • Kubernetes 1.16 +

  • Helm 3+

可以使用 Helm 安装 Prometheus Operator。要了解更多信息,请参阅安装说明

要使用 Helm 安装 Prometheus Operator,请运行以下命令:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus prometheus-community/kube-prometheus-stack \
--namespace <prometheus-system> \
--create-namespace

运行以下命令以安装 Kubernetes Operator,并创建一个命名空间以包含 Kubernetes Operator 和资源:

helm install mongodb-kubernetes-operator mongodb/mongodb-kubernetes \
--namespace <mongodb> --create-namespace

要学习;了解更多信息,请参阅为Kubernetes Operator 安装MongoDB控制器。

可以使用 mongodb-prometheus-sample.yaml 文件在 Kubernetes 集群中部署 MongoDB 资源,并使用 ServiceMonitor 指示 Prometheus 如何使用其中的指标数据。

您可以使用以下命令直接应用该样本:

注意

Specify the full path to the mongodb-prometheus-sample.yaml file. Ensure you specify spec.credentials and spec.cloudManager.configMapRef.name.

kubectl apply -f <mongodb-prometheus-sample.yaml>

This command creates two secrets that contain authentication for a new MongoDB user and basic HTTP authentication for the Prometheus endpoint. The command creates both secrets in the mongodb namespace.

This command also creates a ServiceMonitor that configures Prometheus to consume this resource's metrics. This command creates the ServiceMonitor in the prometheus-system namespace.

  1. 要使用 Helm 安装 cert-manager,请参阅cert-manager 安装文档

  2. 要创建 cert-manager Issuer,请参阅 cert-manager 配置文档

  3. 要创建证书,请参阅 cert-manager 使用文档

重要

不要在生产环境中使用该配置!安全专家应向您建议如何配置 TLS

To enable TLS, you must add a new entry to the spec.prometheus section of the MongoDB custom resource. Run the following patch operation to add the needed entry.

注意

tlsSecretKeyRef.name points at a secret of type kubernetes.io/tls that holds a Server certificate.

kubectl patch mdbc mongodb --type='json' \
-p='[{"op": "add", "path": "/spec/prometheus/tlsSecretKeyRef", "value":{"name": "prometheus-target-cert"}}]' \
--namespace mongodb

将显示以下响应:

mongodbenterprise.mongodbenterprise.mongodb.com/mongodb patched

在几分钟后,MongoDB 资源应恢复为“正在运行”阶段。现在,您必须配置 Prometheus ServiceMonitor 以指向 HTTPS 终结点。

要更新 ServiceMonitor,请运行以下命令以再次修补资源:

kubectl patch servicemonitors mongodb-sm --type='json' \
-p='
[
{"op": "replace", "path": "/spec/endpoints/0/scheme", "value": "https"},
{"op": "add", "path": "/spec/endpoints/0/tlsConfig", "value": {"insecureSkipVerify": true}}
]
' \
--namespace mongodb

将显示以下响应:

servicemonitor.monitoring.coreos.com/mongodb-sm patched

With these changes, the new ServiceMonitor points to the HTTPS endpoint (defined in /spec/endpoints/0/scheme). You also set spec/endpoints/0/tlsConfig/insecureSkipVerify to true, so that Prometheus doesn't verify the TLS certificates on MongoDB's end.

现在,Prometheus 应该能够使用 HTTPS 抓取 MongoDB 目标。

创建以下 mongodb-prometheus-sample.yaml 文件以在 Kubernetes 集群中部署 MongoDB 资源,并使用 ServiceMonitor 向 Prometheus 指示如何使用其中的指标数据。

这份样本文件指定具有一个用户的简单 MongoDB 资源以及具有基本 HTTP 身份验证且无 TLSspec.prometheus 属性。该样本可让您测试 MongoDB 发送到 Prometheus 的指标。

要了解更多信息,请参阅 Prometheus 设置

---
apiVersion: mongodb.com/v1
kind: MongoDB
metadata:
name: my-replica-set
spec:
members: 3
version: 8.0.0
cloudManager:
configMapRef:
name: <project-configmap>
credentials: <credentials-secret>
type: ReplicaSet
persistent: true
prometheus:
passwordSecretRef:
# SecretRef to a Secret with a 'password' entry on it.
name: metrics-endpoint-creds
# change this value to your Prometheus username
username: prometheus-username
# Enables HTTPS on the prometheus scrapping endpoint
# This should be a reference to a Secret type kuberentes.io/tls
# tlsSecretKeyRef:
# name: <prometheus-tls-cert-secret>
# Port for Prometheus, default is 9216
# port: 9216
#
# Metrics path for Prometheus, default is /metrics
# metricsPath: '/metrics'
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
# This needs to match `spec.ServiceMonitorSelector.matchLabels` from your
# `prometheuses.monitoring.coreos.com` resouce.
labels:
release: prometheus
name: mongodb-sm
# Make sure this namespace is the same as in `spec.namespaceSelector`.
namespace: mongodb
spec:
endpoints:
# Configuring a Prometheus Endpoint with basic Auth.
# `prom-secret` is a Secret containing a `username` and `password` entries.
- basicAuth:
password:
key: password
name: metrics-endpoint-creds
username:
key: username
name: metrics-endpoint-creds
# This port matches what we created in our MongoDB Service.
port: prometheus
# If using HTTPS enabled endpoint, change scheme to https
scheme: http
# Configure different TLS related settings. For more information, see:
# https://github.com/prometheus-operator/prometheus-operator/blob/main/pkg/apis/monitoring/v1/types.go#L909
# tlsConfig:
# insecureSkipVerify: true
# What namespace to watch
namespaceSelector:
matchNames:
# Change this to the namespace the MongoDB resource was deployed.
- mongodb
# Service labels to match
selector:
matchLabels:
app: my-replica-set-svc
---
apiVersion: v1
kind: Secret
metadata:
name: metrics-endpoint-creds
namespace: mongodb
type: Opaque
stringData:
password: 'Not-So-Secure!'
username: prometheus-username
...

以下示例显示使用 Prometheus 监控 MongoDB 资源所需的资源定义。

要了解更多信息,请参阅 Prometheus 设置

---
apiVersion: mongodb.com/v1
kind: MongoDB
metadata:
name: my-replica-set
spec:
members: 3
version: 8.0.0
cloudManager:
configMapRef:
name: <project-configmap>
credentials: <credentials-secret>
type: ReplicaSet
persistent: true
prometheus:
passwordSecretRef:
name: metrics-endpoint-creds
username: prometheus-username
...
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
release: prometheus
name: mongodb-sm
namespace: mongodb
spec:
endpoints:
- basicAuth:
password:
key: password
name: metrics-endpoint-creds
username:
key: username
name: metrics-endpoint-creds
port: prometheus
scheme: http
namespaceSelector:
matchNames:
- mongodb
selector:
matchLabels:
app: my-replica-set-svc
...
---
apiVersion: v1
kind: Secret
metadata:
name: metrics-endpoint-creds
namespace: mongodb
type: Opaque
stringData:
password: 'Not-So-Secure!'
username: prometheus-username
...