Skip to main content
Version: release-0.9

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 if you want to create and connect a MySQL cluster by 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
    ...
    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.

  1. Create a MongoDB cluster.

    kbcli cluster create mycluster --cluster-definition mongodb -n demo

    The commands above are some common examples to create a cluster with default settings. If you want to customize your cluster specifications, kbcli provides various options, such as setting cluster version, termination policy, CPU, and memory. You can view these options by adding --help or -h flag.

    kbcli cluster create mongodb --help
    kbcli cluster create mongodb -h
  2. Verify whether this cluster is created successfully.

    kbcli cluster list -n demo
    >
    NAME NAMESPACE CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS CREATED-TIME
    mycluster demo mongodb mongodb-5.0 Delete Running Sep 20,2024 10:01 UTC+0800

Connect to a MongoDB Cluster

kbcli cluster connect mycluster -n demo

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