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

An error occurred while submitting your form. Please try again or file a bug report. Close

Charmed MongoDB Tutorials > Deploy a sharded cluster > 2. Deploy MongoDB

Deploy MongoDB

Sharding is MongoDB’s solution to horizontal scaling. MongoDB’s documentation describes horizontal scaling in databases as the process of “ dividing a large database into smaller, more manageable pieces (called shards) and then distributing the shards across multiple machines.”

In Charmed MongoDB K8s, we create sharded clusters by deploying several MongoDB applications as individual cluster components, and then integrating them. This part of the tutorial will guide you through a practical example of a sharded cluster deployment.

Summary


Deploy cluster components

The first step to creating your sharded cluster is to deploy cluster components.

If you run juju deploy mongodb-k8s without specifying a role, the application will be automatically deployed as a replica set. To deploy a sharded cluster, we must manually define the roles of the individual cluster components:

juju deploy mongodb-k8s --config role="config-server" config-server
juju deploy mongodb-k8s --config role="shard" shard0
juju deploy mongodb-k8s --config role="shard" shard1

For a deeper explanation about the difference between replication and sharding see Explanation | Replication vs. sharded cluster deployments.

Use juju status --watch 1s to wait until these components are ready.

When the components are ready, your output should look similar to the example below:

Model           Controller  Cloud/Region         Version  SLA          Timestamp
tutorial        overlord    microk8s/localhost   3.1.7    unsupported  19:06:51+01:00

App            Version  Status   Scale  Charm    Channel    Rev  Exposed  Message    
config-server           blocked      1  mongodb  6/stable   158  no       Missing relation to shard(s). 
shard0                  blocked      1  mongodb  6/stable   158  no       Missing relation to config-server.
shard1                  blocked      1  mongodb  6/stable   158  no       Missing relation to config-server.

Unit              Workload  Agent    Address        Ports    Message    
config-server/0*  blocked   idle     10.56.148.138           Missing relation to shard(s).      
shard0/0*         blocked   idle     10.56.148.149           Missing relation to config-server.
shard1/0*         blocked   idle     10.56.148.60            Missing relation to config-server.   

All statuses are blocked because the shards are not yet integrated/related with the config-server.

Integrate shards with the config server

Juju integrations are a way to connect applications such that they can communicate with one another through compatible endpoints.

To integrate the shards with the config server, run

juju integrate config-server:config-server shard0:sharding
juju integrate config-server:config-server shard1:sharding
You’ll know your cluster is active and ready when juju status --watch 1s shows all apps as active.
Model           Controller  Cloud/Region         Version  SLA          Timestamp
tutorial        overlord    microk8s/localhost   3.1.7    unsupported  19:15:36+01:00

App            Version  Status  Scale  Charm    Channel  Rev  Exposed  Message     
config-server           active      1  mongodb  6/stable 158  no                                            
shard0                  active      1  mongodb  6/stable 158  no                                  
shard1                  active      1  mongodb  6/stable 158  no                                  

Unit              Workload  Agent    Address        Ports    Message    
config-server/0*  blocked   idle     10.56.148.138           Primary   
shard0/0*         blocked   idle     10.56.148.149           Primary
shard1/0*         blocked   idle     10.56.148.60            Primary                            

Next step: 3. Access a sharded cluster

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