Skip to main content
Version: Preview

Scale for a MySQL cluster

You can scale a MySQL cluster in two ways, vertical scaling and horizontal scaling.

From v0.9.0, for MySQL and PostgreSQL, after vertical scaling or horizontal scaling is performed, KubeBlocks automatically matches the appropriate configuration template based on the new specification. This is the KubeBlocks dynamic configuration feature. This feature simplifies the process of configuring parameters, saves time and effort and reduces performance issues caused by incorrect configuration. For detailed instructions, refer to Configuration.

Vertical scaling

You can vertically scale a cluster by changing resource requirements and limits (CPU and storage). For example, if you need to change the resource class from 1C2G to 2C4G, vertical scaling is what you need.

note

During the vertical scaling process, all pods restart in the order of learner -> follower -> leader and the leader pod may change after the restarting.

Before you start

Check whether the cluster status is Running. Otherwise, the following operations may fail.

kubectl get cluster mycluster
>
NAME CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS AGE
mycluster mysql mysql-8.0.33 Delete Running 4d18h

Steps

There are two ways to apply vertical scaling.

  1. Apply an OpsRequest to the specified cluster. Configure the parameters according to your needs.

    kubectl apply -f - <<EOF
    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: OpsRequest
    metadata:
    name: ops-vertical-scaling
    namespace: demo
    spec:
    clusterName: mycluster
    type: VerticalScaling
    verticalScaling:
    - componentName: mysql
    requests:
    memory: "2Gi"
    cpu: "1"
    limits:
    memory: "4Gi"
    cpu: "2"
    EOF
  2. Check the operation status to validate the vertical scaling.

    kubectl get ops -n demo
    >
    NAMESPACE NAME TYPE CLUSTER STATUS PROGRESS AGE
    demo ops-vertical-scaling VerticalScaling mycluster Succeed 3/3 6m

    If an error occurs to the vertical scaling operation, you can troubleshoot with kubectl describe ops -n demo command to view the events of this operation.

  3. Check whether the corresponding resources change.

    kubectl describe cluster mycluster -n demo
  1. Change the configuration of spec.componentSpecs.resources in the YAML file. spec.componentSpecs.resources controls the requirement and limit of resources and changing them triggers a vertical scaling.

    kubectl edit cluster mycluster -n demo
    >
    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: Cluster
    metadata:
    name: mycluster
    namespace: demo
    spec:
    clusterDefinitionRef: apecloud-mysql
    clusterVersionRef: ac-mysql-8.0.30
    componentSpecs:
    - name: mysql
    componentDefRef: mysql
    replicas: 3
    resources: # Change the values of resources.
    requests:
    memory: "2Gi"
    cpu: "1"
    limits:
    memory: "4Gi"
    cpu: "2"
    volumeClaimTemplates:
    - name: data
    spec:
    accessModes:
    - ReadWriteOnce
    resources:
    requests:
    storage: 20Gi
    terminationPolicy: Delete
  2. Check whether the corresponding resources change.

    kubectl describe cluster mycluster -n demo

Horizontal scaling

Horizontal scaling changes the amount of pods. For example, you can apply horizontal scaling to scale pods up from three to five. The scaling process includes the backup and restore of data.

note

From v0.9.0, after horizontal scaling is performed, KubeBlocks will automatically adjust part of the parameters of the database instance to more appropriate values. It is recommended to back up and record your custom parameter settings so that you can restore them if needed.

Before you start

Check whether the cluster STATUS is Running. Otherwise, the following operations may fail.

kubectl get cluster mycluster
>
NAME CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS AGE
mycluster mysql mysql-8.0.33 Delete Running 4d19h

Steps

There are two ways to apply horizontal scaling.

  1. Apply an OpsRequest to a specified cluster. Configure the parameters according to your needs.

    kubectl apply -f - <<EOF
    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: OpsRequest
    metadata:
    name: ops-horizontal-scaling
    namespace: demo
    spec:
    clusterName: mycluster
    type: HorizontalScaling
    horizontalScaling:
    - componentName: mysql
    replicas: 1
    EOF
  2. Check the operation status to validate the horizontal scaling.

    kubectl get ops -n demo
    >
    NAMESPACE NAME TYPE CLUSTER STATUS PROGRESS AGE
    demo ops-horizontal-scaling HorizontalScaling mycluster Succeed 3/3 6m

    If an error occurs to the horizontal scaling operation, you can troubleshoot with kubectl describe ops -n demo command to view the events of this operation.

  3. Check whether the corresponding resources change.

    kubectl describe cluster mycluster -n demo
  1. Change the value of spec.componentSpecs.replicas in the YAML file.

    spec.componentSpecs.replicas stands for the pod amount and changing this value triggers a horizontal scaling of a cluster.

    kubectl edit cluster mycluster -n demo
    >
    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: Cluster
    metadata:
    name: mycluster
    namespace: demo
    spec:
    clusterDefinitionRef: apecloud-mysql
    clusterVersionRef: ac-mysql-8.0.30
    componentSpecs:
    - name: mysql
    componentDefRef: mysql
    replicas: 1 # Change the amount
    volumeClaimTemplates:
    - name: data
    spec:
    accessModes:
    - ReadWriteOnce
    resources:
    requests:
    storage: 20Gi
    terminationPolicy: Delete
  2. Check whether the corresponding resources change.

    kubectl describe cluster mycluster -n demo

Handle the snapshot exception

If STATUS=ConditionsError occurs during the horizontal scaling process, you can find the cause from cluster.status.condition.message for troubleshooting. In the example below, a snapshot exception occurs.

Status:
conditions:
- lastTransitionTime: "2023-02-08T04:20:26Z"
message: VolumeSnapshot/mycluster-mysql-scaling-dbqgp: Failed to set default snapshot
class with error cannot find default snapshot class
reason: ApplyResourcesFailed
status: "False"
type: ApplyResources

Reason

This exception occurs because the VolumeSnapshotClass is not configured. This exception can be fixed after configuring VolumeSnapshotClass, but the horizontal scaling cannot continue to run. It is because the wrong backup (volumesnapshot is generated by backup) and volumesnapshot generated before still exist. First, delete these two wrong resources and then KubeBlocks re-generates new resources.

Steps:

  1. Configure the VolumeSnapshotClass by running the command below.

    kubectl create -f - <<EOF
    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshotClass
    metadata:
    name: csi-aws-vsc
    annotations:
    snapshot.storage.kubernetes.io/is-default-class: "true"
    driver: ebs.csi.aws.com
    deletionPolicy: Delete
    EOF
  2. Delete the wrong backup (volumesnapshot is generated by backup) and volumesnapshot resources.

    kubectl delete backup -l app.kubernetes.io/instance=mycluster

    kubectl delete volumesnapshot -l app.kubernetes.io/instance=mycluster

Result

The horizontal scaling continues after backup and volumesnapshot are deleted and the cluster restores to running status.