Last year I shared a series of posts walking through how to set up and monitor MongoDB with Aria Operations. For reference, here are those articles:
- Creating a Replica Set
- Aria Operations Management Pack
- Test data for performance monitoring
- Using LDAP Authentication
When I originally created those posts, Mongo DB had not yet added support for Ubuntu 24.04, so I used Ubuntu 20.04, as I had a template for that distribution. Recently I noticed these older Ubuntu 20.04 VMs, as Ubuntu 20.04 reached end of standard support earlier in the year. This post will review the updated setup steps to deploy a Mongo DB Replica Set on Ubuntu 24.04.
Installing Mongo DB 8.0 (latest) on Ubuntu 24.04
The Mongo DB documentation is very well written. I followed their steps from https://www.mongodb.com/docs/manual/tutorial/install-mongodb-enterprise-on-ubuntu/#std-label-install-mdb-enterprise-ubuntu. I’ll include a short code block below with the specific steps:
sudo apt-get install gnupg curl
curl -fsSL https://pgp.mongodb.com/server-8.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \
--dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.com/apt/ubuntu noble/mongodb-enterprise/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-enterprise-8.0.list
sudo apt-get update
sudo apt-get install mongodb-enterprise
sudo systemctl start mongod
sudo systemctl status mongod
sudo systemctl enable mongod
After running the above commands, my systems were all running a default MongoDB service and that service was set to run automatically at boot.
I confirmed that I could connect to the instance by running mongosh
at the console. This allowed me to connect automatically without specifying a password. While in the mongosh
console, I created a dbadmin
user account with the root
role.
var admin = db.getSiblingDB("admin")
admin.createUser(
{
user: "dbadmin",
pwd: "VMware1!",
roles:["root"]
})
After getting a successful response that my new user account was created, I exited the mongo shell by typing exit
.
Configuring MongoDB for Replica Set and LDAP authentication
Back at the command line, I created a directory to store a security.key
file to be used for each node in the replica set. I’ve included the details of these commands below:
cd /opt
sudo mkdir mongodb
sudo chown mongodb:mongodb /opt/mongodb
echo '88157a33a9dc499ea6b05c504daa36f8v2' | sudo tee /opt/mongodb/security.key
sudo chmod 400 /opt/mongodb/security.key
sudo chown mongodb:mongodb /opt/mongodb/security.key
With this file created & properly permissioned, we’ll update our mongo configuration file to specify the path to the
file. While we are in the file, we’ll add some additional settings for LDAP auth, as well as define the replica set name. We do this with security
.keyvi /etc/mongod.conf
and then make the following edits:
In the security
section, we add:
authorization: enabled
keyFile: /opt/mongodb/security.key
ldap:
servers: "core-control-21.lab.enterpriseadmins.org:389"
bind:
queryUser: "CN=svc-ldapbind,OU=LAB Service Accounts,DC=lab,DC=enterpriseadmins,DC=org"
queryPassword: "VMware1!"
transportSecurity: "none"
authz:
queryTemplate: "{USER}?memberOf?base"
validateLDAPServerConfig: true
setParameter:
authenticationMechanisms: "PLAIN,SCRAM-SHA-1,SCRAM-SHA-256"
In the replication
section we add:
replSetName: svcs-rs-11
After updating the /etc/mongod.conf
file on each host in my three node cluster, I restarted the service with the command sudo systemctl restart mongod
After the service restarted, I launched mongosh
again. Now that authentication has been enabled, I set my database and then login using the following commnds:
use admin
db.auth({ user: 'dbadmin', pwd: 'VMware1!', mechanism: 'SCRAM-SHA-256' })
Next I initiated the replica set using the following syntax:
rs.initiate( {
_id : "svcs-rs-11",
members: [
{ _id: 0, host: "svcs-mongo-11.lab.enterpriseadmins.org:27017" },
{ _id: 1, host: "svcs-mongo-12.lab.enterpriseadmins.org:27017" },
{ _id: 2, host: "svcs-mongo-13.lab.enterpriseadmins.org:27017" }
]
})
This took a few seconds, but then returned the message { ok: 1 }
. I double checked everything was running as expected by checking status rs.status()
which returned details of the replica set, showing member nodes and which were primary vs secondary.
Creating custom role for monitoring and administration
I then created a custom role to be used by monitoring tools, like Aria Operations.
var admin = db.getSiblingDB('admin')
admin.createRole(
{
role: "CN=LAB MongoDB Ent Monitoring,OU=LAB Service Accounts,DC=lab,DC=enterpriseadmins,DC=org",
roles: [ { role: "clusterMonitor", db: "admin" } ],
privileges: []
}
)
I also created a role to use for management. I could have done this with a single command by providing both roles when creating the role, but wanted to show an example of modifying an existing role as well.
var admin = db.getSiblingDB('admin')
admin.createRole(
{
role: "CN=LAB MongoDB Ent Admins,OU=LAB Service Accounts,DC=lab,DC=enterpriseadmins,DC=org",
roles: [ "dbAdminAnyDatabase", "clusterAdmin" ],
privileges: []
}
)
db.grantRolesToRole("CN=LAB MongoDB Ent Admins,OU=LAB Service Accounts,DC=lab,DC=enterpriseadmins,DC=org", [ { role: "root", db: "admin" } ] )
Loading sample data
Similar to the previous series of posts, I loaded some sample data into this replica set using the following syntax:
curl https://atlas-education.s3.amazonaws.com/sampledata.archive -o sampledata.archive
mongorestore --archive=sampledata.archive -u dbadmin -p 'VMware1!'
Using mongosh
as an LDAP user
Since we have LDAP authentication configured, we can also login to the mongo shell as an LDAP user. The following syntax is an example of how to do so:
mongosh --username "CN=svc-mgdbeadm,OU=LAB Service Accounts,DC=lab,DC=enterpriseadmins,DC=org" --password 'VMware1!' --authenticationDatabase='$external' --authenticationMechanism="PLAIN"
In this case we specify that we want to use an external authentication database (LDAP) and the mechanism as ‘PLAIN’, which we previously enabled as an option when configuring the replica set & LDAP authentication.
Managing databases with a graphical user interface
When demoing the Operations management pack, it is often helpful to interact / show the databases. Workflows such as creating, deleting, renaming a database can be helpful. Doing these demos is often more interesting from a GUI instead of a command line. I recently found mongo-express, a web-based, graphical interface to manage Mongo DB databases. I ran this as a container as a test using the following sytax:
sudo docker run -p 8081:8081 -e ME_CONFIG_MONGODB_URL='mongodb://dbadmin:VMware1!@svcs-mongo-11.lab.enterpriseadmins.org,svcs-mongo-12.lab.enterpriseadmins.org,svcs-mongo-13.lab.enterpriseadmins.org/admin?replicaSet=svcs-rs-11' mongo-express
This connects to the Mongo DB service using our local dbadmin. The console shows us that we can use http://0.0.0.0:8081
to connect to the web interface with the username admin
and password of pass
. From this web interface we can see / edit / delete our databases during our demos. I’ve since wrapped this up in a docker compose file and exposed it with a reverse proxy to apply an SSL certificate.
Conclusion
With Ubuntu 20.04 out of standard support, refreshing to 24.04 was a necessary step, even in a lab. Getting current by rebuilding this replica set configuration was rather straightforward. Monitoring is continuing to work with the same Aria Operations management pack previously used, with only creating a new data source and reusing the previous credential object.