apecloud-mysql

by apecloud

Helm chartv1.2.0-alpha.0App 8.0.30Verified publisher

ApeCloud MySQL is a database that is compatible with MySQL syntax and achieves high availability through the utilization of the RAFT consensus protocol.

About this chart

ApeCloud MySQL

ApeCloud MySQL is a database that is compatible with MySQL syntax and achieves high availability through the utilization of the RAFT consensus protocol. This example shows how it can be managed in Kubernetes with KubeBlocks.

Please refer to the ApeCloud MySQL Documentation[^1] for more information.

Features In KubeBlocks

Lifecycle Management

HorizontalscalingVertical scalingExpandvolumeRestartStop/StartConfigureExposeSwitchover
YesYesYesYesYesYesYesYes

Backup and Restore

FeatureMethodDescription
Full Backupxtrabackupuses xtrabackup, an open-source tool developed by Percona to perform full backups

Versions

Major VersionsDescription
8.08.0.30

Prerequisites

  • Kubernetes cluster >= v1.21

  • kubectl installed, refer to K8s Install Tools

  • Helm, refer to Installing Helm

  • KubeBlocks installed and running, refer to Install Kubeblocks

  • ApeCloud MySQL 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 ApeCloud-MySQL cluster with three replicas:

# cat examples/apecloud-mysql/cluster.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
  name: acmysql-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 `apecloud-mysql` to create a ApeCloud-MySQL Cluster
  clusterDef: apecloud-mysql
  # Specifies the name of the ClusterTopology to be used when creating the
  # Cluster.
  topology: apecloud-mysql
  # 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: mysql
      # ServiceVersion specifies the version of the Service expected to be
      # provisioned by this Component.
      # Valid options are: [8.0.30]
      serviceVersion: "8.0.30"
      # Determines whether metrics exporter information is annotated on the
      # Component's headless Service.
      # Valid options are [true, false]
      disableExporter: false
      # Specifies the desired number of replicas in the Component
      # ApeCloud-MySQL prefers ODD numbers like [1, 3, 5, 7]
      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/apecloud-mysql/cluster.yaml

And you will see the ApeCloud-MySQL cluster status goes Running after a while:

kubectl get cluster acmysql-cluster -n demo

and three pods are Running with roles leader, follower and follower separately. To check the roles of the pods, you can use following command:

# replace `acmysql-cluster` with your cluster name
kubectl get po -l  app.kubernetes.io/instance=acmysql-cluster -L kubeblocks.io/role -n demo

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: mysql
      # ServiceVersion specifies the version of the Service expected to be
      # provisioned by this Component.
      # Valid options are: [8.0.30]
      serviceVersion: "8.0.30" # more ApeCloud-MySQL versions will be supported in the future

The list of supported versions can be found by following command:

kubectl get cmpv apecloud-mysql

Horizontal scaling

[!IMPORTANT] As per the ApeCloud MySQL documentation, the number of Raft replicas should be odd to avoid split-brain scenarios. Make sure the number of ApeCloud MySQL replicas, is always odd after Horizontal Scaling.

Scale-out

Horizontal scaling out ApeCloud-MySQL cluster by adding ONE more replica:

# cat examples/apecloud-mysql/scale-out.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: acmysql-scale-out
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: acmysql-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: mysql
    # Specifies the replica changes for scaling out components
    scaleOut:
      # Specifies the replica changes for the component.
      # add one more replica to current component
      replicaChanges: 1

kubectl apply -f examples/apecloud-mysql/scale-out.yaml

After applying the operation, you will see a new pod created and the ApeCloud-MySQL cluster status goes from Updating to Running, and the newly created pod has a new role follower.

And you can check the progress of the scaling operation with following command:

kubectl describe -n demo ops acmysql-scale-out

[!IMPORTANT] On scaling out, the new replica will be added as a follower, and KubeBlocks will clone data from the leader to the new follower before the replica starts as defined.

Scale-in

Horizontal scaling in ApeCloud-MySQL cluster by deleting ONE replica:

# cat examples/apecloud-mysql/scale-in.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: acmysql-scale-in
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: acmysql-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: mysql
    # Specifies the replica changes for scaling out components
    scaleIn:
      # Specifies the replica changes for the component.
      # add one more replica to current component
      replicaChanges: 1

kubectl apply -f examples/apecloud-mysql/scale-in.yaml

[!IMPORTANT] On scaling in, the replica will be forgotten from the cluster's Raft group before it is deleted.

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.

apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
spec:
  horizontalScaling:
  - componentName: mysql
    # Specifies the replica changes for scaling out components
    scaleIn:
      onlineInstancesToOffline:
        - 'acmysql-cluster-mysql-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: apecloud-mysql
      replicas: 3 # decrease `replicas` for scaling in, and increase for scaling out

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/apecloud-mysql/verticalscale.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: acmysql-verticalscaling
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: acmysql-cluster
  type: VerticalScaling
  # Lists VerticalScaling objects, each specifying a component and its desired compute resources for vertical scaling.
  verticalScaling:
  - componentName: mysql
    # 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/apecloud-mysql/verticalscale.yaml

You will observe that the follower pods are recreated first, followed by the leader 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: mysql
      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/apecloud-mysql/volumeexpand.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
  name: acmysql-volumeexpansion
  namespace: demo
spec:
  # Specifies the name of the Cluster resource that this operation is targeting.
  clusterName: acmysql-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: mysql
    # volumeClaimTemplates specifies the storage size and volumeClaimTemplate name.
    volumeClaimTemplates:
    - name: data
      storage: 30Gi

kubectl apply -f examples/apecloud-mysql/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=acmysql-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.

# snippet of cluster.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
spec:
  componentSpecs:
    - name: mysql
      volumeClaimTemplates:
        - name: data
          spec:
            storageClassName: "<STORAGE_CLASS_NAME>"
            accessModes:
              - ReadWriteOnce
            resources:
              requests:
                storage: 30Gi  # specify new size, and make sure it is larger than the current size

Restart

Restart the specified components in the cluster

# cat examp

Deploy apecloud-mysql with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has apecloud-mysql 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.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartapecloud-mysql is pinned at v1.2.0-alpha.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. 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