一、前言
Keepalived是用来实现高可用的,提供健康检查,故障转移,即保证主LVS宕机后,从LVS可以在很短时间顶上,从而保证了系统或网站的稳定性。Keepalived在后端的realserver接收不到主节点的信息之后,keepalived能够自己调用ipvsadm命令生成规则,能够自动实现,将主节点的VIP以及ipvs规则“拿过来”,应用在从节点上,继续为用户服务。
拓扑图:
二:配置keepalived
1 keepalive安装
分别在master1,master2上安装keepalive
yum install -y popt-devel
cd /usr/local/src
wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
tar zxvf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure --prefix=/
make
make install
假如在执行./configure --prefix=/时报错:
OpenSSL is not properly installed on your system !!!Can notinclude OpenSSL headers files
yum install openssl-devel -y
2 分别在master1,master2上新建检查mysql脚本
vi /root/check_mysql.sh
内容如下
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=system@123
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" >/dev/null 2>&1
#$mysqlclient --host=$host --port=$port --user=$user --password=$password -e "show databases;" > /dev/null 2>&1
if [ $? == 0 ]
then
echo " $host mysql login successfully "
exit 0
else
#echo " $host mysql login faild"
/etc/init.d/keepalived stop
exit 2
fi
chmod +x /root/check_mysql.sh
3. 修改配置文件
vi /etc/keepalived/keepalived.conf
master1和master2配置文件内容相同。 内容:
#ConfigurationFile for keepalived
global_defs {
notification_email { ######定义接受邮件的邮箱
wangjj@hrloo.com
}
notification_email_from jiankong@staff.tuge.com ######定义发送邮件的邮箱
smtp_server mail.tuge.com
smtp_connect_timeout 10
}
vrrp_script check_mysql { ######定义监控mysql的脚本
script "/root/check_mysql.sh"
interval 2 ######监控时间间隔
weight 2 ######负载参数
}
vrrp_instance vrrptest { ######定义vrrptest实例
state BACKUP ######服务器状态
interface eth0 ######使用的接口
virtual_router_id 51 ######虚拟路由的标志,一组lvs的虚拟路由标识必须相同,这样才能切换
priority 150 ######服务启动优先级,值越大,优先级越高,BACKUP 不能大于MASTER
advert_int 1 ######服务器之间的存活检查时间
authentication {
auth_type PASS ######认证类型
auth_pass ufsoft ######认证密码,一组lvs 服务器的认证密码必须一致
}
track_script { ######执行监控mysql进程的脚本
check_mysql
}
virtual_ipaddress { ######虚拟IP地址
192.168.1.60
}
}
这里state不配置MASTER,且优先级一样,是期望在MASTER1宕机后再恢复时,不主动将MASTER状态抢过来,避免MySQL服务的波动。 由于不存在使用lvs进行负载均衡,不需要配置虚拟服务器virtual server,下同。
4. vi /etc/sysconfig/iptables
#注意,在两台机器上都要修改。 添加:
-A INPUT -d 192.168.1.60/32 -j ACCEPT
-A INPUT -d 224.0.0.18 -j ACCEPT #添加VRRP通讯支持
注意:第一行中的192.168.1.60需要改成你自己的vip。
service iptables restart
5 启动keepalived
在master1、master2上分别启动:
service keepalived start
分别执行ip addr命令,可以在其中一台机器上看到虚拟IP.如:
[root@slave1 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_faststate UP qlen 1000
link/ether 08:00:27:04:05:16 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.22/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.60/32 scope global eth0
inet6 fe80::a00:27ff:fe04:516/64 scope link tentativedadfailed
valid_lft forever preferred_lft forever
说明虚拟vip连在了master1这台机器上。 如果自动只连接到了master2,关闭master2的keepalived,再启动,自动就连接到master1了。 现在都可以ping通虚拟ip了。
6 测试
停止master1服务器keepalived,检查VIP是否切换到master2服务器(用ip addr命令验证即可);
三 测试高可用环境是否配置成功
3.1 建允许远程访问的用户 在master1,master2创建允许远程访问的用户:
grant select,update,delete,insert on *.* to 'dandan' identified by 'dandan';
3.2 访问虚拟IP 用一台同网段的机器访问通过vip访问数据库:
mysql -u dandan-pdandan -h 192.168.1.60 --port 3307 停止master1服务器的mysql,VIP切换到了master2服务器。 在master2上查看:
mysql> showprocesslist;
+----+-------------+--------------------+------+---------+------+-----------------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+--------------------+------+---------+------+-----------------------------------------------------------------------------+------------------+
| 3 | root | localhost | dba | Query | 0 | init | show processlist |
| 14 | systemuser | | NULL |Connect | 247 | Reconnecting after afailed master event read | NULL |
| 15 | systemuser | | NULL |Connect | 207 | Slave has read all relaylog; waiting for the slave I/O thread to update it | NULL |
| 90 |dandan | 192.168.1.60:39995 |dba | Sleep | 8| | NULL |
+----+-------------+--------------------+------+---------+------+-----------------------------------------------------------------------------+------------------+
看到了dandan的连接信息。
|