Skip to main content

Monitoring MongoDB Multi-Node

To achieve monitoring metrics data collection from all nodes in a MongoDB replica set, an independent ops-agent component must be deployed for each MongoDB node. Each agent has a built-in mongodb_exporter that collects the metrics data of the corresponding node. The monitoring data is exposed through a Service and then uniformly collected by the Ops Gateway.

  1. Create Agent Deployment File

    Create a mongodb-agent.yaml file with the following content:

    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: ops-agent-mongodb-1
    namespace: hap-ops
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: ops-agent-mongodb-1
    template:
    metadata:
    labels:
    app: ops-agent-mongodb-1
    spec:
    nodeSelector:
    hap-ops: "true"
    containers:
    - name: ops-agent-mongodb-1
    image: registry.cn-hangzhou.aliyuncs.com/mdpublic/ops-agent:1.1.0
    env:
    - name: ENV_AGENT_LIST
    value: "mongodb"
    - name: ENV_MONGODB_URI
    value: "mongodb://root:changeme@192.168.0.11:27017" # Modify to the connection address of the first node
    - name: ENV_MONGODB_OPTIONS
    value: "?authSource=admin"
    resources:
    limits:
    cpu: "1"
    memory: "2Gi"
    requests:
    cpu: "0.05"
    memory: 128Mi

    ---

    apiVersion: v1
    kind: Service
    metadata:
    name: ops-agent-mongodb-1
    namespace: hap-ops
    spec:
    selector:
    app: ops-agent-mongodb-1
    ports:
    - name: mongodb
    port: 9216
    targetPort: 9216
    type: ClusterIP

    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: ops-agent-mongodb-2
    namespace: hap-ops
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: ops-agent-mongodb-2
    template:
    metadata:
    labels:
    app: ops-agent-mongodb-2
    spec:
    nodeSelector:
    hap-ops: "true"
    containers:
    - name: ops-agent-mongodb-2
    image: registry.cn-hangzhou.aliyuncs.com/mdpublic/ops-agent:1.1.0
    env:
    - name: ENV_AGENT_LIST
    value: "mongodb"
    - name: ENV_MONGODB_URI
    value: "mongodb://root:changeme@192.168.0.12:27017" # Modify to the connection address of the second node
    - name: ENV_MONGODB_OPTIONS
    value: "?authSource=admin"
    resources:
    limits:
    cpu: "1"
    memory: "2Gi"
    requests:
    cpu: "0.05"
    memory: 128Mi
    ---

    apiVersion: v1
    kind: Service
    metadata:
    name: ops-agent-mongodb-2
    namespace: hap-ops
    spec:
    selector:
    app: ops-agent-mongodb-2
    ports:
    - name: mongodb
    port: 9216
    targetPort: 9216
    type: ClusterIP



    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: ops-agent-mongodb-3
    namespace: hap-ops
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: ops-agent-mongodb-3
    template:
    metadata:
    labels:
    app: ops-agent-mongodb-3
    spec:
    nodeSelector:
    hap-ops: "true"
    containers:
    - name: ops-agent-mongodb-3
    image: registry.cn-hangzhou.aliyuncs.com/mdpublic/ops-agent:1.1.0
    env:
    - name: ENV_AGENT_LIST
    value: "mongodb"
    - name: ENV_MONGODB_URI
    value: "mongodb://root:changeme@192.168.0.13:27017" # Modify to the connection address of the third node
    - name: ENV_MONGODB_OPTIONS
    value: "?authSource=admin"
    resources:
    limits:
    cpu: "1"
    memory: "2Gi"
    requests:
    cpu: "0.05"
    memory: 128Mi
    ---

    apiVersion: v1
    kind: Service
    metadata:
    name: ops-agent-mongodb-3
    namespace: hap-ops
    spec:
    selector:
    app: ops-agent-mongodb-3
    ports:
    - name: mongodb
    port: 9216
    targetPort: 9216
    type: ClusterIP
    • The configuration file defines three Deployment services and three Service services, each Deployment uses the ops-agent component to monitor the target MongoDB node.

    • Make sure to modify the connection address information of the MongoDB node monitored by each Deployment service.

    • If more MongoDB nodes need to be monitored, continue to add more Deployment and Service services.

  2. Start the Agent

    kubectl apply -f mongodb-agent.yaml
  3. Configure Ops-Gateway Service to Collect MongoDB-Agent Metrics Data

    Modify the environment variable in ops.yaml

    ENV_PROMETHEUS_MONGODB: "mongodb_1/ops-agent-mongodb-1:9216,mongodb_2/ops-agent-mongodb-2:9216,mongodb_3/ops-agent-mongodb-3:9216"

    Configuration specification:

    • Use the format <identifier>/<service_name>:<port>.
    • Separate multiple instances with commas.
    • service_name must correspond to the Service names created in the mongodb-agent.yaml.
  4. Restart Ops Platform

    # Stop services
    kubectl delete -f ops.yaml

    # Confirm that all Pods in the Ops Platform have terminated
    kubectl get pod -n hap-ops

    # Start services
    kubectl apply -f ops.yaml