Docs Menu
Docs Home
/
MongoDB Enterprise Kubernetes 演算子
/ /

Prometheus で使用するリソースを配置する

項目一覧

  • クイック スタート
  • MongoDB リソースの作成
  • オプション: Prometheus エンドポイントで TLS を有効にする
  • mongodb-prometheus-sample.yaml

mongodb-prometheus-sample.YAML ファイルを使用して、 ServiceMonitor を使用して Kubernetes クラスターに MongoDB リソースを配置できます。 は、Prometheus がメトリクス データを消費する方法を Prometheus に示します。

このサンプルでは、ユーザー 1 人の単純な MongoDB リソースと、基本的なspec.prometheus HTTP 認証を使用し、 TLS なしの 属性を指定します。サンプルを使用すると、MongoDB が Prometheus に送信するメトリクスをテストできます。

注意

この設定を Prometheus0.54 演算子 のバージョン でテストしました。

  • Kubernetes 1.16+

  • Helm 3+

Helm を使用して Prometheus 演算子をインストール できます。 詳しくは、「 インストール手順 」を参照してください。

Helm を使用して Prometheus 演算子をインストールするには、次のコマンドを実行します。

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 enterprise-operator mongodb/enterprise-operator \
--namespace <mongodb> --create-namespace

詳しくは、「 MongoDB Enterprise Kubernetes Operator のインストール 」を参照してください。

mongodb-prometheus-sample. は MongoDBKubernetesは、Prometheus がメトリクス データを消費する方法を Prometheus に示します。

次のコマンドを使用してサンプルを直接適用できます。

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

このコマンドは 2 つの シークレット を作成します 新しい MongoDB ユーザーの認証と Prometheus エンドポイントの基本的な HTTP 認証を含みます。コマンドは両方の シークレット を作成しますmongodb ( 名前空間)。

このコマンドは、 ServiceMonitor も作成します は、このリソースのメトリクスを消費するように Prometheus を構成します。このコマンドは、 prometheus-system名前空間にServiceMonitorを作成します。

  1. 証明書マネージャー をインストールするには Helm の使用については、 cert-manager のインストール ドキュメント を参照してください 。

  2. 証明書マネージャー を作成するには、Issuer 証明書マネージャーの構成ドキュメント を参照してください。

  3. 証明書を作成するには、 cert Manager の使用ドキュメント を参照してください。 。

重要

本番環境では、この構成は使用しないでください。 セキュリティの専門家は、 TLSの構成方法についてアドバイスする必要があります。

TLSを有効にするには、MongoDB カスタム リソースのspec.prometheusセクションに新しいエントリを追加する必要があります。 次の パッチを 実行します 必要なエントリを追加するための 操作です。

注意

tlsSecretKeyRef.name 秘密 kubernetes.io/tlsを示す 点 サーバー証明書 を保持するタイプ の。

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 エンドポイントを指すようにします。

ServiceMongoDB を更新するには 、次のコマンドを実行してリソースを再度パッチします。

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

これらの変更により、新しい ServiceMongoDB は は HTTPS エンドポイント(/spec/endpoints/0/scheme で定義されている)を指します。また、 spec/endpoints/0/tlsConfig/insecureSkipVerifytrueに設定すると、Prometheus は MongoDB のエンドポイント上のTLS証明書を検証しなくなります。

Prometheus は HTTPS を使用して MongoDB ターゲットをスクレイピングできるようになりました。

次のmongodb-prometheus-sample.yaml ファイルを作成して、Kubernetes クラスターに ServiceMongoDB リソースを使用して MongoDB リソースを配置します。 は、Prometheus がメトリクス データを消費する方法を Prometheus に示します。

このサンプル ファイルでは、ユーザーが 1 人の単純な MongoDB リソースと、基本的なspec.prometheus HTTP 認証を使用し TLS なしの 属性を指定しています。サンプルを使用すると、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
...

次の例は、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
...
---
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
...

戻る

シャーディングされたクラスターの配置