实现keepalived
准备工作
配置好nginx负载后
主辅nginx配置需一样
keepalived 下载地址:http://www.keepalived.org/download.html 
keepalived安装
两台机器上同样安装keepalived,需调整配置文件:
 tar zxvf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure --prefix=/usr/local/keepalived --sysconf=/etc
make && make install
  
默认的配置文件会产生在 /etc/keepalived/
 chkconfig keepalived on
/usr/local/keepalived/sbin/keepalived 启动
 
修改主服务模式keepalived
 #vi /etc/keepalived/keepalived.conf
修改为:
! Configuration File for keepalived 
global_defs { 
   notification_email { 
   linuxemail@126.com
        } 
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30 
   router_id LVS_DEVEL 
} 
vrrp_instance VI_1 { 
    state MASTER                    #主服务器模式
    interface eth0 
    virtual_router_id 51 
    #mcast_src_ip 192.168.64.190    #<==nginxIP地址 
    priority 100   #要求主服务器值大于辅值
    advert_int 1 
    authentication { 
        auth_type PASS 
        auth_pass chtopnet 
    } 
    virtual_ipaddress { 
        192.168.64.192                    # <==vip地址 
    } 
} 
 #/usr/local/keepalived/sbin/keepalived
启动keepalived
#ip addr
查看网口
 
修改辅服务模式keepalived
 #vi /etc/keepalived/keepalived.conf
修改为:
! Configuration File for keepalived 
global_defs { 
   notification_email { 
   linuxemail@126.com
        } 
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30 
   router_id LVS_DEVEL 
} 
vrrp_instance VI_1 { 
    state BACKUP       #修改为BACKUP模式
    interface eth0 
    virtual_router_id 51 
    #mcast_src_ip 192.168.64.191    #<==nginxIP地址 
    priority 90                                           #值要求比主服务值小
    advert_int 1 
    authentication { 
        auth_type PASS                        #认证方式和密码需同主 一致
        auth_pass chtopnet 
    } 
    virtual_ipaddress { 
        192.168.64.192                    # <==vip地址 
    } 
} 
 #/usr/local/keepalived/sbin/keepalived
启动keepalived
#ip addr
查看网口 
监控nginx进程
为保证keepalived实时工作可以写一个监控nginx进程脚本:
1.         Cat  /usr/local/keepalived/monitor_nginx.sh
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
 /usr/local/nginx/sbin/nginx
 sleep 5
 if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
 then
 killall keepalived
 fi
fi
 后台执行: nohup /bin/bash /root/monitor_nginx.sh &
 或者:
2.         Cat /usr/local/keepalived/nginx_pid.sh
    #!/bin/bash 
    while  : 
    do 
     nginxpid=`ps -C nginx --no-header | wc -l` 
     if [ $nginxpid -eq 0 ];then 
      /usr/local/nginx/sbin/nginx 
      sleep 5 
       if [ $nginxpid -eq 0 ];then 
       /etc/init.d/keepalived stop 
       fi 
     fi 
     sleep 5  
    done
 后台执行: nohup /bin/bash /root/nginx_pid.sh &
验证keepalived
浏览器访问:http://192.168.64.192
关掉主服务器nginx keepalived进程
能继续访问表示keepalived配置成功。
可以查看日志 /var/log/messages
主服务器日志:
Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
avahi-daemon[3048]: Registering new address record for 192.168.64.192 on eth0.
进程关闭后 查看辅服务器日志:
Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
进入MASTER模式。
恢复主服务器的nginxkeepalived
VRRP_Instance(VI_1) Received higher prio advert
Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE