Installing MongoDB

MongoDB is configured as a replica set across multiple nodes with an arbiter, and includes user and role setup. Monitoring is enabled using MongoDB Exporter.

Install MongoDB on the designated VMs and configure replica set with required ports and users.

To install the MongoDB, follow the steps below:

  1. Configure MongoDB repository and install.
    vi /etc/yum.repos.d/mongodb-org-6.0.repo
    
     [mongodb-org-6.0]
     name=MongoDB Repository
     baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/6.0/x86_64/
     gpgcheck=1
     enabled=1
     gpgkey=https://pgp.mongodb.com/server-6.0.asc
     
    yum install -y mongodb-org
  2. Update systemd configuration for replica set.
    echo "exclude=mongodb-org,mongodb-org-database,mongodb-org-server,mongodb-mongosh,mongodb-org-mongos,mongodb-org-tools" >> /etc/yum.conf
    sed -i "s|OPTIONS=-f /etc/mongod.conf|OPTIONS=--replSet cdprsg -f /etc/mongod.conf" /usr/lib/systemd/system/mongod.service
    vi /etc/mongod.conf
  3. Configure data directories and SELinux permissions.
    chown -R mongod: /sdd
     chcon -R --reference=/var/lib/mongo /sdd
     semanage port -a -t mongod_port_t -p tcp 51090
     systemctl daemon-reload
     systemctl restart mongod
     systemctl status mongod
    
     nc -zv  puapso1bhxcdmgodbs1002.hxcd.aws.hclsw.internal 5190
     nc -zv puapso1ahxcdmgodbs1003.hxcd.aws.hclsw.internal 5190
     nc -zv puapso1bhxcdmgodbs1004.hxcd.aws.hclsw.internal 5190
     nc -zv  puapso1ahxcdmgodbs1005 5190
    
  4. Configure replica set.
    mongosh --host <primary-host> --port 51090
    
     use admin;
    
     conf = {
      _id: "cdprsg",
      members: [
        { _id: 0, host: "<host1>:51090", tags: { usage:
            "schedule" } },
        { _id: 1, host: "<host2>:51090", tags: { usage:
            "schedule" } },
        { _id: 2, host: "<host3>:51090", tags: { usage:
            "segloader" } },
        { _id: 3, host: "<host4>:51090", tags: { usage:
            "analytics" } }
      ]
     }
     rs.initiate(conf)
     rs.addArb("<arbiter-host>:51090") 
     conf = rs.conf()
     conf.members[3].hidden = true
     rs.status()
  5. Create required MongoDB users.
    use admin;
    
     db.createUser({ user: "rtsms", pwd: "<password>", roles: [{ role: "readWrite",
            db: "segmentation" }, { role: "dbAdmin", db: "segmentation" }] });
    
     db.createUser({ user: "segdbconnector", pwd: "<password>", roles: [{ role:
            "readWrite", db: "segmentation" }] });
    
     db.createUser({ user: "pmm", pwd: "<password>", roles: [
      { role: "backup", db: "admin" },
      { role: "clusterMonitor", db: "admin" },
      { role: "read", db: "local" },
      { role: "readWrite", db: "admin" },
      { role: "restore", db: "admin" }
     ]
            });
      openssl rand -base64 16

Node Exporter

Node Exporter installed on MongoDB virtual machines collect systems metrics such as CPU, memory, disk, and network usage. These metrics are then scraped by Prometheus for monitoring and visualization.

To install node exporter, follow the steps below:

  1. Download and extract Node Exporter.
    wget  https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
     tar xfz node_exporter-1.7.0.linux-amd64.tar.gz
  2. Move binary and create system user.
    sudo mv node_exporter-1.7.0.linux-amd64/node_exporter /usr/local/bin/
     sudo useradd -rs /bin/false node_exporter
  3. Create a systemd service unit.
    vi /etc/systemd/system/node_exporter.service
    Paste the following contents:
    
     [Unit]
     Description=Node Exporter
     After=network.target
    
     [Service]
     User=node_exporter
     Group=node_exporter
     Type=simple
     ExecStart=/usr/local/bin/node_exporter
    
     [Install]
     WantedBy=multi-user.target
  4. Start and enable service.
    restorecon -Rv /usr/local/bin
    systemctl daemon-reload
     systemctl start node_
     systemctl status node_exporter
     systemctl enable node_exporter
  5. To validate metrics endpoint and exporter status, run this command to confirm the service is up.
      curl -kv http://localhost:9100/metrics

MongoDB Prometheus Exporter

This section details how to install and configure Percona's MongoDB Exporter to expose MongoDB metrics to Prometheus for scraping and monitoring. To install Prometheus exporter, follow the steps below:

  1. Install the Exporter Package.
    wget https://github.com/percona/mongodb_exporter/releases/download/v0.40.0/mongodb_exporter-0.40.0.linux-64-bit.rpm
    
    rpm -Uhv mongodb_exporter-0.40.0.linux-64-bit.rpm
  2. Create a systemd Unit File.
    vi /etc/systemd/system/mongodb_exporter.service
    
    Paste the following contents:
    
     [Unit]
     Description=Prometheus MongoDB Exporter
     After=network.target
    
     [Service]
     Type=simple
     User=mongodb_exporter
     Group=mongodb_exporter
    
            Environment="OPTIONS=--mongodb.uri=mongodb://pmm:<password>@<mongo-host>:51090
            --compatible-mode"
     ExecStart=/usr/bin/mongodb_exporter $OPTIONS
     Restart=always
    
     [Install]
     WantedBy=multi-user.target
    Note: Replace <password> and <mongo-host> with actual credentials and hostname.
  3. Reload and Start the Service.
    systemctl daemon-reload
     systemctl start mongodb_exporter
     systemctl status mongodb_exporter
     systemctl enable mongodb_exporter
  4. Run this command to validate and confirm the service is up.
    curl -kv http://localhost:9216/metrics