apiVersion: v1kind: Secretmetadata:name: mongodb-secret-testnamespace: mongodb-testtype: OpaquestringData:datasource: "mongodb://{user}:{passwd}@{host1}:{port1},{host2}:{port2},{host3}:{port3}/admin" # Corresponding connection URI
apiVersion: apps/v1kind: Deploymentmetadata:labels:k8s-app: mongodb-exporter # Rename the exporter based on the business needs. We recommend you add the MongoDB instance informationname: mongodb-exporter # Rename the exporter based on the business needs. We recommend you add the MongoDB instance informationnamespace: mongodb-testspec:replicas: 1selector:matchLabels:k8s-app: mongodb-exporter # Rename the exporter based on the business needs. We recommend you add the MongoDB instance informationtemplate:metadata:labels:k8s-app: mongodb-exporter # Rename the exporter based on the business needs. We recommend you add the MongoDB instance informationspec:containers:- args:- --collect.database # Enable the collection of `Database` metrics- --collect.collection # Enable the collection of `Collection` metrics- --collect.topmetrics # Enable the collection of `table top` metrics- --collect.indexusage # Enable the collection of `per index usage stats`- --collect.connpoolstats # Enable the collection of `MongoDB connpoolstats`env:- name: MONGODB_URIvalueFrom:secretKeyRef:name: mongodb-secret-testkey: datasourceimage: ssheehy/mongodb-exporterimagePullPolicy: IfNotPresentname: mongodb-exporterports:- containerPort: 9216name: metric-port # This name is required during scrape task configurationsecurityContext:privileged: falseterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilednsPolicy: ClusterFirstimagePullSecrets:- name: qcloudregistrykeyrestartPolicy: AlwaysschedulerName: default-schedulersecurityContext: { }terminationGracePeriodSeconds: 30

wget command with the address exposed by the exporter on the command line, and you can get the corresponding MongoDB metrics normally. If no corresponding data is returned, please check whether the connection URI is correct as shown below:wget 127.0.0.1:9216/metricscat metrics

Pod Monitor to define a Prometheus scrape task. Below is a sample YAML configuration:apiVersion: monitoring.coreos.com/v1kind: PodMonitormetadata:name: mongodb-exporter # Enter a unique namenamespace: cm-prometheus # The namespace is fixed. Do not change itspec:podMetricsEndpoints:- interval: 30sport: metric-port # Enter the name of the corresponding port of the Prometheus exporter in the Pod YAML configuration filepath: /metrics # Enter the value of the corresponding path of the Prometheus exporter. If it is not specified, it will be `/metrics` by defaultrelabelings:- action: replacesourceLabels:- instanceregex: (.*)targetLabel: instancereplacement: 'cmgo-xxxxxxxx' # Change it to the corresponding MongoDB instance IDnamespaceSelector: # Select the namespace where the Pod to be monitored residesmatchNames:- mongodb-testselector: # Enter the label value of the Pod to be monitored to locate the target PodmatchLabels:k8s-app: mongodb-exporter



Feedback