mongodb
by apecloud
MongoDB is a document database designed for ease of application development and scaling.
About this chart
MongoDB
MongoDB is a document database designed for ease of application development and scaling
Features In KubeBlocks
Lifecycle Management
| Topology | Horizontalscaling | Vertical scaling | Expandvolume | Restart | Stop/Start | Configure | Expose | Switchover |
|---|---|---|---|---|---|---|---|---|
| replica set | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
Backup and Restore
| Feature | Method | Description |
|---|---|---|
| Full Backup | dump | uses mongodump, a MongoDB utility used to create a binary export of the contents of a database |
| Full Backup | datafile | backup the data files of the database |
Versions
| Major Versions | Description |
|---|---|
| 4.0 | 4.0.28 |
| 4.4 | 4.4.29 |
| 5.0 | 5.0.29 |
| 6.0 | 6.0.27 |
| 7.0 | 7.0.28 |
| 8.0 | 8.0.17 |
Prerequisites
-
Kubernetes cluster >= v1.21
-
kubectlinstalled, refer to K8s Install Tools -
Helm, refer to Installing Helm
-
KubeBlocks installed and running, refer to Install Kubeblocks
-
MongoDB Addon Enabled, refer to Install Addons
-
Create K8s Namespace
demo, to keep resources created in this tutorial isolated:kubectl create ns demo
Examples
Create
Create a MongoDB replicaset cluster with 1 primary replica and 2 secondary replicas:
# cat examples/mongodb/cluster.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: mongo-cluster
namespace: demo
spec:
# Specifies the behavior when a Cluster is deleted.
# Valid options are: [DoNotTerminate, Delete, WipeOut] (`Halt` is deprecated since KB 0.9)
# - `DoNotTerminate`: Prevents deletion of the Cluster. This policy ensures that all resources remain intact.
# - `Delete`: Extends the `Halt` policy by also removing PVCs, leading to a thorough cleanup while removing all persistent data.
# - `WipeOut`: An aggressive policy that deletes all Cluster resources, including volume snapshots and backups in external storage. This results in complete data removal and should be used cautiously, primarily in non-production environments to avoid irreversible data loss.
terminationPolicy: Delete
# Specifies the name of the ClusterDefinition to use when creating a Cluster.
# Note: DO NOT UPDATE THIS FIELD
# The value must be `mongodb` to create a MongoDB Cluster
clusterDef: mongodb
# Specifies the name of the ClusterTopology to be used when creating the
# Cluster.
# Valid options are [replicaset]
topology: replicaset
# Specifies a list of ClusterComponentSpec objects used to define the
# individual Components that make up a Cluster.
# This field allows for detailed configuration of each Component within the Cluster
componentSpecs:
- name: mongodb
# ServiceVersion specifies the version of the Service expected to be
# provisioned by this Component.
# Valid options are: [8.0.17,7.0.28,6.0.27,5.0.29,4.4.29,4.0.28]
serviceVersion: "6.0.27"
# Specifies the desired number of replicas in the Component
replicas: 3
# Specifies the resources required by the Component.
resources:
limits:
cpu: '0.5'
memory: 0.5Gi
requests:
cpu: '0.5'
memory: 0.5Gi
# Specifies a list of PersistentVolumeClaim templates that define the storage
# requirements for the Component.
volumeClaimTemplates:
# Refers to the name of a volumeMount defined in
# `componentDefinition.spec.runtime.containers[*].volumeMounts
- name: data
spec:
# The name of the StorageClass required by the claim.
# If not specified, the StorageClass annotated with
# `storageclass.kubernetes.io/is-default-class=true` will be used by default
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
# Set the storage size as needed
storage: 20Gi
kubectl apply -f examples/mongodb/cluster.yaml
To check the roles of the pods, you can use following command:
# replace `mongo-cluster` with your cluster name
kubectl get po -n demo -l app.kubernetes.io/instance=mongo-cluster -L kubeblocks.io/role
If you want to create a cluster of specified version, set the spec.componentSpecs.serviceVersion field in the yaml file before applying it:
# snippet of cluster.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
spec:
componentSpecs:
- name: mongodb
# ServiceVersion specifies the version of the Service expected to be
# provisioned by this Component.
# Valid options are: [4.0.28,4.4.29,5.0.29,6.0.27,7.0.28,8.0.17]
serviceVersion: "7.0.28"
The list of supported versions can be found by following command:
kubectl get cmpv mongodb
What is MongoDB Replica Set?
A MongoDB replica set[^1] is a group of MongoDB servers that maintain the same dataset, providing high availability and data redundancy. Replica sets are the foundation of MongoDB's fault tolerance and data reliability. By replicating data across multiple nodes, MongoDB ensures that if one server fails, another can take over seamlessly without affecting the application's availability.
In a replica set, there are typically three types of nodes:
- Primary Node: Handles all write operations and serves read requests by default.
- Secondary Nodes: Maintain copies of the primary's data and can optionally serve read requests.
- Arbiter Node: Participates in elections but does not store data. It is used to maintain an odd number of voting members in the replica set.
And it is recommended to create a cluster with at least three nodes to ensure high availability, one primary and two secondary nodes.
Horizontal scaling
Scale-out
Horizontal scaling out cluster by adding ONE more replica:
# cat examples/mongodb/scale-out.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: mongo-scale-out
namespace: demo
spec:
# Specifies the name of the Cluster resource that this operation is targeting.
clusterName: mongo-cluster
type: HorizontalScaling
# Lists HorizontalScaling objects, each specifying scaling requirements for a Component, including desired total replica counts, configurations for new instances, modifications for existing instances, and instance downscaling options
horizontalScaling:
# Specifies the name of the Component.
- componentName: mongodb
# Specifies the replica changes for scaling in components
scaleOut:
# Specifies the replica changes for the component.
# add one more replica to current component
replicaChanges: 1
kubectl apply -f examples/mongodb/scale-out.yaml
After applying the operation, you will see a new pod created and the cluster status goes from Updating to Running, and the newly created pod has a new role secondary.
And you can check the progress of the scaling operation with following command:
kubectl describe -n demo ops mongo-scale-out
Scale-in
Horizontal scaling in cluster by deleting ONE replica:
# cat examples/mongodb/scale-in.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: mongo-scale-in
namespace: demo
spec:
# Specifies the name of the Cluster resource that this operation is targeting.
clusterName: mongo-cluster
type: HorizontalScaling
# Lists HorizontalScaling objects, each specifying scaling requirements for a Component, including desired total replica counts, configurations for new instances, modifications for existing instances, and instance downscaling options
horizontalScaling:
# Specifies the name of the Component.
- componentName: mongodb
# Specifies the replica changes for scaling in components
scaleIn:
# Specifies the replica changes for the component.
# add one more replica to current component
replicaChanges: 1
kubectl apply -f examples/mongodb/scale-in.yaml
On horizontal scaling in/out, member list of the replica set will be updated to make sure the cluster is healthy.
You may verify the full list of members in the replica set by connecting to any pod, and running the following command:
mongo-cluster-mongodb > rs.status();
Set Specified Replicas Offline
There are cases where you want to set a specified replica offline, when it is problematic or you want to do some maintenance work on it. You can use the onlineInstancesToOffline field in the spec.horizontalScaling.scaleIn section to specify the instance names that need to be taken offline.
# snippet of opsrequest
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
spec:
clusterName: mongo-cluster
horizontalScaling:
- componentName: mongodb
# Specifies the replica changes for scaling out components
scaleIn:
onlineInstancesToOffline:
- 'mongo-cluster-mongodb-1' # Specifies the instance names that need to be taken offline
Scale-in/out using Cluster API
Alternatively, you can update the replicas field in the spec.componentSpecs.replicas section to your desired non-zero number.
# snippet of cluster.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
spec:
componentSpecs:
- name: mongodb
replicas: 3 # set desired number of replicas
# Optional. Specifies the names of instances to be transitioned to offline status.
# If no specified, KubeBlocks will select the instances in descending ordinal number order.
offlineInstances:
- mongo-cluster-mongodb-1
Vertical scaling
Vertical scaling involves increasing or decreasing resources to an existing database cluster. Resources that can be scaled include:, CPU cores/processing power and Memory (RAM).
To vertical scaling up or down specified component, you can apply the following yaml file:
# cat examples/mongodb/verticalscale.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: mongo-verticalscaling
namespace: demo
spec:
# Specifies the name of the Cluster resource that this operation is targeting.
clusterName: mongo-cluster
type: VerticalScaling
# Lists VerticalScaling objects, each specifying a component and its desired compute resources for vertical scaling.
verticalScaling:
- componentName: mongodb
# VerticalScaling refers to the process of adjusting the compute resources (e.g., CPU, memory) allocated to a Component. It defines the parameters required for the operation.
requests:
cpu: '1'
memory: 1Gi
limits:
cpu: '1'
memory: 1Gi
kubectl apply -f examples/mongodb/verticalscale.yaml
You will observe that the secondary pods are recreated first, followed by the primary pod, to ensure the availability of the cluster.
Scale-up/down using Cluster API
Alternatively, you may update spec.componentSpecs.resources field to the desired resources for vertical scale.
# snippet of cluster.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
spec:
componentSpecs:
- name: mongodb
replicas: 3
resources:
requests:
cpu: "1" # Update the resources to your need.
memory: "2Gi" # Update the resources to your need.
limits:
cpu: "2" # Update the resources to your need.
memory: "4Gi" # Update the resources to your need.
Expand volume
Volume expansion is the ability to increase the size of a Persistent Volume Claim (PVC) after it's created. It is introduced in Kubernetes v1.11 and goes GA in Kubernetes v1.24. It allows Kubernetes users to simply edit their PersistentVolumeClaim objects without requiring any downtime at all if possible.
[!NOTE] Make sure the storage class you use supports volume expansion.
Check the storage class with following command:
kubectl get storageclass
If the ALLOWVOLUMEEXPANSION column is true, the storage class supports volume expansion.
To increase size of volume storage with the specified components in the cluster
# cat examples/mongodb/volumeexpand.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: mongo-volumeexpansion
namespace: demo
spec:
# Specifies the name of the Cluster resource that this operation is targeting.
clusterName: mongo-cluster
type: VolumeExpansion
# Lists VolumeExpansion objects, each specifying a component and its corresponding volumeClaimTemplates that requires storage expansion.
volumeExpansion:
# Specifies the name of the Component.
- componentName: mongodb
# volumeClaimTemplates specifies the storage size and volumeClaimTemplate name.
volumeClaimTemplates:
- name: data
storage: 30Gi
kubectl apply -f examples/mongodb/volumeexpand.yaml
After the operation, you will see the volume size of the specified component is increased to 30Gi in this case. Once you've done the change, check the status.conditions field of the PVC to see if the resize has completed.
kubectl get pvc -l app.kubernetes.io/instance=mongo-cluster -n demo
Volume expansion using Cluster API
Alternatively, you may update the spec.componentSpecs.volumeClaimTemplates.spec.resources.requests.storage field to the desired size.
apiVersion: apps.kubeblocks.io/v1
# snippet of cluster.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
spec:
componentSpecs:
- name: mongodb
volumeClaim
Deploy mongodb with OpenTenant
Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has mongodb on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.
- Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
- Place the chartmongodb is pinned at v1.2.0-alpha.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
- ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below opentenant validate opentenant.yaml terraform init && terraform apply