环境:
主机:192.168.102.50
备机:192.168.102.101
vip:192.168.102.30
- 安装keepalived
tar xzf keepalived-1.2.24.tar.gzcd keepalived-1.2.24./configure --prefix=/usr/local/keepalivedmake && make installmkdir /etc/keepalivedcp keepalived.conf /etc/keepalived/keepalived.confcp check_nginx.sh /etc/keepalived/check_nginx.shcp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/cp /usr/local/keepalived/sbin/keepalived /usr/sbin/chkconfig --level 2345 keepalived on #注册开机启动
- keepalived.conf
! Configuration File for keepalivedglobal_defs { notification_email { ninglong@twsm.com.cn } notification_email_from ninglong@twsm.com.cn smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL_jjrb_prtal vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0}vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight -5 fall 3 rise 2 }vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 61 priority 100 mcast_src_ip 172.10.101.12 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.10.101.21 } track_script { chk_nginx }}
- check_nginx.sh
#!/bin/bashcounter=$(ps -C nginx --no-heading|wc -l)if [ "${counter}" = "0" ]; then /usr/local/bin/nginx sleep 2 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then /etc/init.d/keepalived stop fifi
然后在192.168.102.50上
[root@jjrb ~]# ip a1: lo: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 forever2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 8c:dc:d4:4e:73:58 brd ff:ff:ff:ff:ff:ff inet 192.168.102.50/24 brd 192.168.102.255 scope global eth0 inet 192.168.102.30/24 scope global secondary eth0 inet6 fe80::8edc:d4ff:fe4e:7358/64 scope link valid_lft forever preferred_lft forever[root@jjrb ~]#
在192.168.102.101上
[root@jjrb1 ~]# ip a1: lo: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 forever2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 50:65:f3:32:4a:48 brd ff:ff:ff:ff:ff:ff inet 192.168.102.101/24 brd 192.168.102.255 scope global eth0 inet6 fe80::5265:f3ff:fe32:4a48/64 scope link valid_lft forever preferred_lft forever[root@jjrb1 ~]#
停掉50上的keepalived,再查看vip是否转移到101上来,如果转移成功,则配制成功了。另如果主备机上都出现了vip则考虑在防火墙里添加
-A INPUT -d 224.0.0.18/32 -i bond0 -j ACCEPT-A OUTPUT -d 224.0.0.18/32 -o bond0 -j ACCEPT