Keepalived: 192.168.165.135
Keepalived: 192.168.165.136
MySQL-master1: 192.168.165.135
MySQL-master2: 192.168.165.136
一. 安装mysql5.7, 并配置主主备份
二. 在mysql-master1 上部署Keepalived
(1)yum源安装:yuminstall keepalived –y,只是版本低一些是1.2.13
(2)源码安装:
# 下载最新版本:1.3.6
wget http://www.keepalived.org/software/keepalived-1.3.6.tar.gz
# 解压缩安装
tar -xvf keepalived-1.3.6.tar.gz
cd keepalived-1.3.6
yum install openssl* -y
./configure --prefix=/usr/local/keepalived-1.3.6
make
make install
Tips 1:安装过程中提示:*** WARNING – this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
# yum install -y libnl3.x86_64 # yum install -y libnl3-devel.x86_64
Tips 2:configure: error: !!! Please install libnfnetlink headers. !!!
# yum install -y libnfnetlink.x86_64 # yum install -y libnfnetlink-devel.x86_64
(3)配置:
配置可以参考:Keepalived的配置详解
(4)设置开机启动项:
在源文件的目录./keepalived-1.3.6/keepalived/etc/init.d下有3个三个快捷启动的文件。
# cp ./keepalived-1.3.6/keepalived/etc/init.d/keepalived /etc/init.d/ # cp /usr/local/keepalived-1.3.6/etc/keepalived/keepalived.conf /etc/keepalived/ # cp ./keepalived-1.3.6/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived #keepalived的快捷启动,必须要执行以上三步,且文件路径必须一致,也不知道什么原因, # 可能是init.d中的keepalived中已经指明了吧
这样可以执行service keepalived [start | stop | reload | restart ]
设置开机启动:
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived
chkconfig keepalived on
在运行就Ok了
service keepalived start
Starting keepalived (via systemctl): [ OK ]
(5)添加keepalived.conf配置文件:
sudo vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP # 2 severs keep the same value.
interface eth0 # 修改为自己的网卡名字(ifconfig)
virtual_router_id 51
priority 100 # priority, the another set to 90
advert_int 1
nopreempt #don't race to control, set the highst priorty mysql servers only.
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { # 虚拟ip
192.168.165.200
}
}
virtual_server 192.168.165.200 3306 { # 虚拟ip
delay_loop 2 # check the real_server status for every 2 seconds.
lb_algo wrr #LVS arithmetic
lb_kind DR #LVS model
persistence_timeout 60 #k
protocol TCP
real_server 192.168.165.135 3306 { # 真实本机IP
weight 3
notify_down /etc/keepalived/bin/mysql.sh # run the scripts if mysql is down.
TCP_CHECK {
connect_timeout 10 #timeout
nb_get_retry 3 #conect times to try to connect
delay_before_retry 3 #interval of retry
connect_port 3306 # check mysql port
}
}
}
三. 在mysql-master2 上部署Keepalived
安装和启动keepalived就不再赘述,直接上keepalived的配置文件,只是keepalived.conf有所不同,如下黄色背景的部分配置信息:
#vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP # 2 severs keep the same value.
interface eth0 # 修改为自己的网卡名字(ifconfig)
virtual_router_id 51
priority 90 # priority, m2 is set to 90
advert_int 1
#nopreempt #don't race to control, set the highst priorty mysql servers only.
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.165.200
}
}
virtual_server 192.168.165.200 3306 {
delay_loop 2 # check the real_server status for every 2 seconds.
lb_algo wrr #LVS arithmetic
lb_kind DR #LVS model
persistence_timeout 60 #k
protocol TCP
real_server 192.168.165.136 3306 {
weight 3
notify_down /etc/keepalived/bin/mysql.sh # run the scripts if mysql is down.
TCP_CHECK {
connect_timeout 10 #
nb_get_retry 3 #conect times to try to connect
delay_before_retry 3 #interval of retry
connect_port 3306 # check mysql port
}
}
}
四. 日志
Keepalived默认的日志在/var/log/messages里面
五. 给Mysql-master1和master2添加检测脚本
master1和master2上都添加此检测脚本,作用是当mysql停止工作时自动关闭本机的keepalived,从而实现将故障机器踢出(因每台机器上keepalived只添加了本机为realserver).
当mysqld正常启动起来后,要手动启动keepalived服务。
#mkdir /etc/keepalived/bin
vi /etc/keepalived /bin/mysql.sh,内容如下:
#!/bin/bash
pkill keepalived
/sbin/ifdown eth0 && /sbin/ifup eth0 # eth0根据自己机器修改