Topologies
Operations
Backup And Restores
Custom Secret
Monitoring
This guide demonstrates how to manage the lifecycle of a MySQL cluster in KubeBlocks, including stopping, starting, and restarting the cluster. Proper lifecycle management helps optimize resource usage, reduce operational costs, and ensure flexibility in your Kubernetes environment.
Before proceeding, ensure the following:
kubectl create ns demo
namespace/demo created
KubeBlocks uses a declarative approach for managing MySQL clusters. Below is an example configuration for deploying a MySQL cluster with 2 nodes (1 primary, 1 replicas) in semi-synchronous mode.
Apply the following YAML configuration to deploy the cluster:
kubectl apply -f - <<EOF
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: example-mysql-cluster
namespace: demo
spec:
clusterDef: mysql
topology: semisync
terminationPolicy: Delete
componentSpecs:
- name: mysql
serviceVersion: 8.0.35
replicas: 2
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
EOF
Monitor the cluster status until it transitions to the Running state:
kubectl get cluster example-mysql-cluster -n demo -w
Example Output:
NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE
example-mysql-cluster mysql Delete Creating 7s
example-mysql-cluster mysql Delete Running 63s
Once the cluster status becomes Running, your MySQL cluster is ready for use.
Stopping the cluster terminates all its running pods but retains the persistent storage. This is useful when you want to temporarily suspend the cluster to save costs.
Option 1: Using OpsRequest
You can stop the cluster using an OpsRequest:
kubectl apply -f - <<EOF
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: example-mysql-cluster-stop-ops
namespace: demo
spec:
clusterName: example-mysql-cluster
type: Stop
EOF
Option 2: Using the Declarative Cluster API
Alternatively, you may stop the cluster by setting the spec.componentSpecs.stop
field to true
in the cluster configuration:
kubectl patch cluster example-mysql-cluster -n demo --type='json' -p='[
{
"op": "add",
"path": "/spec/componentSpecs/0/stop",
"value": true
}
]'
Monitor the cluster's status to ensure it transitions to the Stopped state:
kubectl get cluster -n demo -w
Example Output:
NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE
example-mysql-cluster mysql Delete Stopping 93s
example-mysql-cluster mysql Delete Stopped 101s
There is no Pods running in the cluster, but the persistent storage is retained.
kubectl get pods -n demo
Expected Output:
No resources found in demo namespace.
kubectl get pvc -n demo
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
data-example-mysql-cluster-mysql-0 Bound pvc-98ce87ab-acc6-4f95-8638-16e8052f98d8 20Gi RWO kb-default-sc <unset> 16m
data-example-mysql-cluster-mysql-1 Bound pvc-5bb87b23-7c38-45de-bf04-f2822051d897 20Gi RWO kb-default-sc <unset> 16m
Starting the cluster recreates the pods and brings the cluster back online.
Option 1: Using OpsRequest
You can start the stopped cluster using an OpsRequest:
kubectl apply -f - <<EOF
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: example-mysql-cluster-start-ops
namespace: demo
spec:
# Specifies the name of the Cluster resource that this operation is targeting.
clusterName: example-mysql-cluster
type: Start
EOF
Option 1: Using the Declarative Cluster API
Alternatively, you can start the cluster by:
spec.componentSpecs.stop
field to false, orspec.componentSpecs.stop
field entirely.kubectl patch cluster example-mysql-cluster -n demo --type='json' -p='[
{
"op": "remove",
"path": "/spec/componentSpecs/0/stop"
}
]'
Monitor the cluster's status to ensure it transitions back to the Running state:
kubectl get cluster -n demo -w
Example Output:
NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE
example-mysql-cluster mysql Delete Updating 5m54s
example-mysql-cluster mysql Delete Running 6m6s
Restarting the cluster allows you to recreate the pods for specific components without deleting or stopping the entire cluster.
To restart a specific component (e.g., mysql), use the following OpsRequest:
kubectl apply -f - <<EOF
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: example-mysql-cluster-restart-ops
namespace: demo
spec:
clusterName: example-mysql-cluster
type: Restart
restart:
- componentName: mysql
EOF
Monitor the cluster's status to ensure the restart is completed successfully:
kubectl get opsrequest example-mysql-cluster-restart-ops -n demo -w
Expected Output:
NAME TYPE CLUSTER STATUS PROGRESS AGE
example-mysql-cluster-restart-ops Restart example-mysql-cluster Succeed 2/2 3m16s
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 MySQL 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.