How to scale replicas and shards

MongoDB shards are a configuration of an existing MongoDB charm, but run as applications alongside the main MongoDB application.

This guide goes over how to scale replicas of any application by adding and removing Juju units, and how to scale a sharded cluster.

Scale a replica set

To scale a replica set, use juju’s add-unit and remove-unit commands.

To add more replicas, run:

juju add-unit <application_name> -n <num_of_replicas_to_add>

where an application can be either a bare replica set, shard, or config-server.

To remove replicas, run:

juju remove-unit <application_name>/<unit_number> <application_name>/<unit_number>
juju remove-unit <application_name> --num-units <number_of_units_to_remove>

where an application can be either a bare replica set, shard, or config-server.

Note

juju remove-unit allows removing more than one replica so long as they do not constitute the majority of the replicas.

Scale a sharded cluster

To add a shard to a cluster, deploy the new shard as an application and add it to your config-server.

For example, to deploy a new shard named new-shard, run:

juju deploy mongodb --config role="shard" new-shard -n <number_of_replicas>
juju deploy mongodb-k8s --config role="shard" new-shard -n <number_of_replicas> --trust

Wait for the shard to show a blocked and idle status.

Next, add it to your config-server:

juju integrate <config-server-name>:config-server new-shard:sharding

To remove new-shard, remove the relation:

juju remove-relation <config-server-name>:config-server new-shard:sharding

Once the shard is drained, it can be fully removed with remove-application:

juju remove-application new-shard

Caution

As with upstream MongoDB, Charmed MongoDB does not support removing the last shard.

Retrieve primary replica

To get the primary replica, use the Juju action get-primary:

juju run <application_name>/<unit_number> get-primary

Where an application can be either a bare replica set, shard, or config-server.