In actual projects, the Mycat service also needs to consider high availability. If the server where Mycat is located is down or the Mycat service fails, a standby machine is required to provide services, and the Mycat cluster needs to be considered.
High-availability solution
We can use HAProxy + Keepalived with two Mycats to set up a Mycat cluster to achieve high availability. HAProxy realizes MyCat multi-node cluster high availability and load balancing, and HAProxy's own high availability can be achieved through Keepalived.
Address planning
CPU name | IP | Important services deployed |
---|---|---|
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 |
Two mysql to imitate mysql cluster, two mycat for haproxy high availability, two haproxy for keepalived high availability
Install and deploy mycat
Deploy mycat please refer to: deploy mycat article
After the deployment is complete, mycat is used to test the mycat user login on the random client
//test mycat-01 host
[root@mysql-02 ~]# mysql -umycat -p123456 -P8066 -h192.168.1.61
//Test mycat-02 host
[root@mysql-02 ~]# mysql -umycat -p123456 -P8066 -h192.168.1.69
Install and configure HAProxy
1. Install HAProxy and
prepare the HAProxy installation package, unzip it to /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/
Check the kernel version and compile
yum -y install gcc gcc-c++
cd /usr/local/haproxy-1.9.16/
uname -r
make TARGET=linux310 PREFIX=/usr/local/haproxy
# ARGET=linux310, kernel version, use uname -r to view the kernel, such as: 3.10.0-514.el7, at this time the parameter is linux310;
#ARCH=x86_64, the number of system bits;
#PREFIX=/usr/local/ haprpxy #/usr/local/haprpxy, is the installation path of haprpxy
After the compilation is complete, install it
make install PREFIX=/usr/local/haproxy
Insert the following configuration information into the configuration file and save
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. Start verification
Start haproxy
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.conf
View haproxy process
netstat -anput | grep haproxy
Open the browser and visit http://192.168.1.68:7777/admin. Enter the user name in the pop-up box: admin Password: 123123
Verify load balancing and access Mycat through HAProxy
mysql -umycat -p123456 -P48066 -h192.168.1.68
So far, one of mycat’s downtime will not affect the normal access of mycat.
The second haproxy (harproxy-02 host) has the same configuration as the first one, so omitted here
Install and configure keepalived
1. Install Keepalived
yum -y install openssl-devel popt-devel epel-release.noarch
yum -y install keepalived.x86_64
Keepalived master configuration file
[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 slave configuration file
[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
}
}
}
Start keepalived
systemctl start keepalived
//View VIP address
Main:
[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
From:
[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
The vip is on the haproxy-01 host, because I turned off the firewall directly, I did not consider the phenomenon of split-brain
mysql -umycat -p123456 -P48066 -h192.168.1.100 #-h指定vip地址
//Close haproxy-01 to connect
mysql -umycat -p123456 -P48066 -h192.168.1.100