helm 安装 MySQL 主从

资源清单

本文安装 MySQL 1主2从 依赖 K8S集群helm ,本文不提供 K8S集群helm 安装方式

使用此文档部署,需要自行解决 storageClass 问题 ( NFS, ceph, openebs等 )

软件 版本
chart 4.5.2
mysql 5.7.26
kubernetes v1.19.3
helm v3.8.1

helm 安装 MySQL 1主2从

1. 添加 bitnami 的仓库

$ helm repo add bitnami https://charts.bitnami.com/bitnami

2. 查询 MySQL 资源

$ helm repo update
$ helm search repo mysql
NAME                   	CHART VERSION	APP VERSION	DESCRIPTION                                       
bitnami/mysql          	8.9.6        	8.0.29     	MySQL is a fast, reliable, scalable, and easy t...
bitnami/phpmyadmin     	10.0.1       	5.1.3      	phpMyAdmin is a free software tool written in P...
bitnami/mariadb        	11.0.2       	10.6.7     	MariaDB is an open source, community-developed ...
bitnami/mariadb-cluster	1.0.2        	10.2.14    	DEPRECATED Chart to create a Highly available M...
bitnami/mariadb-galera 	7.1.8        	10.6.7     	MariaDB Galera is a multi-primary database clus...

3. 拉取 MySQL chart 到本地

$ mkdir /root/mysql && cd /root/mysql

# 拉取 chart 到本地 /root/mysql 目录
$ helm pull bitnami/mysql --version 4.5.2


$ tar -xvf mysql-4.5.2.tgz
$ cp mysql/values.yaml ./values-test.yaml

# 查看当前目录层级
$ tree -L 2
.
├── mysql
│   ├── Chart.yaml
│   ├── files
│   ├── README.md
│   ├── templates
│   ├── values-production.yaml
│   └── values.yaml
├── mysql-4.5.2.tgz
└── values-test.yaml

4. 对本地 values-test.yaml 修改

  • 查看集群 storageclasses
$ kubectl get storageclasses.storage.k8s.io 
NAME                   PROVISIONER           RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
openebs-device         openebs.io/local      Delete          WaitForFirstConsumer   false                  34d
openebs-hostpath       openebs.io/local      Delete          WaitForFirstConsumer   false                  34d
openebs-jiva-default   jiva.csi.openebs.io   Delete          Immediate              true                   33d
  • 修改配置
$ cat values-test.yaml 

## 配置文件中定义 storageClass: "",会使用集群配置的 openebs 提供的 storageClass,
## 使用此文档部署,需要自行解决 storageClass 问题 (ceph, nfs, 公有云提供的 nfs)

service:
  ## Kubernetes service type
  # nodePort:
  type: ClusterIP
  port: 3306


root:
  ## MySQL admin password
  ## ref: https://github.com/bitnami/bitnami-docker-mysql#setting-the-root-password-on-first-run
  ##
  password: root123 		# 设置 MySQL root密码


replication:
  ## Enable replication. This enables the creation of replicas of MySQL. If false, only a
  ## master deployment would be created
  enabled: true
  user: replicator
  password: replicator 		# 设置 MySQL 主从同步用户密码


master:
  persistence:
    storageClass: "openebs-jiva-default" 		# 设置 storageClass


slave:
  replicas: 2
  persistence:
    storageClass: "openebs-jiva-default" 		# 设置 storageClass

5. 对本地 templates 模板 修改

# k8s v1.19.3 版本中,statefulsets 版本为:apps/v1  而不是模板中 apps/v1beta1 的版本,
# 因此需要替换 master-statefulset.yaml 和 slave-statefulset.yaml 模板

$ sed -i "s#apps/v1beta1#apps/v1#" mysql/templates/master-statefulset.yaml

$ sed -i "s#apps/v1beta1#apps/v1#" mysql/templates/slave-statefulset.yaml
  • 未替换模板中 statefulsets 版本,直接安装服务会报错
$ helm install mysql-cluster mysql -f values-test.yaml 

Error: INSTALLATION FAILED: unable to build kubernetes objects from release manifest: unable to recognize "": no matches for kind "StatefulSet" in version "apps/v1beta1"

6. 安装 MySQL 集群

# 创建 test-middleware 名称空间
$ kubectl create ns test-middleware

# 安装 MySQL 集群
$ helm -n test-middleware install mysql-cluster mysql -f value-test.yaml

## helm -n NAMESAPCE install SERVER_NAME FILE_NAME -f CONFIG_FILE
-n 指定 kubernetes 集群名称空间
-f 指定使用的配置文件,文件中定义的配置可以覆盖 mysql/values.yaml 文件中配置


helm -n test-middleware install mysql-cluster mysql -f value-test.yaml 
NAME: mysql-cluster
LAST DEPLOYED: Mon May  9 01:54:38 2022
NAMESPACE: test-middleware
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Please be patient while the chart is being deployed

Tip:

  Watch the deployment status using the command: kubectl get pods -w --namespace test-middleware

Services:

  echo Master: mysql-cluster-mysql.test-middleware.svc.cluster.local:3306
  echo Slave:  mysql-cluster-mysql-slave.test-middleware.svc.cluster.local:3306

Administrator credentials:

  echo Username: root
  echo Password : $(kubectl get secret --namespace test-middleware mysql-cluster-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode)

To connect to your database:

  1. Run a pod that you can use as a client:

      kubectl run mysql-cluster-mysql-client --rm --tty -i --restart='Never' --image  docker.io/bitnami/mysql:5.7.26 --namespace test-middleware --command -- bash

  2. To connect to master service (read/write):

      mysql -h mysql-cluster-mysql.test-middleware.svc.cluster.local -uroot -p my_database

  3. To connect to slave service (read-only):

      mysql -h mysql-cluster-mysql-slave.test-middleware.svc.cluster.local -uroot -p my_database

To upgrade this helm chart:

  1. Obtain the password as described on the 'Administrator credentials' section and set the 'root.password' parameter as shown below:

      ROOT_PASSWORD=$(kubectl get secret --namespace test-middleware mysql-cluster-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode)
      helm upgrade mysql-cluster bitnami/mysql --set root.password=$ROOT_PASSWORD

7. 查看部署的 MySQL 集群

$ helm -n test-middleware list
NAME         	NAMESPACE      	REVISION	UPDATED                                	STATUS  	CHART      	APP VERSION
mysql-cluster	test-middleware	1       	2022-05-09 01:54:38.848559008 -0400 EDT	deployed	mysql-4.5.2	5.7.26

$ kubectl -n test-middleware get pods -l app=mysql
NAME                           READY   STATUS    RESTARTS   AGE
mysql-cluster-mysql-master-0   1/1     Running   0          16m
mysql-cluster-mysql-slave-0    1/1     Running   0          16m
mysql-cluster-mysql-slave-1    1/1     Running   0          14m


> mysql-cluster-mysql-master-0 为主,mysql-cluster-mysql-slave-0 和 mysql-cluster-mysql-slave-1 为从

> default名称空间如何访问此 MySQL 集群

> MySQL主节点:mysql-cluster-mysql.test-middleware
> MySQL从节点0:mysql-cluster-mysql-slave-0.mysql-cluster-mysql-slave.test-middleware
> MySQL从节点1:mysql-cluster-mysql-slave-1.mysql-cluster-mysql-slave.test-middleware
  • 查看服务使用的 storageclass
# 查看 pvc
$ kubectl -n test-middleware get pvc
NAME                                STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS           AGE
data-mysql-cluster-mysql-master-0   Bound    pvc-b9a1d1ca-44d3-4292-af45-e6f3b3063395   8Gi        RWO            openebs-jiva-default   31m
data-mysql-cluster-mysql-slave-0    Bound    pvc-0d234b12-26eb-4e07-9dc0-ef9f0230e9fa   8Gi        RWO            openebs-jiva-default   31m
data-mysql-cluster-mysql-slave-1    Bound    pvc-16531f4b-41ac-4a04-9d90-04b92aab7b49   8Gi        RWO            openebs-jiva-default   29m

# 查看 pv
$ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                                                 STORAGECLASS           REASON   AGE
pvc-b9a1d1ca-44d3-4292-af45-e6f3b3063395   8Gi        RWO            Delete           Bound    test-middleware/data-mysql-cluster-mysql-master-0                     openebs-jiva-default            33m
pvc-0d234b12-26eb-4e07-9dc0-ef9f0230e9fa   8Gi        RWO            Delete           Bound    test-middleware/data-mysql-cluster-mysql-slave-0                      openebs-jiva-default            33m
pvc-16531f4b-41ac-4a04-9d90-04b92aab7b49   8Gi        RWO            Delete           Bound    test-middleware/data-mysql-cluster-mysql-slave-1                      openebs-jiva-default            31m

8. 连接 MySQL 集群 验证服务

# 获取 MySQL 集群的密码
$ kubectl get secret --namespace test-middleware mysql-cluster-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode
root123

# 启动一个临时容器
$ kubectl run mysql-cluster-mysql-client --rm --tty -i --restart='Never' --image  docker.io/bitnami/mysql:5.7.26 --namespace test-middleware --command -- bash

## 登陆 MySQL Master节点
$ mysql -h mysql-cluster-mysql.test-middleware -uroot -p
Enter password: # root123

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

# 查看主从状态
# 查看File和Position的值,在从库配置中会显示。
> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000002
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified


## 登陆从库,查看主从同步状态
$ mysql -h mysql-cluster-mysql.test-middleware -uroot -p
Enter password:  # root123

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-cluster-mysql
                  Master_User: replicator
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000002  # File: mysql-bin.000002
          Read_Master_Log_Pos: 154  # Position: 154
               Relay_Log_File: mysql-relay-bin.000004
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 2236
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 641
                  Master_UUID: aa7a516b-cf5c-11ec-b974-a2ee403fe88f
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

参考文档

https://luckfu.com/post/helm_deploy_mysql_master_slave/

https://cloud.tencent.com/developer/article/1632915

https://github.com/bitnami/charts/issues/2892
posted @ 2022-05-09 14:43  evescn  阅读(5172)  评论(0编辑  收藏  举报