環境:python
主機:192.168.102.50nginx
備機:192.168.102.101bash
vip:192.168.102.30oop
tar xzf keepalived-1.2.24.tar.gz cd keepalived-1.2.24 ./configure --prefix=/usr/local/keepalived make && make install mkdir /etc/keepalived cp keepalived.conf /etc/keepalived/keepalived.conf cp check_nginx.sh /etc/keepalived/check_nginx.sh cp /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 #註冊開機啓動
! Configuration File for keepalived global_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 } }
#!/bin/bash counter=$(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 fi fi
而後在192.168.102.50上 code
[root@jjrb ~]# ip a 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_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上router
[root@jjrb1 ~]# ip a 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_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則考慮在防火牆裏添加server
-A INPUT -d 224.0.0.18/32 -i bond0 -j ACCEPT -A OUTPUT -d 224.0.0.18/32 -o bond0 -j ACCEPT