keepalived安装配置实现高可用
角色 |
IP |
用途 |
Master A |
10.10.10.33 |
MySQL DB |
Master B |
10.10.10.36 |
|
VIP |
10.10.10.199 |
KeepAlived VIP |
解压安装包到指定目录/opt/usr/:
# tar -zxvf keepalived-1.2.13.tar.gz -C /opt/usr/
# cd /opt/usr/keepalived-1.2.13/
创建安装目录:
# mkdir /opt/usr/keepalived
编译安装
# ./configure --prefix=/opt/usr/keepalived/
# make;make install
configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
解决:
yum install -y openssl openssl-devel
默认情况下keepalived启动时会去/etc/keepalived目录下找配置文件
#A主机和B主机都操作
# mkdir /etc/keepalived
#cp安装生成的conf模板到/etc/keepalived
# cp /opt/usr/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
#Master A配置文件
# vi /etc/keepalived/keepalived.conf
# mkdir -p /usr/local/MySQL/bin/
# vi /usr/local/MySQL/bin/MySQL.sh
注:此脚本是上面配置文件notify_down选项所用到的,keepalived使用notify_down选项来检查real_server的服务状态,当发现real_server服务故障时,便触发此脚本;我们可以看到,脚本就一个命令,通过pkill keepalived强制杀死keepalived进程,从而实现了MySQL故障自动转移。另外,我们不用担心两个MySQL会同时提供数据更新操作,因为每台MySQL上的keepalived的配置里面只有本机MySQL的IP+VIP,而不是两台MySQL的IP+VIP
~]# /opt/us/keepalived/sbin/keepalived –D
# ps -ef|grep keep
#停止MySQL服务
# /etc/init.d/mysql stop
Shutting down MySQL..... [ OK ]
#查看vip,发现VIP已经不存在
# ip a|grep eth0
3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.56.101/24 brd 192.168.56.255 scope global eth1
#查看keepalive进程消失
# ps -ef|grep keep
root 29537 4894 0 20:34 pts/2 00:00:00 grep keep
#监控日志
# tail -f /var/log/messages
Dec 3 20:34:08 MySQL1 Keepalived_healthcheckers[29424]: TCP connection to [192.168.56.101]:3306 failed !!!
Dec 3 20:34:08 MySQL1 Keepalived_healthcheckers[29424]: Removing service [192.168.56.101]:3306 from VS [192.168.56.111]:3306
Dec 3 20:34:08 MySQL1 Keepalived_healthcheckers[29424]: Executing [/usr/local/MySQL/bin/MySQL.sh] for service [192.168.56.101]:3306 in VS [192.168.56.111]:3306
Dec 3 20:34:08 MySQL1 Keepalived_healthcheckers[29424]: Lost quorum 1-0=1 > 0 for VS [192.168.56.111]:3306
Dec 3 20:34:08 MySQL1 Keepalived_healthcheckers[29424]: Remote SMTP server [127.0.0.1]:25 connected.
Dec 3 20:34:09 MySQL1 Keepalived[29423]: Stopping Keepalived v1.2.13 (12/03,2014)
Dec 3 20:34:09 MySQL1 Keepalived_vrrp[29425]: VRRP_Instance(MySQL-HA{) sending 0 priority
Dec 3 20:34:09 MySQL1 Keepalived_vrrp[29425]: VRRP_Instance(MySQL-HA{) removing protocol VIPs.
Dec 3 20:34:09 MySQL1 Keepalived_healthcheckers[29424]: Netlink reflector reports IP 192.168.56.111 removed
Dec 3 20:34:09 MySQL1 avahi-daemon[1430]: Server startup complete. Host name is MySQL1-62.local. Local service cookie is 1197773774.
Dec 3 20:34:09 MySQL1 avahi-daemon[1430]: Withdrawing address record for 192.168.56.111 on eth1.
Dec 3 20:34:10 MySQL1 avahi-daemon[1430]: Service "MySQL1-62" (/services/ssh.service) successfully established.
#跟DB1基本一致,但有三个地方不同:优先级为90、无抢占设置、real_server为本机IP
# vi /etc/keepalived/keepalived.conf
# mkdir -p /usr/local/MySQL/bin/
# vi /usr/local/MySQL/bin/MySQL.sh
# /opt/usr/keepalived/sbin/keepalived
# ip a|grep eth0
#停止MySQL服务
# /etc/init.d/mysql stop
Shutting down MySQL..... [ OK ]
#查看vip,发现VIP已经不存在
# ip a|grep eth0
3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.56.101/24 brd 192.168.56.255 scope global eth1
#查看keepalive进程消失
# ps -ef|grep keep
root 29537 4894 0 20:34 pts/2 00:00:00 grep keep
#监控日志
# tail -f /var/log/messages
Dec 4 10:35:53 MySQL2 Keepalived_healthcheckers[6733]: TCP connection to [192.168.56.102]:3306 failed !!!
Dec 4 10:35:53 MySQL2 Keepalived_healthcheckers[6733]: Removing service [192.168.56.102]:3306 from VS [192.168.56.111]:3306
Dec 4 10:35:53 MySQL2 Keepalived_healthcheckers[6733]: Executing [/usr/local/MySQL/bin/MySQL.sh] for service [192.168.56.102]:3306 in VS [192.168.56.111]:3306
Dec 4 10:35:53 MySQL2 Keepalived_healthcheckers[6733]: Lost quorum 1-0=1 > 0 for VS [192.168.56.111]:3306
Dec 4 10:35:53 MySQL2 Keepalived_healthcheckers[6733]: Remote SMTP server [127.0.0.1]:25 connected.
Dec 4 10:35:53 MySQL2 Keepalived[6732]: Stopping Keepalived v1.2.13 (11/24,2014)
Dec 4 10:35:53 MySQL2 Keepalived_vrrp[6734]: VRRP_Instance(MySQL-HA{) sending 0 priority
Dec 4 10:35:53 MySQL2 Keepalived_vrrp[6734]: VRRP_Instance(MySQL-HA{) removing protocol VIPs.
Dec 4 10:35:53 MySQL2 Keepalived_healthcheckers[6733]: Netlink reflector reports IP 192.168.56.111 removed
Dec 4 10:35:54 MySQL2 avahi-daemon[1387]: Withdrawing address record for 192.168.56.111 on eth1.
~]# mysql -u root -h 10.10.10.199 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2622
Server version: 5.6.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
cp /opt/usr/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /opt/usr/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /opt/usr/keepalived/sbin/keepalived /sbin/
# /etc/init.d/keepalived -h
Usage: /etc/init.d/keepalived {start|stop|reload|restart|condrestart|status}
chkconfig --level 2345 keepalived on