keepalived 安装
wgethttp://www.keepalived.org/software/keepalived-1.2.19.tar.gz
yum install gcc gcc-c++ pcre-devel zlib-devel openssl-devel -y
yum –y install kernel-devel kerne 内核开发包
tar -xf keepalived-1.2.19.tar.gz
cp keepalived-1.2.19
./configure --with-kernel-dir=/usr/src/kernels/2.6.32-573.18.1.el6.x86_64/
make make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir -p /etc/keepalived
cp /usr/local/sbin/keepalived /usr/sbin/
配置文件默认不存在 需要手动创建配置文件
cp /usr/local/sbin/keepalived /usr/sbin/
! configuration file for keepalived
#by:v
global_defs {
}
vrrp_script chk_run {
script /root/my.sh #要执行的脚本
interval 2
weight 2
}
# VIP1
vrrp_instance VI_1 {
state MASTER #名称
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 151 #id
priority 102 #优先级
advert_int 1 #检测频率 发送 vrrp包 1秒
authentication {
auth_typepass
auth_pass 111 #验证密码
}
virtual_ipaddress {
192.168.80.6 #虚拟ip 可以写多个
}
track_script {
chk_run
}
}
35,1
backup keepaliced 安装和master一样就不演示了
backup 配置文件
vim /etc/keepalived/keepalived.conf
! configuration file for keepalived
#by:V
global_defs {
}
vrrp_script chk_run {
script "/root/my.sh"
interval 2
weight 2
}
# VIP1
vrrp_instance VI_1 {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 151
priority 100
advert_int 1
authentication {
auth_typepass
auth_pass 1111
}
virtual_ipaddress {
192.168.80.6
}
track_script {
chk_run
}
}
在两台机创建脚本如下
vim my.sh
#!/bin/bash
A=`ps -C mysqld --no-header |wc -l`
if [ $A -eq 0 ];then
service mysqld start
sleep 3
if [ `ps -C mysqld --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
~
cchmod +x my.sh
ip a 查看vip
我们来验证一下 手动停止mysql
/etc/init.d/mysqld stop
需要注释掉脚本 service mysqld start
查看日志
6)
Feb 27 18:18:57 han3 Keepalived_vrrp[12343]: VRRP_Instance(VI_1) sending 0 priority
Feb 27 18:18:57 han3 Keepalived_vrrp[12343]: VRRP_Instance(VI_1) removing protocol VIPs.
Feb 27 18:18:57 han3 Keepalived_healthcheckers[12342]: Netlink reflector reports IP 192.168.80.6 removed
在backup查看日志
itous ARPs on eth0 for 192.168.80.6
Feb 27 18:21:44 han2 Keepalived_healthcheckers[13140]: Netlink reflector reports IP 192.168.80.6 added
Feb 27 18:21:49 han2 Keepalived_vrrp[13141]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.80.6
[root@han2 han]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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_fast state UP qlen 1000
link/ether 00:0c:29:67:cd:68 brd ff:ff:ff:ff:ff:ff
inet 192.168.80.62/24 brd 192.168.80.255 scope global eth0
inet 192.168.80.6/32 scope global eth0
inet6 fe80::20c:29ff:fe67:cd68/64 scope link
valid_lft forever preferred_lft forever
这是vip 已经漂移过来
如果要均衡nginx 或其他服务只需要把脚本改一下即可 如nginx如下
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
service nginx start
sleep 3
if [ `ps -C mysqld --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
本文出自 “秋水无痕” 博客,谢绝转载!
原文地址:http://7456193.blog.51cto.com/7446193/1759387