1. MyCat high-availability architecture
This is mainly based on haproxy and keepalive to achieve the high availability of Mycat, so the database distribution behind Mycat is not drawn. Among them, keepalived is mainly used to ensure the high availability of haproxy, mainly used to monitor whether haproxy is available, and then failover; and haproxy is mainly used to achieve load balancing.
2. Server planning
server | haproxy | keepalive | mycat | mysql |
---|---|---|---|---|
192.168.1.8 | deploy | |||
192.168.1.9 | deploy | deploy | deploy | deploy |
192.168.1.10 | deploy | deploy | deploy |
3. Deploy mysql, mycat
We have deployed mycat on 192.168.1.10 and mysql on 192.168.1.8/9. Here we need to deploy another mycat on 192.168.1.9. Here we copy directly from 192.168.1.10 to 192.168.1.9, the command is as follows:
scp -r mycat/ 192.168.1.9:$PWD
Then, start mycat.
3. Deploy haproxy
HAProxy is responsible for distributing requests to MyCat, which plays a role of load balancing. At the same time, HAProxy can also detect whether MyCat is alive, and HAProxy will only forward the request to the surviving MyCat. If a MyCat server goes down, HAPorxy will not forward the request to the down MyCat, so MyCat is still available.
The following operations need to be performed on the two servers 192.168.1.9/10.
3.1, view haproxy and install
#查看
yum search haproxy
#安装
yum install haproxy.x86_64
3.2, modify haproxy configuration file
Install through the above method, the default configuration file is /etc/haproxy/haproxy.cfg.
defaults
mode tcp #修改成TCP协议
log global
option tcplog #修改日志
option dontlognull
#option http-server-close #注释掉,主要用于HTTP协议
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
#timeout http-request 10s #注释掉,主要用于HTTP协议
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
#timeout http-keep-alive 10s #注释掉,主要用于HTTP协议
timeout check 10s
maxconn 3000
#省略了其他的配置
#增加了负载均衡的mycat配置
backend app
balance roundrobin
server app1 192.168.1.9:8066 check
server app2 192.168.1.10:8066 check
3.3, start
Start it with the following command, and then connect through Navicat using haproxy (default port 5000).
haproxy -f /etc/haproxy/haproxy.cfg
4. Deploy keepalived
The following operations to deploy keepalived need to be performed on the two servers 192.168.1.9/10.
4.1, view and install
#查看
yum search keepalived
#安装
yum install keepalived.x86_64
4.2, modify the keepalived configuration file
Install through the above method, the default configuration file is /etc/keepalived/keepalived.cfg. Modify the configuration file as follows:
! Configuration File for keepalived
global_defs {
# notification_email {
# acassen@firewall.loc
# failover@firewall.loc
# sysadmin@firewall.loc
# }
# notification_email_from Alexandre.Cassen@firewall.loc
# smtp_server 192.168.200.1
# smtp_connect_timeout 30
router_id node10
# vrrp_skip_check_adv_addr
# vrrp_strict
# vrrp_garp_interval 0
# vrrp_gna_interval 0
}
##keepalived会定时执行脚本并对脚本执行的结果进行分析,动态调整vrrp_instance的优先级。
## 如果脚本执行结果为0,并且weight配置的值大于0,则优先级相应的增加。
## 如果脚本执行结果非0,并且weight配置的值小于0,则优先级相应的减少。
## 其他情况,维持原本配置的优先级,即配置文件中priority对应的值。
vrrp_script chk_haproxy {
script "killall -0 haproxy" ## 检测haproxy状态的脚本路径,该命令需要安装psmisc
interval 2 ## 检测时间间隔
weight 2 ## 如果条件成立,权重+2
}
## 定义虚拟路由,VI_1 为虚拟路由的标示符,自己定义名称
vrrp_instance VI_1 {
state BACKUP ##这一项主从都用BACKUP,MASTER会出现问题,会出现抢占的问题,如果主节点宕机掉,然后切换到备节点,如果主节点起来的话主节点就会把VIP抢占。这样就由优先级(priority)来控制同时启动情况下的默认主备,否则先启动的为主设备
interface ens33 ## 绑定虚拟IP的网络接口,与本机IP地址所在的网络接口相同
virtual_router_id 51 ## 虚拟路由的ID号,两个节点设置必须一样,因为属于同一个组,不同组的id一定不要一样,否则冲突
priority 100 ## 节点优先级,值范围0-254,MASTER要比BACKUP高,万一同时启动呢,一定要有一个高低之分
advert_int 1 ## 组播信息发送间隔,就是两个虚拟路由节点之间会通过组播检测对方是否正常,如果一个有问题,另一个会抢占vip,这里间隔1秒,时间太长可能会出现系统中断
## 设置验证信息,两个节点必须一致
authentication {
auth_type PASS
auth_pass 1111
}
## 将track_script块加入instance 配置块
track_script {
chk_haproxy ## 检查HAProxy服务是否存活
}
## 虚拟IP池, 两个节点设置必须一样
virtual_ipaddress {
192.168.1.13 ## 虚拟ip,可以定义多个,每行一个
}
}
The configuration content refers to "High-availability Mysql architecture_Haproxy+keepalived+mycat cluster configuration" .
4.3, install the killall command
In the above script, "killall -0 haproxy" is used to determine whether haproxy is started normally. This command needs to install psmisc. The command is as follows:
yum -y install psmisc
4.4, start keepalived
Complete the above configuration, and then start keepalived. The command is as follows:
keepalived -f /etc/keepalived/keepalived.conf
4.5, test
The haproxy and keepalived services on the two servers 192.168.1.9/10 are started. We check the addresses of the two servers and find that the VIP is on 192.168.1.9, as shown below:
In order to verify the high availability of haproxy, we killed the haproxy process on 192.168.1.9, and then checked the address, and found that the VIP had been switched to the 192.168.1.10 server. At this time, VIP is used to access the database, and it is found that it is still accessible, indicating that the HA configuration of haproxy is successful.