一、简介
在实际项目中,Mycat 服务也需要考虑高可用性,如果 Mycat 所在服务器出现宕机,或 Mycat 服务故障,需要有备机提供服务,需要考虑 Mycat 集群。
二、高可用方案
我们可以使用 HAProxy + Keepalived 配合两台 Mycat 搭起 Mycat 集群,实现高可用性。HAProxy实现了 MyCat 多节点的集群高可用和负载均衡,而 HAProxy 自身的高可用则可以通过 Keepalived 来实现。
例如:
编号 角色 IP地址 1 Mycat1 192.168.56.20 2 Mycat2 192.168.56.21 3 HAProxy(Master) 192.168.56.22 4 Keepalived(Master) 192.168.56.22 5 HAProxy(backup) 192.168.56.23 6 Keepalived(backup) 192.168.56.23
集群部署图理解:
1、keepalived和haproxy必须装在同一台机器上(如192.168.46.161机器上,keepalived和haproxy都要安装),keepalived负责为该服务器抢占vip(虚拟ip),抢占到vip后,对该主机的访问可以通过原来的ip(192.168.46.161)访问,也可以直接通过vip(192.168.46.180)访问。
2、192.168.46.162上的keepalived也会去抢占vip,抢占vip时有优先级,配置keepalived.conf中的(priority 150 #数值愈大,优先级越高,192.168.46.162上改为120,master和slave上该值配置不同)决定。但是一般哪台主机上的keepalived服务先启动就会抢占到vip,即使是slave,只要先启动也能抢到。
3、haproxy负责将对vip的请求分发到mycat上。起到负载均衡的作用,同时haproxy也能检测到mycat是否存活,haproxy只会将请求转发到存活的mycat上。
4、如果一台服务器(keepalived+haproxy服务器)宕机,另外一台上的keepalived会立刻抢占vip并接管服务。如果一台mycat服务器宕机,haporxy转发时不会转发到宕机的mycat上,所以mycat依然可用。
三、安装配置 HAProxy,使用root权限
1、准备好HAProxy安装包,传到/home/dhapp/software/目录下
2、解压到/usr/local/src
tar -zxvf haproxy-1.5.18.tar.gz -C /usr/local/src
3、进入解压后的目录,查看内核版本,进行编译
cd /usr/local/src/haproxy-1.5.18
uname -r
make TARGET=linux310 PREFIX=/usr/local/haproxy ARCH=x86_64
注意:
ARGET=linux310:
内核版本,使用uname -r查看内核,如:3.10.0-514.el7,此时参数就是linux310;ARCH=x86_64:系统位数;
PREFIX=/usr/local/haprpxy :为haprpxy安装路径。
4、编译完成后,进行安装
make install PREFIX=/usr/local/haproxy
5、安装完成后,创建目录、创建HAProxy配置文件
mkdir -p /usr/data/haproxy/
vim /usr/local/haproxy/haproxy.conf
内容如下:
global log 127.0.0.1 local0 #log 127.0.0.1 local1 notice #log loghost local0 info maxconn 4096 chroot /usr/local/haproxy pidfile /usr/data/haproxy/haproxy.pid uid 99 gid 99 daemon #debug #quiet defaults log global mode tcp option abortonclose option redispatch retries 3 maxconn 2000 timeout connect 5000 timeout client 50000 timeout server 50000 listen proxy_status bind :48066 mode tcp balance roundrobin server mycat_1 192.168.56.20:8066 check inter 10s server mycat_2 192.168.56.21:8066 check inter 10s frontend admin_stats bind :7777 mode http stats enable option httplog maxconn 10 stats refresh 30s stats uri /admin stats auth admin:123123 stats hide-version stats admin if TRUE
6、将之前192.168.56.20上的mycat到192.168.56.21上面再装一次
7、启动验证
7.1、启动HAProxy
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.conf
7.2、查看HAProxy进程
ps -ef|grep haproxy
7.3、打开浏览器访问
http://192.168.56.22:7777/admin
在弹出框输入用户名:admin密码:123123
如果Mycat主备机均已启动,则可以看到如下图
8、验证负载均衡,通过HAProxy访问Mycat
mysql -umycat -pxxxxxx -h 192.168.56.22 -P 48066
ok,成功登陆
9、192.168.56.23的HAProxy和上面安装的步骤相同
四、配置 Keepalived
1、准备好Keepalived安装包,传到/home/dhapp/software/目录下
2、解压到/usr/local/src
tar -zxvf keepalived-1.4.2.tar.gz -C /usr/local/src
3、安装依赖插件
yum install -y gcc openssl-devel popt-devel
4、进入解压后的目录,进行配置,进行编译
cd /usr/local/src/keepalived-1.4.2
./configure --prefix=/usr/local/keepalived
5、进行编译,完成后进行安装
make && make install
6、运行前配置
cp /usr/local/src/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/src/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/7、修改配置文件
vim /etc/keepalived/keepalived.conf
内容如下:
! Configuration File for keepalived global_defs { notification_email { xlcocoon@foxmail.com } notification_email_from keepalived@showjoy.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { #主机配MASTER,备机配BACKUP state MASTER #所在机器网卡 interface ens1 virtual_router_id 51 #数值越大优先级越高 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { #虚拟IP 192.168.56.200 } } virtual_server 192.168.56.200 48066 { delay_loop 6 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP real_server 192.168.56.22 48066 { weight 1 TCP_CHECK { connect_timeout 3 retry 3 delay_before_retry 3 } } real_server 192.168.56.23 48600 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
8、启动验证
启动Keepalived:service keepalived start
登录验证:mysql -umycat -pxxxxxx -h 192.168.56.200 -P 48066
可以通过systemctl status keepalived.service查看keepalived状态
成功登陆!
9、测试高可用
1、 测试步骤
#1、关闭其中一个mycat
#2、通过虚拟ip查询数据
mysql -umycat -pxxxxxx -h 192.168.56.200 -P 48066