一. mycat集群+Haproxy+Keepalived
mycat集群+Haproxy+Keepalived+mysql1主2从
环境规划 centos7.9
1主2从,读写分离
名称 | ip | 端口 |
---|---|---|
mysql-master | 192.168.1.220 | 3306 |
mysql-slave1 | 192.168.1.221 | 3306 |
mysql-slave2 | 192.168.1.222 | 3306 |
mycat-1 | 192.168.1.221 | 8066 |
mycat-2 | 192.168.1.222 | 8066 |
haproxy-1 | 192.168.1.221 | 18066 |
haproxy-2 | 192.168.1.222 | 18066 |
keepalived-1 | 192.168.1.221 | |
keepalived-2 | 192.168.1.222 |
1.1 在另一台机器上面部署mycat,
1.2 Keepalived安装
#查看yum可用的keepalived版本
yum list|grep keepalived#在线安装使用yum安装keepalived
yum -y install keepalived.x86_64#在/etc/keepalived下新建check_haproxy.sh脚本,脚本内容如下
cat > /etc/keepalived/check_haproxy.sh <<-'EOF'
#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`
if [ $A -eq 0 ];then
systemctl start haproxy.service
fi
EOFcat /etc/keepalived/check_haproxy.shchmod + /etc/keepalived/check_haproxy.sh
1.3 修改Keepalived配置文件
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf-bak#192.168.1.221 节点1配置
vim /etc/keepalived/keepalived.conf
global_defs {router_id NodeA #当前节点名称
}
vrrp_instance VI_1 {state MASTER #设置主服务器interface ens160 #系统的网卡名字(centos6为eth0,centos7位eth33)virtual_router_id 10 haproxy.cfg #多个保持心跳的keepalived节点要设置同一个router_idpriority 100 #权重,权重数字越大,权重越大:即表示VIP访问权重最大的advert_int 1authentication {auth_type PASS #通过PASS认证(密码认证)auth_pass 123456 #多个保持心跳的keepalived节点要设置同一个auth_pass}virtual_ipaddress { #虚拟IP地址,可设置多个192.168.1.67/24 #对外暴露的IP地址,24是网络位数(可省略)}track_script{ #自定义追踪脚本chk_haproxy #追踪haproxy}
}
vrrp_script chk_haproxy { #定义的方法名script "/etc/keepalived/check_haproxy.sh"interval 4 #追踪频率weight 3 #追踪权重
}#192.168.1.222 节点2配置
vim /etc/keepalived/keepalived.conf
global_defs {router_id NodeB #当前节点名称
}
vrrp_instance VI_1 {state MASTER #设置主服务器interface ens160 #系统的网卡名字(centos6为eth0,centos7位eth33)virtual_router_id 10 haproxy.cfg #多个保持心跳的keepalived节点要设置同一个router_idpriority 90 #权重,权重数字越大,权重越大:即表示VIP访问权重最大的advert_int 1authentication {auth_type PASS #通过PASS认证(密码认证)auth_pass 123456 #多个保持心跳的keepalived节点要设置同一个auth_pass}virtual_ipaddress { #虚拟IP地址,可设置多个192.168.1.67/24 #对外暴露的IP地址,24是网络位数(可省略)}track_script{ #自定义追踪脚本chk_haproxy #追踪haproxy}
}
vrrp_script chk_haproxy { #定义的方法名script "/etc/keepalived/check_haproxy.sh"interval 4 #追踪频率weight 3 #追踪权重
}#重启
systemctl enable keepalived
systemctl restart keepalived
systemctl status keepalived
ip a |grep 192.168.1.67
1.4 Haproxy安装的安装与配置
#查看yum可用的haproxy版本
yum list|grep haproxy
#在线安装使用yum安装haproxy
yum -y install haproxy.x86_64cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg-bakcd /etc/haproxy/
节点1和节点2 配置修改
vim /etc/haproxy/haproxy.cfggloballog 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.pidmaxconn 4000user haproxygroup haproxydaemon# turn on stats unix socketstats socket /var/lib/haproxy/statsdefaultsmode tcplog globaloption tcplogoption dontlognulloption http-server-closeoption redispatchretries 3timeout http-request 10stimeout queue 1mtimeout connect 10stimeout client 1mtimeout server 1mtimeout http-keep-alive 10stimeout check 10smaxconn 3000#配置haproxy可连接的地址,与绑定固定的域名
frontend mysqlbind 0.0.0.0:18066mode tcplog globaldefault_backend mysql_server#负载均衡的真实数据库地址
backend mysql_serverbalance roundrobinserver mycat1 192.168.1.221:8066 check inter 5s rise 2 fall 3server mycat2 192.168.1.222:8066 check inter 5s rise 2 fall 3#rise 2是2次正确认为服务器可用,fall 3是3次失败认为服务器不可用#客户端配置
listen admin_statusmode httpbind 0.0.0.0:18000option httploglog globalstats enablestats refresh 10sstats hide-versionstats realm Haproxy\ Statisticsstats uri /admin-statusstats auth admin:123456stats admin if TRUEsystemctl enable haproxy.service
systemctl restart haproxy.service
systemctl status haproxy.service
启动mycat
cd /mysql/app/mycat/bin#测试启动控制台
./mycat console#启动
./startup_nowrap.sh#查看进程
jps
1.5 测试mysql
# VIP测试
mysql -uroot -p123456 -P18066 -h 192.168.1.67
http://192.168.1.67:18000/admin-status