部署与 Prometheus 一起使用的资源
您可以使用mongodb-prometheus-sample.yaml 文件来部署您的 Kubernetes 中的 MongoDB 资源Kubernetes集群,通过一个ServiceMonitor来指示Prometheus如何从它那里消费指标数据。
该示例指定了一个简单的MongoDB资源,其中有一个用户,并且spec.prometheus
属性具有基本的HTTP身份验证和无TLS。该示例允许您测试MongoDB发送给Prometheus的指标。
注意
您无法在多Kubernetes集群部署中使用Prometheus。
快速入门
我们使用Prometheus Operator的版本0.54进行了此配置测试。Prometheus Operator。
先决条件
Kubernetes 1.16+
Helm 3+
安装Prometheus Operator
您可以使用 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
安装MongoDB Enterprise Kubernetes Operator
运行以下命令来安装Kubernetes Operator并创建一个命名空间来包含Kubernetes Operator和资源
helm install enterprise-operator mongodb/enterprise-operator \ --namespace <mongodb> --create-namespace
要了解更多信息,请参阅安装 MongoDB Enterprise Kubernetes Operator。
创建 MongoDB 资源
您可以使用mongodb-prometheus-sample.yaml文件来部署您的 MongoDB 资源。Kubernetes集群,通过一个ServiceMonitor来指示Prometheus如何从它那里消费度量数据。
您可以直接使用以下命令应用示例
注意
指定mongodb-prometheus-sample.yaml文件的完整路径。确保指定spec.credentials
和spec.cloudManager.configMapRef.name
。
kubectl apply -f <mongodb-prometheus-sample.yaml>
该命令创建两个机密,包含新的MongoDB用户的身份验证和Prometheus端点的基本HTTP身份验证。该命令在mongodb
命名空间中创建了这两个机密
。
该命令还创建了一个ServiceMonitor,用于配置Prometheus以消费该资源的指标。该命令在prometheus-system
命名空间中创建了ServiceMonitor
。
可选:在Prometheus端点上启用TLS
安装Cert-Manager
使用 Helm 安装 cert-manager,请参阅cert-manager 安装文档。
要创建一个 cert-manager 的
Issuer
,请参阅cert-manager 配置文档要创建证书,请参阅cert-manager 使用文档。
在 MongoDB CRD 上启用 TLS
重要
**请不要**在生产环境中使用此配置!应咨询安全专家以了解如何配置TLS。
要启用TLS,您必须在 MongoDB 自定义资源的 spec.prometheus
部分中添加一个新的条目。运行以下补丁操作以添加所需的条目。
注意
tlsSecretKeyRef.name
指向一个类型为 kubernetes.io/tls
的 secret,该 secret 存储了一个 服务器证书。
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
要更新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
通过这些更改,新的ServiceMonitor指向HTTPS端点(在/spec/endpoints/0/scheme
中定义)。您还设置了spec/endpoints/0/tlsConfig/insecureSkipVerify
为true
,这样Prometheus就不会在MongoDB端验证TLS证书。
现在Prometheus应该能够使用HTTPS抓取MongoDB目标。
mongodb-prometheus-sample.yaml
创建以下 mongodb-prometheus-sample.yaml
文件以在你的集群中部署 MongoDB 资源。Kubernetes集群,通过一个ServiceMonitor来指示Prometheus如何从它那里消费度量数据。
此示例文件指定了一个简单的 MongoDB 资源,包含一个用户,并具有带有基本 HTTP 验证和未启用 TLS 的 spec.prometheus
属性。该示例允许你测试 MongoDB 向 Prometheus 发送的指标。
了解更多信息,请参阅 Prometheus 设置。
--- apiVersion: mongodb.com/v1 kind: MongoDB metadata: name: my-replica-set spec: members: 3 version: 6.0.6-ent 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-password # 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 资源所需的资源定义。
MongoDB 资源与 Prometheus
了解更多信息,请参阅 Prometheus 设置。
--- apiVersion: mongodb.com/v1 kind: MongoDB metadata: name: my-replica-set spec: members: 3 version: 6.0.6-ent cloudManager: configMapRef: name: <project-configmap> credentials: <credentials-secret> type: ReplicaSet persistent: true prometheus: passwordSecretRef: name: metrics-endpoint-password username: prometheus-username ...
ServiceMonitor
--- 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 ...