keepalived安装
MySQL双主配置请看这里:https://tongyao.blog.csdn.net/article/details/132016200?spm=1001.2014.3001.5502
128、129两台服务器安装步骤相同,配置文件不同,下面有介绍。
1.安装相关依赖包,并下载keepalived安装包,解压,配置,编译
[root@128 ~]# cd /opt
[root@128 opt]# yum -y install gcc openssl-devel popt-devel psmisc
[root@128 opt]# wget https://www.keepalived.org/software/keepalived-2.2.7.tar.gz
[root@128 opt]# tar -zxvf keepalived-2.2.7.tar.gz
[root@128 opt]# cd keepalived-2.2.7
[root@128 keepalived-2.2.7]# ./configure --prefix=/opt/keepalived-2.2.7
[root@128 keepalived-2.2.7]# make && make install
2.将文件复制到对应目录下
[root@128 keepalived-2.2.7]# mkdir /etc/keepalived
[root@128 keepalived-2.2.7]# cp keepalived/etc/keepalived/keepalived.conf.sample /etc/keepalived/keepalived.conf
[root@128 keepalived-2.2.7]# cp keepalived/etc/init.d/keepalived /etc/init.d/
[root@128 keepalived-2.2.7]# cp keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@128 keepalived-2.2.7]# cp bin/keepalived /usr/sbin/
3.新建/etc/keepalived/shutdown.sh文件,内容为:
#!/bin/bash
#该脚本是在mysql服务出现异常时,将keepalived应用停止,从而使虚拟vip主机自动连接到另一台mysql上
killall keepalived
执行授权,否则在MySQL出现异常时不会关闭keepalived:
chmod 777 /etc/keepalived/shutdown.sh
4.使用ifconfig命令查看下网卡名称,本机网卡名称为ens33
5.修改128服务器/etc/keepalived/keepalived.conf配置文件,内容如下:
! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_MASTERvrrp_skip_check_adv_addrvrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_instance VI_1 {state MASTER#interface为刚才查到的本机网卡名称interface ens33#同一网段中同一组virtual_router_id值相同。不同组virtual_router_id值唯一。#如server-1、server-2为一组,virtual_router_id=51#server-3、server-4为一组,则virtual_router_id不能为51virtual_router_id 51#字数越大,优先级越高,master必须大于backuppriority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {#该ip为虚拟出来的vip地址192.168.222.130}
}#配置virtual_server ip为上面配置的虚拟vip地址 端口为mysql的端口
virtual_server 192.168.222.130 3306 {delay_loop 6lb_algo rrlb_kind NATpersistence_timeout 50protocol TCP#real_server 该配置为实际物理机ip地址 以及实际物理机mysql端口real_server 192.168.222.128 3306 {#当该ip 端口连接异常时,执行该脚本notify_down /etc/keepalived/shutdown.shTCP_CHECK {#实际物理机ip地址connect_ip 192.168.222.128#实际物理机port端口connect_port 3306connect_timeout 3nb_get_retry 3delay_before_retry 3}}
}
6.修改129服务器/etc/keepalived/keepalived.conf配置文件,与128的配置不同地方为real_server、connect_ip 两个配置,需要配置对应的实际主机ip,详细内容如下:
! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_MASTERvrrp_skip_check_adv_addrvrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_instance VI_1 {state BACKUP#interface为刚才查到的本机网卡名称interface ens33#同一网段中同一组virtual_router_id值相同。不同组virtual_router_id值唯一。#如server-1、server-2为一组,virtual_router_id=51#server-3、server-4为一组,则virtual_router_id不能为51virtual_router_id 51#字数越大,优先级越高,master必须大于backuppriority 90advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {#该ip为虚拟出来的vip地址192.168.222.130}
}#配置virtual_server ip为上面配置的虚拟vip地址 端口为mysql的端口
virtual_server 192.168.222.130 3306 {delay_loop 6lb_algo rrlb_kind NATpersistence_timeout 50protocol TCP#real_server 该配置为实际物理机ip地址 以及实际物理机mysql端口real_server 192.168.222.129 3306 {#当该ip 端口连接异常时,执行该脚本notify_down /etc/keepalived/shutdown.shTCP_CHECK {#实际物理机ip地址connect_ip 192.168.222.129#实际物理机port端口connect_port 3306connect_timeout 3nb_get_retry 3delay_before_retry 3}}
}
详细配置参考《Keepalived 安装和配置详解》
7.将128、129服务器keepalived加入开机自启,并启动服务
[root@128 keepalived-2.2.7]# systemctl enable keepalived
[root@128 keepalived-2.2.7]# systemctl start keepalived
8.启动后keepalived状态为:active(running)则正常
9.启动后相当于虚拟出一个vip 192.168.222.130,可使用远程连接工具,连接该服务器,连接进去后使用ifconfig查看该虚拟vip实际上使用的实体服务器是129服务器。
10.将129服务器的keepalived应用停止,再次查看130服务ifconfig,可以看到,130服务器自动将实体机ip漂移到了128服务器上
至此,keepalived部署完成。
mysql双主双活+keepalived高可用整体测试
1.首先将128、129两台服务器mysql、keepalived应用全部启动,然后用mysql连接工具连接keepalived虚拟出来的192.168.222.130服务器
2.再130数据库test_db库t_test表插入id=3,name=‘wangwu’
3.此时可以查看128、129数据库,数据已同步
4.查看130服务器ifconfig实际使用的物理机为128,所以128服务器mysql为主数据库。
5.此时手动将128服务器mysql停止,keepalived检测到128服务3306端口连接失败,会执行/etc/keepalived/shutdown.sh脚本,将128服务器keepalived应用结束。
6.此时再连接130服务下,ifconfig查看,发现已经实际将物理机由128转移到129服务器上
7.再使用mysql连接工具连接130的mysql,插入一条数据id=4,name=‘zhaoliu’,测试是否将数据存入129服务器mysql中
8.查看129服务器mysql数据,数据已同步,说明keepalived搭建高可用成功,当128服务器mysql出现问题后keepalived自动漂移IP到实体机129服务器上,从而使129服务器mysql作为主数据库。
9.此时再启动128服务器mysql、keepalived应用
10.查看128数据库t_test表数据,数据已同步成功。
11.如果漂移失败,可能是virtual_router_id在同网段内重复导致。可执行命令查看是否有其他ip使用
[root@128 keepalived-2.2.7]# tcpdump -i 网卡名 vrrp -n |grep virtual_router_id值
#例如:
[root@128 keepalived-2.2.7]# tcpdump -i eno16777736 vrrp -n |grep 51
至此,mysql双主双活+keepalived高可用部署并测试完成。
文章参考:https://blog.csdn.net/a360284634/article/details/89892028,并稍做修改。