Skip to main content

Your submission was sent successfully! Close

Thank you for signing up for our newsletter!
In these regular emails you will find the latest updates from Canonical and upcoming events where you can meet our team.Close

Thank you for contacting us. A member of our team will be in touch shortly. Close

Charmed MongoDB K8s Tutorials > Deploy a sharded cluster > 4. Add and remove shards

Add and remove shards

Shards spread data evenly throughout the cluster, making a database highly available. This part of the tutorial will teach you how to scale your cluster by adding and removing shards.

Disclaimer: This tutorial hosts all shards on the same machine. This should never be done in a production environment.

To enable high availability in a production environment, replicas should be hosted on different servers to maintain isolation.

Add shards

Sharded clusters cannot be scaled via juju the way replica sets can. So, before adding a shard to the cluster, we need to create it.

To create a new shard, deploy a Charmed MongoDB K8s application with the shard config option role:

juju deploy  mongodb-k8s --config role="shard" shard2

Use juju status --watch 1s --relations to wait until shard2 is blocked due to missing relation.

Now that our shard is deployed, it is ready to be added to the cluster. For the shard to be added to the cluster it must be integrated with the config-server:

juju integrate config-server:config-server shard2:sharding

To verify that the shard is present in the cluster, we can check the cluster configuration through the mongos router inside config-server and listing shards like we did in 4. Access a sharded cluster.

Remove shards

To remove a shard from Charmed MongoDB, we remove the integration from the shard and the config-server. This signals to the cluster that it is time to drain the shard and remove it from the config.

First, break the integration:

juju remove-relation config-server:config-server shard2:sharding

Watch juju status --watch 1s --relations until shard2 has finished being drained from the cluster and is no longer listed as a requirer of the config-server integration provider.

Now that the shard has been drained and removed from the configuration, it is safe to remove it altogether. To do so, run:

juju remove-application shard2

Watch the cluster with juju status --watch 1s --relations until shard2 is no longer listed anywhere.

Next Step: 5. Manage passwords

Last updated 8 hours ago. Help improve this document in the forum.