结合keepalived实现nginx反向代理群集高可用



192.168.56.104

/etc/keepalived/keepalived.conf


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
global_defs {
        router_id nginx-proxy-ha
}
vrrp_script check_nginx {
        script "/home/lhb/sh/check_nginx.sh"         ###监控脚本
                interval 2                             ###监控时间
                weight 2                                ###目前搞不清楚
}
vrrp_instance VI_1{
        state MASTER                            ### 设置为 主
                interface eth0                             ### 监控网卡
                virtual_router_id 51                    ### 这个两台服务器必须一样
                priority 101                                 ### 权重值 MASTRE 一定要高于 BAUCKUP
                authentication {
                        auth_type PASS             ### 加密
                        auth_pass eric                ### 加密的密码,两台服务器一定要一样,不然会出错
                }
        track_script {
                check_nginx                     ### 执行监控的服务
        }
        virtual_ipaddress {
                192.168.56.200                           ###    VIP 地址
        }
}


192.168.56.107

/etc/keepalived/keepalived.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
global_defs {
        router_id nginx-proxy-ha
}
vrrp_script check_nginx {
                script "/home/lhb/sh/check_nginx.sh"
                interval 2
                weight 2
}
vrrp_instance VI_1 {
        state BACKUP                                ### 设置为 辅机
        interface eth0
        virtual_router_id 51                        ### 与 MASTRE 设置 值一样
        priority 100                                     ### 比 MASTRE权重值 低
        authentication {
                     auth_type PASS
                     auth_pass eric                    ### 密码 与 MASTRE 一样
        }
       track_script {
               check_nginx
       }
        virtual_ipaddress {
                 192.168.56.200
        }
}


/home/lhb/sh/check_nginx.sh

1
2
3
4
5
6
7
8
9
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`                ## 查看是否有 nginx进程 把值赋给变量A
if [ $A -eq 0 ];then                                         ## 如果没有进程值得为 零
   /usr/local/openresty/nginx/sbin/nginx
   sleep 3
   if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
      killall keepalived                        ## 则结束 keepalived 进程
   fi
fi


192.168.56.104、192.168.56.107上nginx配置:

/usr/local/openresty/nginx/conf/nginx.conf


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#user  nobody;
worker_processes  1;
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       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  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    gzip               on;
    gzip_min_length    1k;
    gzip_buffers  4 1024k;
    gzip_http_version 1.1;
    gzip_comp_level     6;
    gzip_types        text/plain application/x-javascript text/css application/xml;
    #gzip_vary          on;
    proxy_hide_header Vary;
    proxy_connect_timeout    600;
    proxy_read_timeout       600;
    proxy_send_timeout       600;
    proxy_buffer_size        16k;
    proxy_buffers            4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    upstream backendserver {
        #ip_hash;
        server   192.168.56.105:80;
        server   192.168.56.106:80;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            index  index.html index.htm;
            proxy_pass         http://backendserver;
            proxy_set_header   Host             $host;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_504;
            proxy_set_header   X-Forwarded-For        $remote_addr;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}