翼度科技»论坛 编程开发 mysql 查看内容

MySQL Router高可用搭建

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15

  • 安装简介
  • 高可用搭建
  • 高可用及负载均衡测试
  • 问题处理
一、安装简介

1.1 安装目的

MySQL官方提供了InnoDB Cluster,该集群由MySQL MGR和MySQL Router组成。MySQL MGR在数据库层面实现自主高可用性,而MySQL Router则负责代理访问。在部署完成后,MySQL Router将形成单点,如果出现故障,将会影响数据库集群的可用性。因此,为了提高数据库系统的可用性,需要搭建MySQL Router的高可用性方案。
1.2 MySQL router高可用组件介绍

本篇文章中的高可用方案,主要是通过Corosync和Pacemaker是两个开源软件项目实现,它们结合起来为高可用性集群提供了通信、同步、资源管理和故障转移等服务。
1.2.1 corosync

Corosync是一个开源的高可用性集群通信和同步服务,可以实现集群节点之间的通信和数据同步,同时提供了可靠的消息传递机制和成员管理功能,以确保在分布式环境下集群的稳定运行。 Corosync基于可靠的UDP多播协议进行通信,并提供了可插拔的协议栈接口,可以支持多种协议和网络环境。它还提供了一个API,可以让其他应用程序使用Corosync的通信和同步服务。
1.2.2 pacemaker

Pacemaker是一个开源的高可用性集群资源管理和故障转移工具,可以实现在集群节点之间自动管理资源(如虚拟IP、文件系统、数据库等),并在节点或资源故障时进行自动迁移,从而确保整个系统的高可用性和连续性。 Pacemaker支持多种资源管理策略,可以根据不同的需求进行配置。它还提供了一个灵活的插件框架,可以支持不同的集群环境和应用场景,比如虚拟化、云计算等。
将Corosync和Pacemaker结合起来,可以提供一个完整的高可用性集群解决方案。它通过Corosync实现集群节点之间的通信和同步,通过Pacemaker实现集群资源管理和故障转移,从而确保整个系统的高可用性和连续性。 它们结合起来为高可用性集群提供了可靠的通信、同步、资源管理和故障转移等服务,是构建可靠、高效的分布式系统的重要基础。
1.2.3 ldirectord

ldirectord是一个用于Linux系统的负载均衡工具,它可以管理多个服务器上的服务,并将客户端请求分发到这些服务器中的一个或多个上,以提高服务的可用性和性能。ldirectord通常是与Heartbeat或Keepalived等集群软件一起使用,以确保高可用性和负载均衡。 ldirectord主要用途包括:

  • 负载均衡:ldirectord可以基于不同的负载均衡算法进行请求分发,例如轮询、加权轮询、最少连接、源地址哈希等。它可以将客户端请求分发到多个后端服务器中的一个或多个上,从而实现负载均衡。
  • 健康检查:ldirectord可以定期检查后端服务器的可用性,并将不可用的服务器从服务池中排除,从而确保服务的高可用性和稳定性。
  • 会话保持:ldirectord可以根据客户端的IP地址、Cookie等标识,将客户端请求路由到相同的后端服务器上,从而实现会话保持,确保客户端与后端服务器之间的连接不会被中断。
  • 动态配置:ldirectord支持动态添加、删除、修改后端服务器和服务,管理员可以通过命令行或配置文件等方式进行操作,从而实现动态配置。
ldirectord是专门为LVS监控而编写的,用来监控lvs架构中服务器池(server pool) 的服务器状态。 ldirectord 运行在 IPVS 节点上, ldirectord作为一个守护进程启动后会对服务器池中的每个真实服务器发送请求进行监控,如果服务器没有响应 ldirectord 的请求,那么ldirectord 认为该服务器不可用, ldirectord 会运行 ipvsadm 对 IPVS表中该服务器进行删除,如果等下次再次检测有相应则通过ipvsadm 进行添加。
2、安装规划

MySQL及MySQL Router版本均为8.0.32
IP主机名安装组件使用端口172.17.140.25gdb1MySQL MySQL Router ipvsadm ldirectord pcs pacemaker corosyncMySQL:3309 MySQL Router:6446 MySQL Router:6447 pcs_tcp:13314 pcs_udp:13315172.17.140.24gdb2MySQL MySQL Router ipvsadm ldirectord pcs pacemaker corosyncMySQL:3309 MySQL Router:6446 MySQL Router:6447 pcs_tcp:13314 pcs_udp:13315172.17.139.164gdb3MySQL MySQL Router ipvsadm ldirectord pcs pacemaker corosyncMySQL:3309 MySQL Router:6446 MySQL Router:6447 pcs_tcp:13314 pcs_udp:13315172.17.129.1VIP6446、6447172.17.139.62MySQL client大概安装步骤如下

二、高可用搭建

2.1 基础环境设置(三台服务器都做)


  • 分别在三台服务器上根据规划设置主机名
  1. hostnamectl set-hostname gdb1
  2. hostnamectl set-hostname gdb2
  3. hostnamectl set-hostname gdb3
复制代码

  • 将下面内容追加保存在三台服务器的文件/etc/hosts中
  1. 172.17.140.25   gdb1
  2. 172.17.140.24   gdb2
  3. 172.17.139.164  gdb3
复制代码

  • 在三台服务器上禁用防火墙
  1. systemctl stop firewalld
  2. systemctl disable firewalld
复制代码

  • 在三台服务器上禁用selinux,如果selinux未关闭,修改配置文件后,需要重启服务器才会生效

如下输出表示完成关闭


  • 在三台服务器上分别执行下面命令,用户建立互相
建立互信,仅仅是为了服务器间传输文件方便,不是集群搭建的必要基础。
  1. ssh-keygen -t dsa
  2. ssh-copy-id gdb1
  3. ssh-copy-id gdb2
  4. ssh-copy-id gdb3
复制代码
执行情况如下
  1. [#19#root@gdb1 ~ 16:16:54]19 ssh-keygen -t dsa
  2. Generating public/private dsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_dsa):         ## 直接回车
  4. /root/.ssh/id_dsa already exists.
  5. Overwrite (y/n)? y                                               ## 如果原来有ssh配置文件,可以输入y覆盖
  6. Enter passphrase (empty for no passphrase):                      ## 直接回车
  7. Enter same passphrase again:                                     ## 直接回车
  8. Your identification has been saved in /root/.ssh/id_dsa.
  9. Your public key has been saved in /root/.ssh/id_dsa.pub.
  10. The key fingerprint is:
  11. SHA256:qwJXgfN13+N1U5qvn9fC8pyhA29iuXvQVhCupExzgTc root@gdb1
  12. The key's randomart image is:
  13. +---[DSA 1024]----+
  14. |     .   .. ..   |
  15. |    o . o Eo.   .|
  16. |     o ooooo.o o.|
  17. |      oo = .. *.o|
  18. |     .  S .. o +o|
  19. |  . .    .o o . .|
  20. |   o    .  * ....|
  21. |    .  .  + *o+o+|
  22. |     ..  .o*.+++o|
  23. +----[SHA256]-----+
  24. [#20#root@gdb1 ~ 16:17:08]20 ssh-copy-id gdb1
  25. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_dsa.pub"
  26. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  27. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  28. root@gdb1's password:                                            ## 输入gdb1服务器的root用户对应密码
  29. Number of key(s) added: 1
  30. Now try logging into the machine, with:   "ssh 'gdb1'"
  31. and check to make sure that only the key(s) you wanted were added.
  32. [#21#root@gdb1 ~ 16:17:22]21 ssh-copy-id gdb2
  33. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_dsa.pub"
  34. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  35. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  36. root@gdb2's password:                                             ## 输入gdb2服务器的root用户对应密码
  37. Number of key(s) added: 1
  38. Now try logging into the machine, with:   "ssh 'gdb2'"
  39. and check to make sure that only the key(s) you wanted were added.
  40. [#22#root@gdb1 ~ 16:17:41]22 ssh-copy-id gdb3
  41. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_dsa.pub"
  42. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  43. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  44. root@gdb3's password:                                             ## 输入gdb3服务器的root用户对应密码
  45. Number of key(s) added: 1
  46. Now try logging into the machine, with:   "ssh 'gdb3'"
  47. and check to make sure that only the key(s) you wanted were added.
  48. [#23#root@gdb1 ~ 16:17:44]23
复制代码
任意切换服务器,不需要输入密码,则说明互相建立成功
  1. [#24#root@gdb1 ~ 16:21:16]24 ssh gdb1
  2. Last login: Tue Feb 21 16:21:05 2023 from 172.17.140.25
  3. [#1#root@gdb1 ~ 16:21:19]1 logout
  4. Connection to gdb1 closed.
  5. [#25#root@gdb1 ~ 16:21:19]25 ssh gdb2
  6. Last login: Tue Feb 21 16:21:09 2023 from 172.17.140.25
  7. [#1#root@gdb2 ~ 16:21:21]1 logout
  8. Connection to gdb2 closed.
  9. [#26#root@gdb1 ~ 16:21:21]26 ssh gdb3
  10. Last login: Tue Feb 21 10:53:47 2023
  11. [#1#root@gdb3 ~ 16:21:22]1 logout
  12. Connection to gdb3 closed.
  13. [#27#root@gdb1 ~ 16:21:24]27
复制代码

  • 时钟同步,对于分布式、集中式集群,时钟同步都非常重要,时间不一致会引发各种异常情况
  1. yum -y install ntpdate    // 安装ntpdate客户端
  2. ntpdate npt1.aliyun.com   // 如果连通外网,可以指定阿里云ntp服务器,或者指定内网ntp server
  3. hwclock -w            // 更新BIOS时间
复制代码
2.2 通过MySQL Router搭建读写分离MGR集群

具体参考文章https://gitee.com/GreatSQL/GreatSQL-Doc/blob/master/deep-dive-mgr/deep-dive-mgr-07.md
2.3 在三台服务器上分别进行进行MySQL Router部署并启动,MySQL Router配置文件如下
  1. # File automatically generated during MySQL Router bootstrap
  2. [DEFAULT]
  3. name=system
  4. user=root
  5. keyring_path=/opt/software/mysql-router-8.0.32-linux-glibc2.17-x86_64-minimal/var/lib/mysqlrouter/keyring
  6. master_key_path=/opt/software/mysql-router-8.0.32-linux-glibc2.17-x86_64-minimal/mysqlrouter.key
  7. connect_timeout=5
  8. read_timeout=30
  9. dynamic_state=/opt/software/mysql-router-8.0.32-linux-glibc2.17-x86_64-minimal/bin/../var/lib/mysqlrouter/state.json
  10. client_ssl_cert=/opt/software/mysql-router-8.0.32-linux-glibc2.17-x86_64-minimal/var/lib/mysqlrouter/router-cert.pem
  11. client_ssl_key=/opt/software/mysql-router-8.0.32-linux-glibc2.17-x86_64-minimal/var/lib/mysqlrouter/router-key.pem
  12. client_ssl_mode=DISABLED
  13. server_ssl_mode=AS_CLIENT
  14. server_ssl_verify=DISABLED
  15. unknown_config_option=error
  16. [logger]
  17. level=INFO
  18. [metadata_cache:bootstrap]
  19. cluster_type=gr
  20. router_id=1
  21. user=mysql_router1_g9c62rk29lcn
  22. metadata_cluster=gdbCluster
  23. ttl=0.5
  24. auth_cache_ttl=-1
  25. auth_cache_refresh_interval=2
  26. use_gr_notifications=0
  27. [routing:bootstrap_rw]
  28. bind_address=0.0.0.0
  29. bind_port=6446
  30. destinations=metadata-cache://gdbCluster/?role=PRIMARY
  31. routing_strategy=first-available
  32. protocol=classic
  33. [routing:bootstrap_ro]
  34. bind_address=0.0.0.0
  35. bind_port=6447
  36. destinations=metadata-cache://gdbCluster/?role=SECONDARY
  37. routing_strategy=round-robin-with-fallback
  38. protocol=classic
  39. [routing:bootstrap_x_rw]
  40. bind_address=0.0.0.0
  41. bind_port=6448
  42. destinations=metadata-cache://gdbCluster/?role=PRIMARY
  43. routing_strategy=first-available
  44. protocol=x
  45. [routing:bootstrap_x_ro]
  46. bind_address=0.0.0.0
  47. bind_port=6449
  48. destinations=metadata-cache://gdbCluster/?role=SECONDARY
  49. routing_strategy=round-robin-with-fallback
  50. protocol=x
  51. [http_server]
  52. port=8443
  53. ssl=1
  54. ssl_cert=/opt/software/mysql-router-8.0.32-linux-glibc2.17-x86_64-minimal/var/lib/mysqlrouter/router-cert.pem
  55. ssl_key=/opt/software/mysql-router-8.0.32-linux-glibc2.17-x86_64-minimal/var/lib/mysqlrouter/router-key.pem
  56. [http_auth_realm:default_auth_realm]
  57. backend=default_auth_backend
  58. method=basic
  59. name=default_realm
  60. [rest_router]
  61. require_realm=default_auth_realm
  62. [rest_api]
  63. [http_auth_backend:default_auth_backend]
  64. backend=metadata_cache
  65. [rest_routing]
  66. require_realm=default_auth_realm
  67. [rest_metadata_cache]
  68. require_realm=default_auth_realm
复制代码
2.4 验证三台MySQL Router连接测试
  1. [#12#root@gdb2 ~ 14:12:45]12 mysql -uroot -pAbc1234567* -h172.17.140.25 -P6446 -N -e 'select now()' 2> /dev/null
  2. +---------------------+
  3. | 2023-03-17 14:12:46 |
  4. +---------------------+
  5. [#13#root@gdb2 ~ 14:12:46]13 mysql -uroot -pAbc1234567* -h172.17.140.25 -P6447 -N -e 'select now()' 2> /dev/null
  6. +---------------------+
  7. | 2023-03-17 14:12:49 |
  8. +---------------------+
  9. [#14#root@gdb2 ~ 14:12:49]14 mysql -uroot -pAbc1234567* -h172.17.140.24 -P6446 -N -e 'select now()' 2> /dev/null
  10. +---------------------+
  11. | 2023-03-17 14:12:52 |
  12. +---------------------+
  13. [#15#root@gdb2 ~ 14:12:52]15 mysql -uroot -pAbc1234567* -h172.17.140.24 -P6447 -N -e 'select now()' 2> /dev/null
  14. +---------------------+
  15. | 2023-03-17 14:12:55 |
  16. +---------------------+
  17. [#16#root@gdb2 ~ 14:12:55]16 mysql -uroot -pAbc1234567* -h172.17.139.164 -P6446 -N -e 'select now()' 2> /dev/null
  18. +---------------------+
  19. | 2023-03-17 14:12:58 |
  20. +---------------------+
  21. [#17#root@gdb2 ~ 14:12:58]17 mysql -uroot -pAbc1234567* -h172.17.139.164 -P6447 -N -e 'select now()' 2> /dev/null
  22. +---------------------+
  23. | 2023-03-17 14:13:01 |
  24. +---------------------+
  25. [#18#root@gdb2 ~ 14:13:01]18
复制代码
2.5 安装pacemaker


  • 安装pacemaker
安装pacemaker会依赖corosync这个包,所以直接安装pacemaker这一个包就可以了
  1. [#1#root@gdb1 ~ 10:05:55]1 yum -y install pacemaker
复制代码

  • 安装pcs管理工具
  1. [#1#root@gdb1 ~ 10:05:55]1 yum -y install pcs
复制代码

  • 创建集群认证操作系统用户,用户名为hacluster,密码设置为abc123
  1. [#13#root@gdb1 ~ 10:54:13]13 echo abc123 | passwd --stdin hacluster
  2. 更改用户 hacluster 的密码 。
  3. passwd:所有的身份验证令牌已经成功更新。
复制代码

  • 启动pcsd,并且设置开机自启动
  1. [#16#root@gdb1 ~ 10:55:30]16 systemctl enable pcsd
  2. Created symlink from /etc/systemd/system/multi-user.target.wants/pcsd.service to /usr/lib/systemd/system/pcsd.service.
  3. [#17#root@gdb1 ~ 10:56:03]17 systemctl start pcsd
  4. [#18#root@gdb1 ~ 10:56:08]18 systemctl status pcsd
  5. ● pcsd.service - PCS GUI and remote configuration interface
  6.    Loaded: loaded (/usr/lib/systemd/system/pcsd.service; enabled; vendor preset: disabled)
  7.    Active: active (running) since 三 2023-02-22 10:56:08 CST; 6s ago
  8.      Docs: man:pcsd(8)
  9.            man:pcs(8)
  10. Main PID: 27677 (pcsd)
  11.     Tasks: 4
  12.    Memory: 29.9M
  13.    CGroup: /system.slice/pcsd.service
  14.            └─27677 /usr/bin/ruby /usr/lib/pcsd/pcsd
  15. 2月 22 10:56:07 gdb1 systemd[1]: Starting PCS GUI and remote configuration interface...
  16. 2月 22 10:56:08 gdb1 systemd[1]: Started PCS GUI and remote configuration interface.
  17. [#19#root@gdb1 ~ 10:56:14]19
复制代码

  • 修改pcsd的TCP端口为指定的13314
  1. sed -i '/#PCSD_PORT=2224/a\
  2. PCSD_PORT=13314' /etc/sysconfig/pcsd
复制代码
重启pcsd服务,让新端口生效
  1. [#23#root@gdb1 ~ 11:23:20]23 systemctl restart pcsd
  2. [#24#root@gdb1 ~ 11:23:39]24 systemctl status pcsd
  3. ● pcsd.service - PCS GUI and remote configuration interface
  4.    Loaded: loaded (/usr/lib/systemd/system/pcsd.service; enabled; vendor preset: disabled)
  5.    Active: active (running) since 三 2023-02-22 11:23:39 CST; 5s ago
  6.      Docs: man:pcsd(8)
  7.            man:pcs(8)
  8. Main PID: 30041 (pcsd)
  9.     Tasks: 4
  10.    Memory: 27.3M
  11.    CGroup: /system.slice/pcsd.service
  12.            └─30041 /usr/bin/ruby /usr/lib/pcsd/pcsd
  13. 2月 22 11:23:38 gdb1 systemd[1]: Starting PCS GUI and remote configuration interface...
  14. 2月 22 11:23:39 gdb1 systemd[1]: Started PCS GUI and remote configuration interface.
  15. [#25#root@gdb1 ~ 11:23:45]25
复制代码

  • 设置集群认证信息,通过操作系统用户hacluster进行认证
  1. [#27#root@gdb1 ~ 11:31:43]27 cp /etc/corosync/corosync.conf.example /etc/corosync/corosync.conf
  2. [#28#root@gdb1 ~ 11:32:15]28 pcs cluster auth gdb1:13314 gdb2:13314 gdb3:13314 -u hacluster -p 'abc123'
  3. gdb1: Authorized
  4. gdb2: Authorized
  5. gdb3: Authorized
  6. [#29#root@gdb1 ~ 11:33:18]29
复制代码

  • 创建集群,任意节点执行即可
  1. ## 名称为gdb_ha , udp协议为13315, 掩码为24 ,集群成员为主机gdb1, gdb2, gdb3
  2. [#31#root@gdb1 ~ 11:41:48]31 pcs cluster setup --force --name gdb_ha --transport=udp --addr0 24 --mcastport0 13315 gdb1 gdb2 gdb3
  3. Destroying cluster on nodes: gdb1, gdb2, gdb3...
  4. gdb1: Stopping Cluster (pacemaker)...
  5. gdb2: Stopping Cluster (pacemaker)...
  6. gdb3: Stopping Cluster (pacemaker)...
  7. gdb2: Successfully destroyed cluster
  8. gdb1: Successfully destroyed cluster
  9. gdb3: Successfully destroyed cluster
  10. Sending 'pacemaker_remote authkey' to 'gdb1', 'gdb2', 'gdb3'
  11. gdb2: successful distribution of the file 'pacemaker_remote authkey'
  12. gdb3: successful distribution of the file 'pacemaker_remote authkey'
  13. gdb1: successful distribution of the file 'pacemaker_remote authkey'
  14. Sending cluster config files to the nodes...
  15. gdb1: Succeeded
  16. gdb2: Succeeded
  17. gdb3: Succeeded
  18. Synchronizing pcsd certificates on nodes gdb1, gdb2, gdb3...
  19. gdb1: Success
  20. gdb2: Success
  21. gdb3: Success
  22. Restarting pcsd on the nodes in order to reload the certificates...
  23. gdb1: Success
  24. gdb2: Success
  25. gdb3: Success
复制代码

  • 确认完整的集群配置,在任意节点查看即可
  1. [#21#root@gdb2 ~ 11:33:18]21 more /etc/corosync/corosync.conf
  2. totem {
  3.     version: 2
  4.     cluster_name: gdb_ha
  5.     secauth: off
  6.     transport: udp
  7.     rrp_mode: passive
  8.     interface {
  9.         ringnumber: 0
  10.         bindnetaddr: 24
  11.         mcastaddr: 239.255.1.1
  12.         mcastport: 13315
  13.     }
  14. }
  15. nodelist {
  16.     node {
  17.         ring0_addr: gdb1
  18.         nodeid: 1
  19.     }
  20.     node {
  21.         ring0_addr: gdb2
  22.         nodeid: 2
  23.     }
  24.     node {
  25.         ring0_addr: gdb3
  26.         nodeid: 3
  27.     }
  28. }
  29. quorum {
  30.     provider: corosync_votequorum
  31. }
  32. logging {
  33.     to_logfile: yes
  34.     logfile: /var/log/cluster/corosync.log
  35.     to_syslog: yes
  36. }
  37. [#22#root@gdb2 ~ 14:23:50]22
复制代码

  • 启动所有集群节点的pacemaker 相关服务,任意节点执行即可
  1. [#35#root@gdb1 ~ 15:30:51]35 pcs cluster start --all
  2. gdb1: Starting Cluster (corosync)...
  3. gdb2: Starting Cluster (corosync)...
  4. gdb3: Starting Cluster (corosync)...
  5. gdb3: Starting Cluster (pacemaker)...
  6. gdb1: Starting Cluster (pacemaker)...
  7. gdb2: Starting Cluster (pacemaker)...
复制代码
关闭服务时,使用pcs cluster stop --all,或者用pcs cluster stop 《server》关闭某一台

  • 在每个节点上设置pacemaker相关服务开机自启动
  1. [#35#root@gdb1 ~ 15:30:51]35 systemctl enable pcsd corosync pacemaker
  2. [#36#root@gdb1 ~ 15:30:53]36 pcs cluster enable --all
复制代码

  • 没有STONITH 设备时,禁用STONITH 组件功能
禁用STONITH 组件功能后,分布式锁管理器DLM等资源以及依赖DLM的所有服务:例如cLVM2,GFS2,OCFS2等都将无法启动,不禁用时会有错误信息
  1. pcs property set stonith-enabled=false
复制代码
完整的命令执行过程如下
  1. [#32#root@gdb1 ~ 15:48:20]32 systemctl status pacemaker
  2. ● pacemaker.service - Pacemaker High Availability Cluster Manager
  3.    Loaded: loaded (/usr/lib/systemd/system/pacemaker.service; disabled; vendor preset: disabled)
  4.    Active: active (running) since 三 2023-02-22 15:35:48 CST; 1min 54s ago
  5.      Docs: man:pacemakerd
  6.            https://clusterlabs.org/pacemaker/doc/en-US/Pacemaker/1.1/html-single/Pacemaker_Explained/index.html
  7. Main PID: 25661 (pacemakerd)
  8.     Tasks: 7
  9.    Memory: 51.1M
  10.    CGroup: /system.slice/pacemaker.service
  11.            ├─25661 /usr/sbin/pacemakerd -f
  12.            ├─25662 /usr/libexec/pacemaker/cib
  13.            ├─25663 /usr/libexec/pacemaker/stonithd
  14.            ├─25664 /usr/libexec/pacemaker/lrmd
  15.            ├─25665 /usr/libexec/pacemaker/attrd
  16.            ├─25666 /usr/libexec/pacemaker/pengine
  17.            └─25667 /usr/libexec/pacemaker/crmd
  18. 2月 22 15:35:52 gdb1 crmd[25667]:   notice: Fencer successfully connected
  19. 2月 22 15:36:11 gdb1 crmd[25667]:   notice: State transition S_ELECTION -> S_INTEGRATION
  20. 2月 22 15:36:12 gdb1 pengine[25666]:    error: Resource start-up disabled since no STONITH resources have been defined
  21. 2月 22 15:36:12 gdb1 pengine[25666]:    error: Either configure some or disable STONITH with the stonith-enabled option
  22. 2月 22 15:36:12 gdb1 pengine[25666]:    error: NOTE: Clusters with shared data need STONITH to ensure data integrity
  23. 2月 22 15:36:12 gdb1 pengine[25666]:   notice: Delaying fencing operations until there are resources to manage
  24. 2月 22 15:36:12 gdb1 pengine[25666]:   notice: Calculated transition 0, saving inputs in /var/lib/pacemaker/pengine/pe-input-0.bz2
  25. 2月 22 15:36:12 gdb1 pengine[25666]:   notice: Configuration ERRORs found during PE processing.  Please run "crm_verify -L" to identify issues.
  26. 2月 22 15:36:12 gdb1 crmd[25667]:   notice: Transition 0 (Complete=0, Pending=0, Fired=0, Skipped=0, Incomplete=0, Source=/var/lib/pacemaker/pengine/pe-input-0.bz2): Complete
  27. 2月 22 15:36:12 gdb1 crmd[25667]:   notice: State transition S_TRANSITION_ENGINE -> S_IDLE
  28. [#33#root@gdb1 ~ 15:37:43]33 pcs property set stonith-enabled=false
  29. [#34#root@gdb1 ~ 15:48:20]34 systemctl status pacemaker
  30. ● pacemaker.service - Pacemaker High Availability Cluster Manager
  31.    Loaded: loaded (/usr/lib/systemd/system/pacemaker.service; disabled; vendor preset: disabled)
  32.    Active: active (running) since 三 2023-02-22 15:35:48 CST; 12min ago
  33.      Docs: man:pacemakerd
  34.            https://clusterlabs.org/pacemaker/doc/en-US/Pacemaker/1.1/html-single/Pacemaker_Explained/index.html
  35. Main PID: 25661 (pacemakerd)
  36.     Tasks: 7
  37.    Memory: 51.7M
  38.    CGroup: /system.slice/pacemaker.service
  39.            ├─25661 /usr/sbin/pacemakerd -f
  40.            ├─25662 /usr/libexec/pacemaker/cib
  41.            ├─25663 /usr/libexec/pacemaker/stonithd
  42.            ├─25664 /usr/libexec/pacemaker/lrmd
  43.            ├─25665 /usr/libexec/pacemaker/attrd
  44.            ├─25666 /usr/libexec/pacemaker/pengine
  45.            └─25667 /usr/libexec/pacemaker/crmd
  46. 2月 22 15:36:12 gdb1 pengine[25666]:   notice: Calculated transition 0, saving inputs in /var/lib/pacemaker/pengine/pe-input-0.bz2
  47. 2月 22 15:36:12 gdb1 pengine[25666]:   notice: Configuration ERRORs found during PE processing.  Please run "crm_verify -L" to identify issues.
  48. 2月 22 15:36:12 gdb1 crmd[25667]:   notice: Transition 0 (Complete=0, Pending=0, Fired=0, Skipped=0, Incomplete=0, Source=/var/lib/pacemaker/pengine/pe-input-0.bz2): Complete
  49. 2月 22 15:36:12 gdb1 crmd[25667]:   notice: State transition S_TRANSITION_ENGINE -> S_IDLE
  50. 2月 22 15:48:20 gdb1 crmd[25667]:   notice: State transition S_IDLE -> S_POLICY_ENGINE
  51. 2月 22 15:48:21 gdb1 pengine[25666]:  warning: Blind faith: not fencing unseen nodes
  52. 2月 22 15:48:21 gdb1 pengine[25666]:   notice: Delaying fencing operations until there are resources to manage
  53. 2月 22 15:48:21 gdb1 pengine[25666]:   notice: Calculated transition 1, saving inputs in /var/lib/pacemaker/pengine/pe-input-1.bz2
  54. 2月 22 15:48:21 gdb1 crmd[25667]:   notice: Transition 1 (Complete=0, Pending=0, Fired=0, Skipped=0, Incomplete=0, Source=/var/lib/pacemaker/pengine/pe-input-1.bz2): Complete
  55. 2月 22 15:48:21 gdb1 crmd[25667]:   notice: State transition S_TRANSITION_ENGINE -> S_IDLE
  56. [#35#root@gdb1 ~ 15:48:31]35
复制代码

  • 验证pcs集群状态正常,无异常信息输出
  1. [#35#root@gdb1 ~ 15:48:31]35 crm_verify -L
  2. [#36#root@gdb1 ~ 17:33:31]36
复制代码
2.6 安装ldirectord(三台都做)


  • ldirectord下载
下载地址 https://rpm.pbone.net/info_idpl_23860919_distro_centos_6_com_ldirectord-3.9.5- 3.1.x86_64.rpm.html
![图片](../图片/Typora_img/MySQL Router高可用搭建 _img/640-16849369139302.png)新标签打开获取到地址后,可以用迅雷下载

  • 下载依赖包ipvsadm
  1. [#10#root@gdb1 ~ 19:51:20]10 wget http://mirror.centos.org/altarch/7/os/aarch64/Packages/ipvsadm-1.27-8.el7.aarch64.rpm
复制代码

  • 执行安装,如果安装过程中,还需要其他依赖,需要自行处理
  1. [#11#root@gdb1 ~ 19:51:29]11 yum -y install ldirectord-3.9.5-3.1.x86_64.rpm ipvsadm-1.27-8.el7.aarch64.rpm
复制代码

  • 创建配置文件/etc/ha.d/ldirectord.cf,编写内容如下
  1. checktimeout=3
  2. checkinterval=1
  3. autoreload=yes
  4. logfile="/var/log/ldirectord.log"
  5. quiescent=no
  6. virtual=172.17.129.1:6446
  7.         real=172.17.140.25:6446 gate
  8.         real=172.17.140.24:6446 gate
  9.         real=172.17.139.164:6446 gate
  10.         scheduler=rr
  11.         service=mysql
  12.         protocol=tcp
  13.         checkport=6446
  14.         checktype=connect
  15.         login="root"
  16.         passwd="Abc1234567*"
  17.         database="information_schema"
  18.         request="SELECT 1"
  19. virtual=172.17.129.1:6447
  20.         real=172.17.140.25:6447 gate
  21.         real=172.17.140.24:6447 gate
  22.         real=172.17.139.164:6447 gate
  23.         scheduler=rr
  24.         service=mysql
  25.         protocol=tcp
  26.         checkport=6447
  27.         checktype=connect
  28.         login="root"
  29.         passwd="Abc1234567*"
  30.         database="information_schema"
  31.         request="SELECT 1"
复制代码
参数说明

  • checktimeout=3 :后端服务器健康检查等待时间
  • checkinterval=5 :两次检查间隔时间
  • autoreload=yes :自动添加或者移除真实服务器
  • logfile="/var/log/ldirectord.log" :日志文件全路径
  • quiescent=no :故障时移除服务器的时候中断所有连接
  • virtual=172.17.129.1:6446 :VIP
  • real=172.17.140.25:6446 gate :真实服务器
  • scheduler=rr :指定调度算法:rr为轮询,wrr为带权重的轮询
  • service=mysql :健康检测真实服务器时ldirectord使用的服务
  • protocol=tcp :服务协议
  • checktype=connect :ldirectord守护进程使用什么方法监视真实服务器
  • checkport=16310 :健康检测使用的端口
  • login="root" :健康检测使用的用户名
  • passwd="a123456" :健康检测使用的密码
  • database="information_schema" :健康检测访问的默认database
  • request="SELECT1" :健康检测执行的检测命令
将编写好的配置文件,分发到另外两个服务器
  1. [#22#root@gdb1 ~ 20:51:57]22 cd /etc/ha.d/
  2. [#23#root@gdb1 /etc/ha.d 20:52:17]23 scp ldirectord.cf gdb2:`pwd`
  3. ldirectord.cf                                                                                                                                                100% 1300     1.1MB/s   00:00   
  4. [#24#root@gdb1 /etc/ha.d 20:52:26]24 scp ldirectord.cf gdb3:`pwd`
  5. ldirectord.cf                                                                                                                                                100% 1300     1.4MB/s   00:00   
  6. [#25#root@gdb1 /etc/ha.d 20:52:29]25
复制代码
2.7 配置回环网卡上配置VIP(三台都做)

此操作用于pcs内部负载均衡,在lo网卡上配置VIP用于pcs cluster内部通信,如果不操作,则无法进行负载均衡,脚本内容如下vip.sh,放在mysql_bin目录即可
  1. #!/bin/bash
  2. . /etc/init.d/functions
  3. SNS_VIP=172.16.50.161
  4.   
  5. case "$1" in
  6. start)
  7.       ifconfig lo:0 $SNS_VIP netmask 255.255.240.0 broadcast $SNS_VIP
  8. #      /sbin/route add -host $SNS_VIP dev lo:0
  9.       echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  10.       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
  11.       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
  12.       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
  13.       sysctl -p >/dev/null 2>&1
  14.       echo "RealServer Start OK"
  15.       ;;
  16. stop)
  17.      ifconfig lo:0 down
  18. #      route del $SNS_VIP >/dev/null 2>&1
  19.       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  20.       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
  21.       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
  22.       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
  23.       echo "RealServer Stoped"
  24.       ;;
  25. *)
  26.       echo "Usage: $0 {start|stop}"
  27.       exit 1
  28. esac
  29. exit 0
复制代码
启动配置
  1. # sh vip.sh start
复制代码
停止配置
  1. # sh vip.sh stop
复制代码
2.8 集群资源添加(任意节点执行即可)


  • pcs中添加vip资源
  1. [#6#root@gdb1 ~ 11:27:30]6 pcs resource create vip --disabled ocf:heartbeat:IPaddr nic=eth0 ip=172.17.129.1 cidr_netmask=24 broadcast=172.17.143.255 op monitor interval=5s timeout=20s
复制代码
命令解析

  • pcs resource create:pcs创建资源对象的起始命令
  • vip: 虚拟IP(VIP)资源对象的名称,可以根据需要自定义
  • --disable: 表示在创建资源对象时将其禁用。这是为了避免资源在尚未完全配置的情况下被Pacemaker集群所使用
  • ocf:heartbeat:IPaddr:告诉Pacemaker使用Heartbeat插件(即ocf:heartbeat)中的IPaddr插件来管理这个VIP资源
  • nic=eth0:这个选项指定了网络接口的名称,即将VIP绑定到哪个网卡上
  • ip=172.17.129.1:指定了要分配给VIP的IP地址
  • cidr_netmask=24:指定了VIP的子网掩码。在这个例子中,CIDR格式的子网掩码为24,相当于255.255.255.0
  • broadcast=172.17.143.255:指定了广播地址
  • op monitor interval=5s timeout=20s:定义了用于监视这个VIP资源的操作。interval=5s表示Pacemaker将每5秒检查一次资源的状态,timeout=20s表示Pacemaker将在20秒内等待资源的响应。如果在这20秒内资源没有响应,Pacemaker将视为资源不可用。

  • pcs中添加lvs资源
  1. [#7#root@gdb1 ~ 11:34:50]7 pcs resource create lvs --disabled ocf:heartbeat:ldirectord op monitor interval=10s timeout=10s
复制代码
命令解析

  • pcs resource create:pcs创建资源对象的起始命令
  • lvs: 虚拟IP(VIP)资源对象的名称,可以根据需要自定义
  • --disable: 表示在创建资源对象时将其禁用。这是为了避免资源在尚未完全配置的情况下被Pacemaker集群所使用
  • ocf:heartbeat:ldirectord:告诉Pacemaker使用Heartbeat插件(即ocf:heartbeat)中的ldirectord插件来管理LVS的负载均衡器,使用的配置文件为上面配置的/etc/ha.d/ldirectord.cf
  • op monitor interval=10s timeout=10s:定义了用于监视这个LVS资源的操作。interval=10s表示Pacemaker将每10秒检查一次资源的状态,timeout=10s表示Pacemaker将在10秒内等待资源的响应。如果在这10秒内资源没有响应,Pacemaker将视为资源不可用。

  • 创建完成后检测resource状态
  1. [#9#root@gdb1 ~ 11:35:42]9 pcs resource show
  2. vip        (ocf::heartbeat:IPaddr):        Stopped (disabled)
  3. lvs        (ocf::heartbeat:ldirectord):        Stopped (disabled)
  4. [#10#root@gdb1 ~ 11:35:48]10
复制代码

  • 创建resource group,并添加resource
  1. [#10#root@gdb1 ~ 11:37:36]10 pcs resource group add dbservice vip
  2. [#11#root@gdb1 ~ 11:37:40]11 pcs resource group add dbservice lvs
  3. [#12#root@gdb1 ~ 11:37:44]12
复制代码
2.9 集群启停

集群启动


  • 启动resource
  1. # pcs resource enable vip lvs 或者 pcs resource enable dbservice
复制代码
如果之前有异常,可以通过下面的命令清理异常信息,然后再启动
  1. # pcs resource cleanup vip
  2. # pcs resource cleanup lvs
复制代码

  • 启动状态确认,执行命令 pcs status
  1. [#54#root@gdb1 /etc/ha.d 15:54:22]54 pcs status
  2. Cluster name: gdb_ha
  3. Stack: corosync
  4. Current DC: gdb1 (version 1.1.23-1.el7_9.1-9acf116022) - partition with quorum
  5. Last updated: Thu Feb 23 15:55:27 2023
  6. Last change: Thu Feb 23 15:53:55 2023 by hacluster via crmd on gdb82
  7. 3 nodes configured
  8. 2 resource instances configured
  9. Online: [ gdb1 gdb2 gdb3 ]
  10. Full list of resources:
  11. Resource Group: dbservice
  12.      lvs        (ocf::heartbeat:ldirectord):        Started gdb2
  13.      vip        (ocf::heartbeat:IPaddr):            Started gdb3
  14. Daemon Status:
  15.   corosync: active/enabled
  16.   pacemaker: active/enabled
  17.   pcsd: active/enabled
  18. [#55#root@gdb1 /etc/ha.d 15:55:27]55
复制代码
输出结果说明
Cluster name: gdb_ha: 集群的名称为 gdb_ha。
Stack: corosync:该集群使用的通信协议栈为 corosync。
`Current DC: gdb3 (version 1.1.23-1.el7_9.1-9acf116022) - partition with quorum ``:当前的集群控制器(DC)为 gdb3,其版本为 1.1.23-1.el7_9.1-9acf116022,并且该节点所在的分区具有投票权。
Last updated: Thu Feb 23 15:55:27 2023:最后一次更新集群状态信息的时间为 2023 年 2 月 23 日 15:55:27。
Last change: Thu Feb 23 15:53:55 2023 by hacluster via crmd on gdb2 :最后一次更改集群配置的时间为 2023 年 2 月 23 日 15:53:55,由用户 hacluster 通过 crmd 在节点 gdb2 上执行。
3 nodes configured:该集群配置了 3 个节点。
2 resource instances configured:该集群中配置了 2 个资源实例。
Online: [ gdb1 gdb2 gdb3 ]:当前在线的节点为 gdb1、gdb2 和 gdb3。

Full list of resources:列出了该集群中所有的资源,包括资源名称、资源类型和所在节点,以及资源的启动状态和当前状态。其中,dbservice 是资源组名称,lvs 是类型为 ocf:
来源:https://www.cnblogs.com/greatsql/p/17457585.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具