Skip to main content
Version: Preview

Create and connect to a MongoDB cluster

This tutorial shows how to create and connect to a MongoDB cluster.

Create a MongoDB cluster

Before you start

  • Install kbcli.

  • Install KubeBlocks.

  • Make sure the MongoDB addon is enabled. If this addon is not enabled, enable it first.

    kbcli addon list
    >
    NAME TYPE STATUS EXTRAS AUTO-INSTALL INSTALLABLE-SELECTOR
    ...
    mongodb Helm Enabled true
    ...
  • View all the database types and versions available for creating a cluster.

    kbcli clusterdefinition list

    kbcli clusterversion list
  • To keep things isolated, create a separate namespace called demo throughout this tutorial.

    kubectl create namespace demo
    >
    namespace/demo created

Create a cluster

KubeBlocks supports creating two types of MongoDB clusters: Standalone and ReplicaSet. Standalone only supports one replica and can be used in scenarios with lower requirements for availability. For scenarios with high availability requirements, it is recommended to create a ReplicaSet, which creates a cluster with two replicas to support automatic failover. To ensure high availability, all replicas are distributed on different nodes by default.

Create a Standalone.

kbcli cluster create <clustername> --cluster-definition mongodb

Create a ReplicatSet.

kbcli cluster create <clustername> --cluster-definition mongodb --set replicas=2 

If you only have one node for deploying a ReplicaSet, set the availability-policy as none when creating a ReplicaSet.

kbcli cluster create <clustername> --cluster-definition mongodb --set replicas=2 --topology-keys null

If you want to specify a cluster version, you can first view the available versions and use --version to specify a version.

kbcli clusterversion list

kbcli cluster create <clustername> --cluster-definition mongodb --version mongodb-6.0
note
  • In the production environment, it is not recommended to deploy all replicas on one node, which may decrease the cluster availability.

  • View more flags for creating a MongoDB cluster to create a cluster with customized specifications.

    kbcli cluster create --help

Connect to a MongoDB Cluster

kbcli cluster connect <clustername>  --namespace <name>

For the detailed database connection guide, refer to Connect database.