visit
Manual server configuration is always a nightmare, especially when there is a large fleet of servers to manage. However, with Infrastructure as Code(IaC) automation tools like Ansible, all that pain goes away. In this blog, we shall leverage the automation tool to create a MongoDB sharded cluster from scratch in just a few minutes using this Ansible role.
As I mentioned earlier, we shall be using an existing Ansible role, so we won't have to write all the playbooks by ourselves from scratch. However, to install the role, we shall need a file that has its installation information. A requirements.yml file. Run the commands below to create the file and the directory, ansible_mongodb, which we shall be working.
mkdir ~/ansible_mongodb
echo "- name: 123mwanjemike.ansible_mongodb
src: //github.com/123MwanjeMike/ansible-mongodb
version: v1.0.4" > ~/ansible_mongodb/requirements.yml
Note on version v1.0.4
The latest version of the role, 123mwanjemike.ansible_mongodb, at the time of writing this blog, was v1.0.4. There may, however, be more recent at the time you read this, which are recommended to use instead.
ansible-galaxy install -r ~/ansible_mongodb/requirements.yml
Output:
mkdir ~/ansible_mongodb/inventory
touch ~/ansible_mongodb/inventory/hosts
Next, populate the inventory with the target servers grouped according to the respective replicaSets. That is to say, the Config servers replicaSet servers in their group, and the Shard servers in the corresponding group.
all:
children:
mongo_cluster:
children:
mongos_routers:
hosts:
mongos-router-0.europe-north1-b.c.oceanic-muse-408212.internal:
config_servers:
hosts:
mongod-cfgsvr-0.europe-north1-b.c.oceanic-muse-408212.internal:
mongod-cfgsvr-1.europe-north1-c.c.oceanic-muse-408212.internal:
mongod-cfgsvr-2.europe-north1-a.c.oceanic-muse-408212.internal:
shard_0:
hosts:
mongod-shard-0-0.europe-north1-b.c.oceanic-muse-408212.internal:
mongod-shard-0-1.europe-north1-c.c.oceanic-muse-408212.internal:
mongod-shard-0-2.europe-north1-a.c.oceanic-muse-408212.internal:
Host VariablesRun the commands below to put them in place
mkdir ~/ansible_mongodb/inventory/group_vars
echo "ansible_python_interpreter: /usr/bin/python3" > ~/ansible_mongodb/inventory/group_vars/all.yml
echo "cluster_role: 'router'" > ~/ansible_mongodb/inventory/group_vars/mongos_routers.yml
echo "cluster_role: 'config'
replica_set:
name: 'cfgsvr'
group: 'config_servers'" > ~/ansible_mongodb/inventory/group_vars/config_servers.yml
echo "cluster_role: 'shard'
replica_set:
name: 'shard-0'
group: 'shard_0'" > ~/ansible_mongodb/inventory/group_vars/shard_0.yml
Configuration Variables
You can find in the var/main.yml
file at the path where the role was installed. For example, in my case, it was installed at ~/.ansible/roles/123mwanjemike.ansible_mongodb
.
You can change them as you wish, especially the mongo_configs
versions and public keys given that they point to Ubuntu Jammy's MongoDB version 7.0 public keys.
Cluster Variables
We're almost good to go. We now just need a playbook to install and configure the MongoDB sharded cluster. I build from the provided in the README of the role on GitHub. Below is my ~/ansible_mongodb/sample_playbook.yml
file
- hosts: mongo_cluster
become: true
vars:
adminUser: "myAdminUser"
adminPass: "" # Secret password here
new_shard:
name: "shard-0"
group: "shard_0"
tgt_db: "myDatabase"
roles: ["readWrite", "userAdmin"]
userName: "targetUser"
userPass: "" # Secret password here
keyfile_src: "./keyfile"
cfg_server:
name: "cfgsvr"
group: "config_servers"
pre_tasks:
- name: Generate random string with OpenSSL on ansible controller
shell: openssl rand -base64 756 > keyfile
delegate_to: localhost
args:
creates: keyfile
roles:
- { role: 123mwanjemike.ansible_mongodb, flags: ["install_mongo"] }
- { role: 123mwanjemike.ansible_mongodb, flags: ["configure_mongo"] }
- { role: 123mwanjemike.ansible_mongodb, flags: ["clear_logs"] }
- { role: 123mwanjemike.ansible_mongodb, flags: ["prepare_members"] }
- { role: 123mwanjemike.ansible_mongodb, flags: ["start_mongo"] }
- {
role: 123mwanjemike.ansible_mongodb,
flags: ["init_replica"],
when: cluster_role != 'router',
}
- {
role: 123mwanjemike.ansible_mongodb,
flags: ["create_admin"],
when: cluster_role != 'router',
delegate_to: "{{ groups[replica_set.group] | first }}",
}
- {
role: 123mwanjemike.ansible_mongodb,
flags: ["add_shard"],
when: cluster_role == 'router',
run_once: true,
}
- {
role: 123mwanjemike.ansible_mongodb,
flags: ["create_database"],
when: cluster_role == 'router',
run_once: true,
}
cd ~/ansible_mongodb/
ansible-playbook sample_playbook.yml -i inventory/hosts
Output:
Checking out the cluster 🎉💃🏽🕺🏽😎
Cheers!
Also published .