mongodb-prometheus-sample.YAMLファイルを使用して、 KubernetesクラスターにMongoDBリソースを配置し、ServiceMongoDB を使用して、メトリクス データを消費する方法を Prometheus に指示できます。
このサンプルでは、ユーザーが 1 人の単純な MongoDB リソースと、基本的な HTTPspec.prometheus 認証を使用し、 TLS がない 属性を指定します。サンプルを使用すると、MongoDB が Prometheus に送信するメトリクスをテストできます。
注意
Prometheus は、複数の Kubernetes クラスターの配置では使用できません。
クイック スタート
この設定を Prometheus 演算子のバージョン 0.54 でテストしました。
前提条件
- Kubernetes 1.16+ 
- Helm 3+ 
Prometheus 演算子をインストールする
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 
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ファイルを使用して、 KubernetesクラスターにMongoDBリソースを配置し、ServiceMongoDB を使用して、メトリクス データを消費する方法を Prometheus に指示できます。
次のコマンドを使用してサンプルを直接適用できます。
注意
kubectl apply -f <mongodb-prometheus-sample.yaml> 
このコマンドは、新しいMongoDBユーザーの認証と Prometheus エンドポイントの基本的なHTTP認証を含む 2 つのシークレットを作成します。このコマンドは、mongodb名前空間に両方のシークレットを作成します。
このコマンドは、このリソースのメトリクスを消費するように Prometheus を構成する ServiceMonitor も作成します。このコマンドは、prometheus-system名前空間に ServiceMonitor を作成します。
オプション: Prometheus エンドポイントで TLS を有効にする
Cert Manager のインストール
- Helm を使用して cert-manager をインストールするには、cert-manager のインストール ドキュメント を参照してください。 
- 証明書マネージャー - Issuerを作成するには、次のドキュメントを参照してください: 証明書マネージャーの構成ドキュメント
- 証明書を作成するには、cert-manager の使用ドキュメント を参照してください。 
MongoDB CRD での TLS の有効化
重要
本番環境では、この構成は使用しないでください。 セキュリティの専門家は、 TLSの構成方法についてアドバイスする必要があります。
TLS を有効にするには、 MongoDBカスタムリソースのspec.prometheus セクションに新しいエントリを追加する必要があります。次のパッチ操作を実行して、必要なエントリを追加します。
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 の更新
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 
これらの変更により、新しい 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 ファイルを作成して、 KubernetesクラスターにMongoDBリソースを配置し、ServiceMongoDB を使用してメトリクス データを消費する方法を 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 と MongoDB リソース
詳しくは、「 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 ... 
ServiceMongoDB
--- 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 ...