Nginx + keepalived

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 keepalived
global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   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 keepalived
global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   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/messages
Apr 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 Bytes
Apr 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 STATE
Apr 27 17:47:34 node1 Keepalived_vrrp[2464]: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 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.100
Apr 27 17:47:34 node1 Keepalived_healthcheckers[2463]: Netlink reflector reports IP 172.16.249.100 added
Apr 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 :80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   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 show
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 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/bash
while :
do
nginxpid=`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 4
done


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章