赞
踩
在云原生应用开发中,数据库的高可用性和数据一致性是至关重要的。MySQL作为一种常用的关系型数据库,在云原生环境中的部署和管理也变得愈发重要。本教程将介绍如何使用Helm一键部署MySQL 8.0的主从复制架构,以实现数据库的高可用和数据备份。
在先前的文中,我们详细介绍了如何利用Helm管理Kubernetes(K8S)集群,相信这已经使大家对Helm的基本使用有了初步的了解。
在开始之前,确保你已经安装了以下工具:
首先,添加MySQL的Helm仓库,执行如下命令:
helm repo add bitnami https://charts.bitnami.com/bitnami
然后,执行下面的命令更新本地仓库索引:
helm repo update
通过下面的命令,在仓库搜索MySQL的版本。
controlplane $ helm search repo mysql
NAME CHART VERSION APP VERSION DESCRIPTION
bitnami/mysql 10.1.1 8.0.36 MySQL is a fast, reliable, scalable, and easy t...
bitnami/phpmyadmin 16.0.1 5.2.1 phpMyAdmin is a free software tool written in P...
bitnami/mariadb 18.0.1 11.3.2 MariaDB is an open source, community-developed ...
bitnami/mariadb-galera 13.0.0 11.3.2 MariaDB Galera is a multi-primary database clus...
MySQL Helm Chart提供了简单的配置选项来设置主从复制。你可以通过以下方式配置:
helm install mysql-cluster \
--set auth.rootPassword='mysql' \
--set global.storageClass=local-path \
--set architecture=replication \
--set auth.password='mysql' \
--set secondary.replicaCount=1 \
--set auth.replicationPassword='replpass' \
-n mysql --create-namespace \
bitnami/mysql
执行上述命令后,会返回一下结果,如下图:
等待几分钟之后,执行如下命令即可查看部署的情况:
controlplane $ k get pod -n mysql -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-cluster-primary-0 1/1 Running 0 5m45s 192.168.1.7 node01 <none> <none>
mysql-cluster-secondary-0 1/1 Running 0 5m45s 192.168.0.5 controlplane <none> <none>
apt install mysql-client -y
# 登录主容器
mysql -uroot -h192.168.1.7 -pmysql
# 查看主从状态
# 查看File和Position的值,在从库配置中会显示。
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 157
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
# 登录从容器 mysql -uroot -h192.168.0.5 -pmysql # 查看Slave_IO_Running和Slave_SQL_Running的值是否为Yes. mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for source to send event Master_Host: mysql-cluster-primary Master_User: replicator Master_Port: 3306 Connect_Retry: 10 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 157 Relay_Log_File: mysql-relay-bin.000006 Relay_Log_Pos: 373 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes
"resources" sections in the chart not set
,如下:WARNING: There are "resources" sections in the chart not set. Using "resourcesPreset" is not recommended for production. For production installations, please set the following values according to your workload needs:
- primary.resources
- secondary.resources
通过上述提示信息是由于resources
的资源没有设置。
通过下面的命令把MySQL包下载到本地。
controlplane $ helm pull bitnami/mysql --version=10.1.1
controlplane $ ls
filesystem get_helm.sh mysql-10.1.1.tgz snap
controlplane $
执行下面的命令解压下载好的MySQL包:
controlplane $ tar zxvf mysql-10.1.1.tgz
mysql/Chart.yaml
mysql/Chart.lock
mysql/values.yaml
...省略...
修改mysql/values.yaml
中的resources
,如下:
resources:
requests:
cpu: 2
memory: 1024Mi
limits:
cpu: 2
memory: 1024Mi
修改完之后保存退出,执行如下命令重新部署。
helm install mysql-cluster \
--set auth.rootPassword='mysql' \
--set global.storageClass=local-path \
--set architecture=replication \
--set auth.password='mysql' \
--set secondary.replicaCount=1 \
--set auth.replicationPassword='replpass' \
--set useBundledSystemChart=true \
-f /root/mysql/values.yaml \
-n mysql --create-namespace \
bitnami/mysql
执行完上述的命令后,刚才那个警告就消失了。如下图:
通过本教程,你学习了如何使用Helm一键部署MySQL 8.0的主从复制架构。这为你提供了一个简单而强大的方法来部署可扩展的、高可用的MySQL数据库集群,并确保了数据的备份和一致性。继续探索更多Helm Chart的配置选项,以满足你的特定需求,并构建稳健的云原生应用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。