mongodb-prometheus-sample.YAMLファイルを使用して、 KubernetesクラスターにMongoDBリソースを配置し、ServiceMongoDB を使用して、メトリクス データを消費する方法を Prometheus に指示できます。
このサンプルでは、ユーザーが 1 人の単純なMongoDBリソースと、基本的なHTTP認証を使用し spec.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-chartshelm repo updatehelm install prometheus prometheus-community/kube-prometheus-stack \ --namespace <prometheus-system> \ --create-namespaceKubernetes Operator 用のMongoDBドライバーのインストール
次のコマンドを実行して Kubernetes Operator をインストールし、Kubernetes Operator とリソースを含むための名前空間を作成します。
helm install mongodb-kubernetes-operator mongodb/mongodb-kubernetes \ --namespace <mongodb> --create-namespace詳細については、 Kubernetes Operator 用のMongoDBコントロールのインストール を参照してください。
MongoDB リソースの作成
mongodb-prometheus-sample.YAMLファイルを使用して、 KubernetesクラスターにMongoDBリソースを配置し、ServiceMongoDB を使用して、メトリクス データを消費する方法を Prometheus に指示できます。
次のコマンドを使用してサンプルを直接適用できます。
注意
mongodb-prometheus-sample.YAMLファイルへのフルパスを指定します。 spec.credentialsとspec.cloudManager.configMapRef.nameを指定するようにします。
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 patchedWith 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
次のmongodb-prometheus-sample.yaml ファイルを作成して、 KubernetesクラスターにMongoDBリソースを配置し、ServiceMongoDB を使用してメトリクス データを消費する方法を Prometheus に示します。
このサンプル ファイルでは、ユーザーが 1 人の単純な MongoDB リソースと、基本的なspec.prometheus HTTP 認証を使用し TLS なしの 属性を指定しています。サンプルを使用すると、MongoDB が Prometheus に送信するメトリクスをテストできます。
詳しくは、「 Prometheus の設定 」を参照してください。
---apiVersion: mongodb.com/v1kind: MongoDBmetadata: name: my-replica-setspec: 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-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/v1kind: ServiceMonitormetadata:
# 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: mongodbspec: 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/v0.94.1/pkg/apis/monitoring/v1/tls_types.go#L36-L39 # 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: v1kind: Secretmetadata: name: metrics-endpoint-creds namespace: mongodbtype: OpaquestringData: password: 'Not-So-Secure!' username: prometheus-username
...例
次の例は、MongoDB リソースで Prometheus を使用するために必要なリソース定義を示しています。
Prometheus と MongoDB リソース
詳しくは、「 Prometheus の設定 」を参照してください。
---apiVersion: mongodb.com/v1kind: MongoDBmetadata: name: my-replica-setspec: members: 3 version: 8.0.0 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/v1kind: ServiceMonitormetadata: labels: release: prometheus name: mongodb-sm namespace: mongodbspec: 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: v1kind: Secretmetadata: name: metrics-endpoint-creds namespace: mongodbtype: OpaquestringData: password: 'Not-So-Secure!' username: prometheus-username
...