Operations
Backup And Restores
Custom Secret
tpl
This guide demonstrates how to manage a MongoDB ReplicaSet Cluster's operational state in KubeBlocks, including:
These operations help optimize resource usage and reduce operational costs in Kubernetes environments.
Lifecycle management operations in KubeBlocks:
| Operation | Effect | Use Case | 
|---|---|---|
| Stop | Suspends cluster, retains storage | Cost savings, maintenance | 
| Start | Resumes cluster operation | Restore service after pause | 
| Restart | Recreates pods for component | Configuration changes, troubleshooting | 
Before proceeding, ensure the following:
kubectl create ns demo
namespace/demo created
KubeBlocks uses a declarative approach for managing MongoDB Replication Clusters. Below is an example configuration for deploying a MongoDB ReplicaSet Cluster with one primary replica and two secondary replicas.
Apply the following YAML configuration to deploy the cluster:
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
  name: mongo-cluster
  namespace: demo
spec:
  terminationPolicy: Delete
  clusterDef: mongodb
  topology: replicaset
  componentSpecs:
    - name: mongodb
      serviceVersion: "6.0.21"
      replicas: 3
      resources:
        limits:
          cpu: '0.5'
          memory: 0.5Gi
        requests:
          cpu: '0.5'
          memory: 0.5Gi
      volumeClaimTemplates:
        - name: data
          spec:
            storageClassName: ""
            accessModes:
              - ReadWriteOnce
            resources:
              requests:
                storage: 20Gi
Monitor the cluster status until it transitions to the Running state:
kubectl get cluster mongo-cluster -n demo -w
Expected Output:
kubectl get cluster mongo-cluster -n demo
NAME            CLUSTER-DEFINITION   TERMINATION-POLICY   STATUS     AGE
mongo-cluster   mongodb              Delete               Creating   49s
mongo-cluster   mongodb              Delete               Running    62s
Check the pod status and roles:
kubectl get pods -l app.kubernetes.io/instance=mongo-cluster -L  kubeblocks.io/role -n demo
Expected Output:
NAME                      READY   STATUS    RESTARTS   AGE   ROLE
mongo-cluster-mongodb-0   2/2     Running   0          78s   primary
mongo-cluster-mongodb-1   2/2     Running   0          63s   secondary
mongo-cluster-mongodb-2   2/2     Running   0          48s   secondary
Once the cluster status becomes Running, your MongoDB cluster is ready for use.
If you are creating the cluster for the very first time, it may take some time to pull images before running.
Stopping a MongoDB ReplicaSet Cluster in KubeBlocks will:
This operation is ideal for:
Option 1: OpsRequest API
Create a Stop operation request:
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: mongo-cluster-stop-ops
  namespace: demo
spec:
  clusterName: mongo-cluster
  type: Stop
Option 2: Cluster API Patch
Modify the cluster spec directly by patching the stop field:
kubectl patch cluster mongo-cluster -n demo --type='json' -p='[
{
  "op": "add",
  "path": "/spec/componentSpecs/0/stop",
  "value": true
}
]'
To confirm a successful stop operation:
Check cluster status transition:
kubectl get cluster mongo-cluster -n demo -w
Example Output:
NAME            CLUSTER-DEFINITION   TERMINATION-POLICY   STATUS     AGE
mongo-cluster   mongodb              Delete               Stopping   6m3s
mongo-cluster   mongodb              Delete               Stopped    6m55s
Verify no running pods:
kubectl get pods -l app.kubernetes.io/instance=mongo-cluster -n demo
Example Output:
No resources found in demo namespace.
Confirm persistent volumes remain:
kubectl get pvc -l app.kubernetes.io/instance=mongo-cluster -n demo
Example Output:
NAME                           STATUS   VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS         VOLUMEATTRIBUTESCLASS   AGE
data-mongo-cluster-mongodb-0   Bound    pvc-uuid   20Gi       RWO            <STORAGECLASS>       <unset>                 22m
data-mongo-cluster-mongodb-1   Bound    pvc-uuid   20Gi       RWO            <STORAGECLASS>       <unset>                 21m
data-mongo-cluster-mongodb-2   Bound    pvc-uuid   20Gi       RWO            <STORAGECLASS>       <unset>                 21m
Starting a stopped MongoDB ReplicaSet Cluster:
Expected behavior:
Initiate a Start operation request:
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: mongo-cluster-start-ops
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: mongo-cluster
  type: Start
Modify the cluster spec to resume operation:
kubectl patch cluster mongo-cluster -n demo --type='json' -p='[
{
  "op": "remove",
  "path": "/spec/componentSpecs/0/stop"
}
]'
To confirm a successful start operation:
Check cluster status transition:
kubectl get cluster mongo-cluster -n demo -w
Example Output:
NAME            CLUSTER-DEFINITION   TERMINATION-POLICY   STATUS     AGE
mongo-cluster   mongodb              Delete               Updating   24m
mongo-cluster   mongodb              Delete               Running    24m
mongo-cluster   mongodb              Delete               Running    24m
Verify pod recreation:
kubectl get pods -n demo -l app.kubernetes.io/instance=mongo-cluster -L kubeblocks.io/role
Example Output:
NAME                      READY   STATUS    RESTARTS   AGE   ROLE
mongo-cluster-mongodb-0   2/2     Running   0          55s   primary
mongo-cluster-mongodb-1   2/2     Running   0          44s   secondary
mongo-cluster-mongodb-2   2/2     Running   0          33s   secondary
Restart operations provide:
Use cases:
Using OpsRequest API
Target specific components mongodb for restart:
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: mongo-cluster-restart-ops
  namespace: demo
spec:
  clusterName: mongo-cluster
  type: Restart
  restart:
  - componentName: mongodb
Verifying Restart Completion
To verify a successful component restart:
Track OpsRequest progress:
kubectl get opsrequest mongo-cluster-restart-ops -n demo -w
Example Output:
NAME                        TYPE      CLUSTER         STATUS    PROGRESS   AGE
mongo-cluster-restart-ops   Restart   mongo-cluster   Running   0/3        4s
mongo-cluster-restart-ops   Restart   mongo-cluster   Running   1/3        28s
mongo-cluster-restart-ops   Restart   mongo-cluster   Running   2/3        56s
mongo-cluster-restart-ops   Restart   mongo-cluster   Running   2/3        109s
Check pod status:
kubectl get pods -n demo -l app.kubernetes.io/instance=mongo-cluster
Note: Pods will show new creation timestamps after restart
Verify component health:
kbcli cluster describe mongo-cluster -n demo
Once the operation is complete, the cluster will return to the Running state.
In this guide, you learned how to:
By managing the lifecycle of your MongoDB ReplicaSet Cluster, you can optimize resource utilization, reduce costs, and maintain flexibility in your Kubernetes environment. KubeBlocks provides a seamless way to perform these operations, ensuring high availability and minimal disruption.