[root@host02]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@host02 sysconfig]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
1.5 keepalived验证
1.5.1 master上执行
[root@host01 keepalived]# ip addr
1: lo: mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 08:00:27:d1:2c:51 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.111/24 brd 192.168.56.255 scope global eth0
inet 192.168.56.199/24 scope global secondary eth0
3: eth1: mtu 1500 qdisc noop qlen 1000
link/ether 08:00:27:b9:1e:c3 brd ff:ff:ff:ff:ff:ff
1.5.2 backup上执行
关闭master上的keepalived服务后在backup上查看
[root@host02 keepalived]# ip addr
1: lo: mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 08:00:27:70:5c:2d brd ff:ff:ff:ff:ff:ff
inet 192.168.56.112/24 brd 192.168.56.255 scope global eth0
inet 192.168.56.199/24 scope global secondary eth0
3: eth1: mtu 1500 qdisc noop qlen 1000
link/ether 08:00:27:49:31:51 brd ff:ff:ff:ff:ff:ff
1.6 keepalived启动指定配置文件和日志文件
分别在master和backup机器上编辑该文件
/usr/local/keepalived/etc/sysconfig/ keepalived,修改红色部分的内容
[root@host01 sysconfig]# more keepalived
# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp -P Only run with VRRP subsystem.
# --check -C Only run with Health-checker subsystem.
# --dont-release-vrrp -V Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs -I Dont remove IPVS topology on daemon stop.
# --dump-conf -d Dump the configuration data.
# --log-detail -D Detailed log messages.
# --log-facility -S 0-7 Set local syslog facility (default=LOG_DAEMON)
#
KEEPALIVED_OPTIONS="-D -d -S 0 -f /etc/keepalived/keepalived.conf"
-f 指定了自定义的配置文件
-S指定了日志文件,我们需要在/etc/syslog.conf文件添加如下红色标识的内容
CentOS 7下该文件为/etc/rsyslog.conf
[root@host01 etc]# more /etc/syslog.conf
# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.* /dev/console
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none /var/log/messages
# The authpriv file has restricted access.
authpriv.* /var/log/secure
# Log all the mail messages in one place.
mail.* -/var/log/maillog
# Log cron stuff
cron.* /var/log/cron
# Everybody gets emergency messages
*.emerg *
# Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler
# Save boot messages also to boot.log
local7.* /var/log/boot.log
# keepalived -S 0
local0.* /var/log/keepalived.log
1.7 配置检查脚本
我们在做mysql高可用的时候,检查mysql停掉后,相应的keepalived也停掉,让其切换到另一台机器
在master上操作,修改配置文件,添加红色部分的内容
vi /etc/keepalived/keepalived.conf
内容如下:
! Configuration File for keepalived
global_defs {
notification_email {
slnngk@163.com
}
notification_email_from slnngk@163.com
smtp_server 192.168.56.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_mysql {
script "/etc/keepalived/check_mysql.sh"
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.56.199/24 dev eth0 scope global
}
track_script { ##注意这里{前面必须有空格,否则不会生效
check_mysql
}
}
要是不在keepalived配置文件中定义执行脚本的话,可以在crontab定时任务中每时每刻执行检查脚本。
### MySQL_HA
* * * * * /etc/keepalived/check_mysql.sh >/dev/null 2>&1
* * * * * sleep 10;/etc/keepalived/check_mysql.sh >/etc/keepalived/mycheck.log 2>&1
* * * * * sleep 20;/etc/keepalived/check_mysql.sh >/etc/keepalived/mycheck.log 2>&1
* * * * * sleep 30;/etc/keepalived/check_mysql.sh >/etc/keepalived/mycheck.log 2>&1
* * * * * sleep 40;/etc/keepalived/check_mysql.sh >/etc/keepalived/mycheck.log 2>&1
* * * * * sleep 50;/etc/keepalived/check_mysql.sh >/etc/keepalived/mycheck.log 2>&1
check_mysql.sh脚本内容如下:
[root@host01 keepalived]# more check_mysql.sh
#!/bin/bash
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=mysql
SOCK_FILE=/db/mysqlmha/mysql.sock
CHECK_TIME=3
now_date=`date '+%Y-%m-%d %H:%M:%S'`
#mysql is working MYSQL_OK is 0 , mysql down MYSQL_OK is 1
MYSQL_OK=1
function check_mysql_helth (){
mysql -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -S $SOCK_FILE -e "show status;" > /etc/keepalived/mycheck.log 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=0
else
MYSQL_OK=1
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
echo $now_date>>/etc/keepalived/mycheck.log
if [ $MYSQL_OK = 0 ] ; then
CHECK_TIME=0
exit 0
fi
if [ $MYSQL_OK -eq 1 ] && [ $CHECK_TIME -eq 1 ]
then
##pkill keepalived
/sbin/service keepalived stop
exit 1
fi
sleep 1
done
-- The End --