HAProxy + Keepalived实现mycat高可用方案
在实际项目中, Mycat 服务也需要考虑高可用性,如果 Mycat 所在服务器出现宕机,或 Mycat 服务故障,需要有备机提供服务,需要考虑 Mycat 集群。
高可用方案
我们可以使用 HAProxy + Keepalived 配合两台 Mycat 搭起 Mycat 集群,实现高可用性。 HAProxy实现了MyCat 多节点的集群高可用和负载均衡, 而 HAProxy 自身的高可用则可以通过 Keepalived 来实现。
地址规划
主机名 | IP | 部署的重要服务 |
---|---|---|
mysql-01 | 192.168.1.65 | MySQL |
mysql-02 | 192.168.1.67 | MySQL |
mycat-01 | 192.168.1.61 | Mycat |
mycat-02 | 192.168.1.69 | Mycat |
haproxy-01 | 192.168.1.68 | HAProxy、Keepalived |
haproxy-02 | 192.168.1.63 | HAProxy、Keepalived |
两台mysql来模仿mysql集群,两台mycat做haproxy高可用,两台haproxy做keepalived高可用
安装部署mycat
部署mycat请参考:部署mycat文章
部署完成mycat在随意客户端测试mycat用户登入
//测试mycat-01主机
[root@mysql-02 ~]# mysql -umycat -p123456 -P8066 -h192.168.1.61
//测试mycat-02主机
[root@mysql-02 ~]# mysql -umycat -p123456 -P8066 -h192.168.1.69
安装配置 HAProxy
1. 安装 HAProxy
准备好HAProxy安装包,解压到/usr/local/
wget https://www.haproxy.org/download/1.9/src/haproxy-1.9.16.tar.gz
tar zxf haproxy-1.9.16.tar.gz -C /usr/local/
查看内核版本, 进行编译
yum -y install gcc gcc-c++
cd /usr/local/haproxy-1.9.16/
uname -r
make TARGET=linux310 PREFIX=/usr/local/haproxy
# ARGET=linux310,内核版本,使用uname -r查看内核,如: 3.10.0-514.el7,此时该参数就为linux310;
ARCH=x86_64,系统位数;
PREFIX=/usr/local/haprpxy #/usr/local/haprpxy,为haprpxy安装路径
编译完成后,进行安装
make install PREFIX=/usr/local/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/local/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.1.61:8066 check inter 10s
server mycat_2 192.168.1.69: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
2.启动验证
启动haproxy
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.conf
查看haproxy进程
netstat -anput | grep haproxy
打开浏览器访问 http://192.168.1.68:7777/admin 在弹出框输入用户名: admin密码: 123123
验证负载均衡,通过HAProxy访问Mycat
mysql -umycat -p123456 -P48066 -h192.168.1.68
至此,其中一台mycat宕机也不影响mycat的正常访问
第二台haproxy(harproxy-02主机)和第一台配置一样,此处省略
安装配置keepalived
1. 安装 Keepalived
yum -y install openssl-devel popt-devel epel-release.noarch
yum -y install keepalived.x86_64
keepalived主的配置文件
[root@haproxy-01 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL01
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}
virtual_server 192.168.1.100 48066 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.1.68 48066 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
keepalived从的配置文件
[root@haproxy-02 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}
virtual_server 192.168.1.100 48066 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.1.63 48066 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
启动keepalived
systemctl start keepalived
//查看vip地址
主:
[root@haproxy-01 ~]# ip a show dev ens33;
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:9a:a6:17 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.68/24 brd 192.168.1.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.1.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::e7ee:bd12:3316:9deb/64 scope link
valid_lft forever preferred_lft forever
从:
[root@haproxy-02 ~]# ip a show dev ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:dd:7b:21 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.63/24 brd 192.168.1.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::a836:35bc:b688:93fe/64 scope link
valid_lft forever preferred_lft forever
vip在haproxy-01主机上,由于我把防火墙直接关掉了就没有考虑脑裂的现象
mysql -umycat -p123456 -P48066 -h192.168.1.100 #-h指定vip地址
//关闭haproxy-01在进行连接
mysql -umycat -p123456 -P48066 -h192.168.1.100