Master Nginx:nginx configure file:

user              nginx;worker_processes  4;error_log  /var/log/nginx/error.log;#error_log  /var/log/nginx/error.log  notice;#error_log  /var/log/nginx/error.log  info;pid        /var/run/nginx.pid;events {    worker_connections  1024;}http {    include       /etc/nginx/mime.types;    default_type  application/octet-stream;    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';    access_log  /var/log/nginx/access.log  main;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    # Load config files from the /etc/nginx/conf.d directory    # The default server is in conf.d/default.conf    upstream backend        {        ip_hash;        server  172.16.249.74:80;        server 172.16.249.75:80 ;}        server {        listen 80;        server_name www.firefox.com;        location / {        root    /var/www/html;        index index.php index.html index.htm;        proxy_redirect off;        proxy_set_header Host $host;        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X-Formarded-For proxy_add_x_forwarded_for;        proxy_pass http://backend;}        location /nginx {        access_log off;        stub_status on;}          log_format  access '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';                        access_log /data/logs/access.log access;}}

Master keepalived: configure file:

! Configuration File for keepalivedglobal_defs {   notification_email {     firefox@main.com   }   notification_email_from Alexandre.Cassen@firewall.loc   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    priority 100    mcast_src_ip 172.16.249.32    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        172.16.249.100    }}

slave keepalived: configure configure file

! Configuration File for keepalivedglobal_defs {   notification_email {     firefox@main.com   }   notification_email_from Alexandre.Cassen@firewall.loc   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    priority 99    mcast_src_ip 172.16.249.64    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        172.16.249.100    }}

查看日志信息:

[root@node1 keepalived]# tail /var/log/messagesApr 27 17:47:32 node1 Keepalived_vrrp[2464]: Using LinkWatch kernel netlink reflector...Apr 27 17:47:32 node1 Keepalived_vrrp[2464]: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)]Apr 27 17:47:32 node1 Keepalived_healthcheckers[2463]: Configuration is using : 7273 BytesApr 27 17:47:32 node1 Keepalived_healthcheckers[2463]: Using LinkWatch kernel netlink reflector...Apr 27 17:47:33 node1 Keepalived_vrrp[2464]: VRRP_Instance(VI_1) Transition to MASTER STATEApr 27 17:47:34 node1 Keepalived_vrrp[2464]: VRRP_Instance(VI_1) Entering MASTER STATEApr 27 17:47:34 node1 Keepalived_vrrp[2464]: VRRP_Instance(VI_1) setting protocol VIPs.Apr 27 17:47:34 node1 Keepalived_vrrp[2464]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.249.100Apr 27 17:47:34 node1 Keepalived_healthcheckers[2463]: Netlink reflector reports IP 172.16.249.100 addedApr 27 17:47:39 node1 Keepalived_vrrp[2464]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.249.100

查看nginx 服务进程

# lsof  -i :80COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAMEnginx   1995  root    7u  IPv4  12113      0t0  TCP *:http (LISTEN)nginx   1997 nginx    7u  IPv4  12113      0t0  TCP *:http (LISTEN)nginx   1998 nginx    7u  IPv4  12113      0t0  TCP *:http (LISTEN)nginx   1999 nginx    7u  IPv4  12113      0t0  TCP *:http (LISTEN)nginx   2000 nginx    7u  IPv4  12113      0t0  TCP *:http (LISTEN)

查看ip地址是否分配:

[root@node1 keepalived]# ip addr show1: 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 00:0c:29:ed:5e:11 brd ff:ff:ff:ff:ff:ff inet 172.16.249.32/16 brd 172.16.255.255 scope global eth0 inet 172.16.249.100/32 scope global eth0 inet6 fe80::20c:29ff:feed:5e11/64 scope link

编写nginx服务监控脚本:

nohup  /bin/bash /root/nginx.sh

#!/bin/bashwhile :donginxpid=`ps -C nginx --no-header | wc -l`if [ $nginxpid -eq 0 ]; then        /usr/sbin/nginx  # 尝试启动        sleep 3        nginxpid=`ps -C nginx --no-header | wc -l`        echo $nginxpid        if [ $nginxpid -eq 0 ]; then        /etc/rc.d/init.d/keepalived stop # 实在启动不了;将keepalived服务关闭        fi        fi        sleep 4done