1、环境说明
haproxy + Keepalived 主服务器1:192.168.137.71,mysql_fz_01
haproxy + Keepalived 备服务器1:192.168.137.72,mysql_fz_02
haproxy + Keepalived 虚拟服务器(VIP):192.168.137.73单主MGR:
mysql主服务器A:192.168.137.55
mysql备服务器B:192.168.137.56
mysql备库服务器C:192.168.137.57
2、搭建单主MGR
00、修改host文件配置(55/56/57)三节点
vi /etc/hosts
192.168.137.55 mysql01
192.168.137.56 mysql02
192.168.137.57 mysql03
01、配置参数(55/56/57)三节点
#55/56/57/58
vi /mysql/data/3306/my.cnf
#bind_address=0.0.0.0 --屏蔽该选项
log_bin=/mysql/log/3306/binlog/binlog
log_bin_index=/mysql/log/3306/binlog/binlog.index
binlog_format=row
binlog_rows_query_log_events=on
binlog_checksum=none
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=4
slave_preserve_commit_order=1
gtid_mode = on
enforce_gtid_consistency = 1
log-slave-updates = 1
binlog_gtid_simple_recovery=1
relay_log = /mysql/log/3306/relaylog/relay.log
relay-log-index = /mysql/log/3306/relaylog/relay.index
master_info_repository=table
relay_log_info_repository=table
plugin_load="group_replication=group_replication.so"
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "192.168.137.55:33006"
#loose-group_replication_local_address= "192.168.137.56:33006"
#loose-group_replication_local_address= "192.168.137.57:33006"
loose-group_replication_group_seeds="192.168.137.55:33006,192.168.137.56:33006,192.168.137.57:33006,192.168.137.58:33006"
loose-group_replication_bootstrap_group= off
loose-group_replication_member_weight=50
#重启三台数据库
systemctl restart mysqld
02、配置mgr的第一个节点:
以下步骤在192.168.137.55主机上的MySQL中执行
第一步:创建用于复制的用户
mysql -uroot -proot --socket=/mysql/data/3306/mysql.sock
-- 关闭二进制日志,防止其他节点重复此操作
set sql_log_bin=0;
create user repuser@'%' identified by 'repuser123';
grant replication slave,replication client on *.* to repuser@'%';
create user repuser@'127.0.0.1' identified by 'repuser123';
grant replication slave,replication client on *.* to repuser@'127.0.0.1';
create user repuser@'localhost' identified by 'repuser123';
grant replication slave,replication client on *.* to repuser@'localhost';
set sql_log_bin=1;
第二步:配置复制所使用的用户
change master to master_user='repuser',master_password='repuser123' for channel 'group_replication_recovery';
第三步:检查MySQL Group replication插件
show plugins;
第四步:建群(初始化一个复制组 )
set global group_replication_bootstrap_group=on;
start group_replication;
set global group_replication_bootstrap_group=off;
select * from performance_schema.replication_group_members;
03、配置mgr的第二个节点
第一步:创建用于复制的用户
mysql -uroot -proot --socket=/mysql/data/3306/mysql.sock
set sql_log_bin=0;
create user repuser@'%' identified by 'repuser123';
grant replication slave,replication client on *.* to repuser@'%';
create user repuser@'127.0.0.1' identified by 'repuser123';
grant replication slave,replication client on *.* to repuser@'127.0.0.1';
create user repuser@'localhost' identified by 'repuser123';
grant replication slave,replication client on *.* to repuser@'localhost';
flush privileges;
set sql_log_bin=1;
第二步:配置复制所使用的用户
change master to master_user='repuser',master_password='repuser123' for channel 'group_replication_recovery';
第三步:检查MySQL Group replication插件
show plugins;
第四步:加入前面创建好的复制组
start group_replication;
select * from performance_schema.replication_group_members;
04、配置mgr其他节点(和第二节点一样的配置方法)
05、确认3台机的状态
select @@read_only,@@super_read_only;
show variables like 'read_only';
#查找集群组中的主节点
select b.member_id, b.member_host, b.member_port from performance_schema.global_status a join performance_schema.replication_group_members b on a.variable_value= b.member_id where a.variable_name= 'group_replication_primary_member';
3、安装Keepalived软件
01、操作系统参数配置
添加非本地ip绑定支持
echo "net.ipv4.ip_nonlocal_bind=1" >> /etc/sysctl.conf
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p
开启路由转发功能
echo "1" > /proc/sys/net/ipv4/ip_forward #临时生效
echo "net.ipv4.ip_forward" >> /etc/sysctl.conf #永久生效
sysctl -p
02、下载并安装keepalived
mount /dev/cdrom /mnt
yum install kernel-devel openssl-devel popt-devel -y
cd /soft/
tar zxvf keepalived-1.4.4.tar.gz
cd keepalived-1.4.4
./configure --prefix=/usr/local/keepalived/
make && make install
#设置Keepalived开机自启
systemctl enable keepalived
#设置Keepalived日志
#修改vi /usr/local/keepalived/etc/sysconfig/keepalived
#把KEEPALIVED_OPTIONS="-D" 修改为KEEPALIVED_OPTIONS="-D -d -S 0"
#在/etc/rsyslog.conf末尾添加
vi /etc/rsyslog.conf
local0.* /var/log/keepalived.log
systemctl restart rsyslog
4、安装haproxy
cd /soft
tar zxvf haproxy-1.8.9.tar.gz
cd haproxy-1.8.9
make TARGET=linux2628 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
/usr/local/haproxy/sbin/haproxy -v
useradd -r -s /sbin/nologin haproxy
touch /var/log/haproxy.log
chmod 755 /var/log/haproxy.log
vi /etc/rsyslog.conf
$ModLoad imudp #去掉注释
$UDPServerRun 514 #去掉注释
local0.* /var/log/haproxy.log
vi /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS="-r -m 0 -c 2"
systemctl restart rsyslog
5、Keepalived配置
01、先测试Keepalived配置文件并可以正常服务
#第一台机(负载均衡主库):
! Configuration File for keepalived
global_defs
{
router_id it-mysql-slave
notification_email
{
112233@sina.com
}
notification_email_from 112233@sina.com
smtp_server stmp.qq.com
smtp_connect_timeout 30
}
vrrp_script chk_haproxy
{
script "/etc/keepalived/scripts/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance v_mysql_slave_wgpt1
{
interface ens33
state MASTER
virtual_router_id 73
priority 200
nopreempt
virtual_ipaddress
{
192.168.137.73/24
}
track_script
{
chk_haproxy
}
notify_master /etc/keepalived/scripts/haproxy_master.sh
notify_backup /etc/keepalived/scripts/haproxy_backup.sh
notify_fault /etc/keepalived/scripts/haproxy_fault.sh
notify_stop /etc/keepalived/scripts/haproxy_stop.sh
}
#第二台机(负载均衡备库):
mkdir /etc/keepalived
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs
{
router_id it-mysql-slave
notification_email
{
112233@sina.com
}
notification_email_from 112233@sina.com
smtp_server stmp.qq.com
smtp_connect_timeout 30
}
vrrp_script chk_haproxy
{
script "/etc/keepalived/scripts/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance v_mysql_slave_wgpt1
{
interface ens33
state BACKUP
virtual_router_id 73
priority 150
nopreempt
virtual_ipaddress
{
192.168.137.73/32
}
track_script
{
chk_haproxy
}
notify_master /etc/keepalived/scripts/haproxy_master.sh
notify_backup /etc/keepalived/scripts/haproxy_backup.sh
notify_fault /etc/keepalived/scripts/haproxy_fault.sh
notify_stop /etc/keepalived/scripts/haproxy_stop.sh
}
02、在两台主机分别配置haproxy.cfg
#在两台负载均衡主机配置:
vi /usr/local/haproxy/haproxy.cfg
global #全局配置参数
log 127.0.0.1 local0 notice #日志
#user haproxy
#group haproxy
daemon #以后台方式运行haproxy
#quiet
nbproc 1 #后台进程数量,可以设置多个进程,来提高性能,根据CPU设置
pidfile /usr/local/haproxy/haproxy.pid #haproxy的pid存放路径
defaults #一些默认参数
log global #全局日志
retries 3 #3次连接失败,认为服务不可用
#option dontlognull 关键字表示日志中将不会记录空连接。所谓空连接就是在上游的负载均衡器或者监控系统为了探测该服务是否存活可用时,需要定期的连接或者获取某一固定的组件或页面,或者探测扫描端口是否在监听或开放等动作被称为空连接,官方文档中标注,如果该服务上游没有其他的负载均衡器的话,建议不要设置该参数,因为设置后互联网上的恶意扫描或其他动作就不会被记录下来。
option dontlognull
option redispatch #对应的服务器挂掉后,强制指向到其他正常的服务器
maxconn 2000 #默认最大链接数
timeout queue 1m #等待最大时长1分钟
timeout http-request 10s #客户端建立连接又不请求数据的时候超过10秒就关闭客户端连接
timeout connect 10s #定义haproxy将客户端请求转发至后端服务器所等待的超时时长
timeout server 1m #服务器非活动状态的超时时长
timeout client 1m #客户端非活动状态的超时时长
timeout http-keep-alive 10s #设置http-keep-alive超时时长
timeout check 10s #健康状态检测时的超时时间
balance roundrobin #负载均衡方式,轮询方式(保持会话session:源地址/cookies,针对web应用服务器)
#定义负载均衡的配置
listen mysql_slave_wgpt_lb1
bind 192.168.137.73:3306 #绑定的ip和端口
mode tcp #模式是tcp,(7层http,4层tcp,如果是mysql肯定是tcp,如果是weblogic、was、Tomcat之类的就用http)
option mysql-check user haproxy_check #通过mysql连接去检测mysql是否可以访问
stats hide-version #隐藏统计页面上的haproxy的版本信息
balance roundrobin #负载均衡策略:轮询方式
#服务器定义
#check inter 2000 :检测心跳频率,毫秒
#rise 2:2次正确认为服务器可用
#fall 5:5次失败认为服务器不可用
#maxconn 300:最大连接数
server mysql55 192.168.137.55:3306 weight 1 check inter 2000 rise 2 fall 5 maxconn 300
server mysql56 192.168.137.56:3306 weight 1 check inter 2000 rise 2 fall 5 maxconn 300
server mysql55 192.168.137.57:3306 weight 1 check inter 2000 rise 2 fall 5 maxconn 300
#自带的监控服务器的配置
listen haproxy_stats
mode http
bind *:8888
option httplog
stats refresh 5s #5秒刷新一次
stats uri /haproxy-stat
stats realm Haproxy Manager
stats auth haproxy:haproxy #监控
03、添加haproxy_check用户
将下面SQL语句在master端执行,通过复制功能,传递到slave上:
drop user haproxy_check@'%';
create user haproxy_check@'192.168.137.55';
create user haproxy_check@'192.168.137.56';
create user haproxy_check@'192.168.137.57';
create user haproxy_check@'192.168.137.58';
create user haproxy_check@'192.168.137.73';
create user haproxy_check@'192.168.137.71';
create user haproxy_check@'192.168.137.72';
grant usage on *.* to haproxy_check@'192.168.137.55';
grant usage on *.* to haproxy_check@'192.168.137.56';
grant usage on *.* to haproxy_check@'192.168.137.57';
grant usage on *.* to haproxy_check@'192.168.137.58';
grant usage on *.* to haproxy_check@'192.168.137.71';
grant usage on *.* to haproxy_check@'192.168.137.72';
grant usage on *.* to haproxy_check@'192.168.137.73';
select user,host from mysql.user;
04、启动 keepalived
两个节点开启Keepalived(主节点会获得VIP,自动拉起haproxy)
systemctl daemon-reload
systemctl enable keepalived
systemctl start keepalived
ip -4 a | grep 73
netstat -tunlp | grep haproxy
ps -ef | grep keepalived
mysql -uroot -proot -h192.168.137.73 -e "select @@hostname;"
http://192.168.137.73:8888/haproxy-stat