mysql
by apecloud
MySQL is a widely used, open-source relational database management system (RDBMS)
About this chart
Mysql
MySQL is a widely used, open-source relational database management system (RDBMS)
Features In KubeBlocks
Lifecycle Management
| Topology | Horizontalscaling | Vertical scaling | Expandvolume | Restart | Stop/Start | Configure | Expose | Switchover |
|---|---|---|---|---|---|---|---|---|
| replication | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
Backup and Restore
| Feature | Method | Description |
|---|---|---|
| Full Backup | xtrabackup | uses xtrabackup, an open-source tool developed by Percona to perform full backups |
Versions
| Major Versions | Description |
|---|---|
| 5.7 | 5.7.44 |
| 8.0 | 8.0.45, 8.0.46 |
| 8.4 | 8.4.9, 8.4.10 |
MySQL 8.0.46 with MGR is temporarily unsupported because of an upstream Group Replication defect
(MySQL issue #696) that can abort the server in
the IP allowlist path. Support will be restored after an upstream-fixed image passes addon validation.
Existing MGR users who explicitly selected 8.0.46 should set serviceVersion: 8.0.45 before their next
reconcile or upgrade; the MGR 8.0 default now resolves explicitly to 8.0.45. MySQL 8.4.9 and 8.4.10
also remain available for MGR. Other MySQL 8.0.46 topologies remain supported, including standalone,
replication, and Orchestrator deployments.
Prerequisites
-
Kubernetes cluster >= v1.21
-
kubectlinstalled, refer to K8s Install Tools -
Helm, refer to Installing Helm
-
KubeBlocks installed and running, refer to Install Kubeblocks
-
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
Cluster with built-in HA Manager
Create a MySQL cluster with two replicas that uses the built-in HA manager
# cat examples/mysql/cluster.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: mysql-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 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
# Specifies the ComponentDefinition custom resource (CR) that defines the
# Component's characteristics and behavior.
# Supports three different ways to specify the ComponentDefinition:
# - the regular expression - recommended
# - the full name - recommended
# - the name prefix
componentDef: "mysql-8.0" # match all CMPD named with 'mysql-8.0-'
# ServiceVersion specifies the version of the Service expected to be
# provisioned by this Component.
# When componentDef is "mysql-8.0",
# Valid options are: [8.0.45, 8.0.46]
serviceVersion: 8.0.46
# 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
replicas: 2
# 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/mysql/cluster.yaml
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
componentDef: "mysql-8.0" # match all CMPD named with 'mysql-8.0-'
# ServiceVersion specifies the version of the Service expected to be
# provisioned by this Component.
# When componentDef is "mysql-8.0",
# Valid options are: [8.0.45, 8.0.46]
serviceVersion: 8.0.46
The list of supported versions can be found by following command:
kubectl get cmpv mysql
Horizontal scaling
Scale-out
Horizontal scaling out MySQL cluster by adding ONE more replica:
# cat examples/mysql/scale-out.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: mysql-scale-out
namespace: demo
spec:
# Specifies the name of the Cluster resource that this operation is targeting.
clusterName: mysql-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 in components
scaleOut:
# Specifies the replica changes for the component.
# add one more replica to current component
replicaChanges: 1
kubectl apply -f examples/mysql/scale-out.yaml
After applying the operation, you will see a new pod created and the MySQL 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 mysql-scale-out
Scale-in
Horizontal scaling in MySQL cluster by deleting ONE replica:
# cat examples/mysql/scale-in.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: mysql-scale-in
namespace: demo
spec:
# Specifies the name of the Cluster resource that this operation is targeting.
clusterName: mysql-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 in components
scaleIn:
# Specifies the replica changes for the component.
# add one more replica to current component
replicaChanges: 1
kubectl apply -f examples/mysql/scale-in.yaml
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: mysql
replicas: 2 # 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/mysql/verticalscale.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: mysql-verticalscaling
namespace: demo
spec:
# Specifies the name of the Cluster resource that this operation is targeting.
clusterName: mysql-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/mysql/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: mysql
replicas: 2
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[^4].
[!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/mysql/volumeexpand.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: mysql-volumeexpansion
namespace: demo
spec:
# Specifies the name of the Cluster resource that this operation is targeting.
clusterName: mysql-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/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=mysql-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: "<you-preferred-sc>"
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 examples/mysql/restart.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: mysql-restart
namespace: demo
spec:
# Specifies the name of the Cluster resource that this operation is targeting.
clusterName: mysql-cluster
type: Restart
# Lists Components to be restarted. ComponentOps specifies the Component to be operated on.
restart:
# Specifies the name of the Component.
- componentName: mysql
kubectl apply -f examples/mysql/restart.yaml
Stop
Stop the cluster will release all the pods of the cluster, but the storage will be retained. It is useful when you want to save the cost of the cluster.
# cat examples/mysql/stop.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: mysql-stop
namespace: demo
spec:
# Specifies the name of the Cluster resource that this operation is targeting.
clusterName: mysql-cluster
type: Stop
kubectl apply -f examples/mysql/stop.yaml
Stop using Cluster API
Alternatively, you may stop the cluster by setting the spec.componentSpecs.stop field to `true
Deploy mysql with OpenTenant
Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has 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.
- Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
- Place the chartmysql 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