Skip to content

MySQL Deployment

1. Overview

MySQL is a lightweight relational database management system developed by MySQL AB in Sweden and currently owned by Oracle Corporation. MySQL is widely used in small and medium-sized websites on the Internet. Because of its small footprint, high speed, low total cost of ownership, open source, and free availability, most small and medium-sized websites choose Linux + MySQL as their website database.

MySQL is a relational database management system. It stores data in separate tables rather than putting all data in one large repository, thereby increasing speed and flexibility.

2. Prerequisites

  • A Kubernetes cluster is already deployed.
  • The OpenEBS storage plugin driver is already deployed.
root@k8s-node01 /etc]# kubectl  get pods -n kube-system|grep openebs
openebs-localpv-provisioner-6b56b5567c-k5r9l   1/1     Running   0          23h
openebs-ndm-jqxtr                              1/1     Running   0          23h
openebs-ndm-operator-5df6ffc98-cplgp           1/1     Running   0          23h
openebs-ndm-qtjxm                              1/1     Running   0          23h
openebs-ndm-vlcrv                              1/1     Running   0          23h

3. Installation Preparation

Since MySQL is resource-intensive and requires dedicated cluster resources, you need to configure cluster scheduling in advance.

4. Cluster Label Configuration

Run the command to label the cluster:

# According to the cluster plan, label the node where MySQL will be deployed (single node)
# xxx represents the actual node in the cluster for deploying mysql service
kubectl label nodes xxx mysql=true

Check the label:

kubectl get nodes --show-labels  | grep 'mysql'

5. Cluster Taint Configuration

Run the command to configure taints on the cluster.

Tip

If no dedicated node is planned for the MySQL service, you can skip this step.

kubectl taint node  xxxx infrastructure=middleware:NoExecute

6. Configure StorageClass

Configure a dedicated StorageClass for MySQL.

# Copy the following yaml content to the k8s cluster, save it as a yaml file, modify as needed, and then deploy
apiVersion: storage.k8s.io/v1
allowVolumeExpansion: true
kind: StorageClass
metadata:
  annotations:
    cas.openebs.io/config: |
      - name: StorageType
        value: "hostpath"
      - name: BasePath
        value: "/data/mysql"  # This path can be modified according to actual conditions. Ensure sufficient storage space and that the path exists.
  name: openebs-mysql  # Name must be unique within the cluster. Must be modified synchronously before installation in the deployment file /etc/kubeasz/guance/infrastructure/yaml/mysql.yaml
provisioner: openebs.io/local
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer

Ensure the disk capacity of the /data/mysql directory is sufficient.

Configure the data storage volume:

# Navigate to /etc/kubeasz/guance/infrastructure/yaml/mysql.yaml and modify as needed
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-data
spec:
  accessModes:
  - ReadWriteOnce
  volumeMode: Filesystem
  resources:
    requests:
      storage: 10Gi  # Set the size according to actual usage
  storageClassName:  openebs-mysql

7. Installation

# Default root password is "mQ2LZenlYs1UoVzi"
# Can be modified before installation via /etc/kubeasz/guance/infrastructure/yaml/mysql.yaml

# Create namespace
kubectl  create  ns middleware
# Deploy mysql service
kubectl  apply  -f /etc/kubeasz/guance/infrastructure/yaml/mysql.yaml  -n middleware

8. Verify Deployment and Configuration

8.1 Check Container Status

[root@k8s-node01 /etc/kubeasz/guance/infrastructure/yaml]# kubectl  get pods -n middleware  |grep mysql
mysql-75499f6fcd-6tc7l      1/1     Running   0          4m36s

8.2 Verify Service

Verify MySQL service availability
# Default root password is "mQ2LZenlYs1UoVzi"
# Can be modified before installation via /etc/kubeasz/guance/infrastructure/yaml/mysql.yaml

[root@k8s-node01 /etc/kubeasz/guance/infrastructure/yaml]# kubectl  exec -it -n middleware  mysql-xxxxx  -- mysql -uroot  -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| FT2.0              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

Feedback

Is this page helpful?