nginx+upstream+keepalived實現負載平衡

來源:互聯網
上載者:User

標籤:nginx+keepalived

IP地址規劃

 前端伺服器

master 192.168.1.112

slave  192.168.1.113

節點伺服器

       192.168.1.114

       192.168.1.115

1、開啟主備伺服器的upstream模組 在http模組下添加配置分發節點

    upstream aaa.xftz.cn{

        server 192.168.1.114;

        server 192.168.1.115;

                }

 upstream bbb.xftz.cn{

        server 192.168.1.114;

        server 192.168.1.115;

                }

 

server {

        listen       80;

       server_name  aaa.xftz.cn;

  location / {

                proxy_pass      http://aaa.xftz.cn;

                proxy_next_upstream  http_500 http_502 http_503 error timeout invalid_header;

                include /home/webserver/nginx/conf/proxy.conf;

   }

}

server {

        listen  80;

       server_name  bbb.xftz.cn;

        location / {

                proxy_pass      http://bbb.xftz.cn;

                proxy_next_upstream  http_500 http_502 http_503 error timeout invalid_header;

                include /home/webserver/nginx/conf/proxy.conf;

}

                include /home/webserver/nginx/conf/proxy.conf  //include指令包含進來一個proxy設定檔

}

proxy.conf設定檔內容

proxy_redirect  off;

proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;

proxy_set_header X-RealIP $remote_addr;

proxy_set_header Host $host;

client_body_buffer_size 128k;

client_max_body_size    8m;

proxy_connect_timeout   30s;

proxy_send_timeout      1m;

proxy_read_timeout      1m;

proxy_temp_file_write_size 1024m;

proxy_buffer_size         32k;

proxy_buffers             4 32k;

proxy_busy_buffers_size 64k;

#proxy_set_header(設定header)

#proxy_hide_header(隱藏header)

#proxy_pass_header(通過header)

X-Forwarded-For X-RealIP 分別設定變數Forwarded-For設定的變數作用為後端伺服器可以擷取客戶的真實訪問IP通過哪個Proxy 伺服器訪問的後端伺服器 X-RealIP設定的變數表示後端伺服器可以擷取到使用者的真實IP

#proxy_connect_timeout(代理連線逾時)

#proxy_send_timeout(代理髮送逾時)

#proxy_read_timeout(代理接收逾時)

#proxy_temp_file_write_size(設定快取檔案夾大小)

#proxy_buffer_size(代理緩衝大小)

#proxy_buffers (代理緩衝)

#proxy_busy_buffers_size(高負荷下緩衝大小)

#proxy_ignore_client_abort(不允許代理端主動關閉串連)

2、slave伺服器113的配置與主一樣

3、節點伺服器114和115配置一致

server {

        listen       80;

       server_name  aaa.xftz.cn;

        access_log  /home/httpd/logs/nginx/aaa.xftz.cn-access.log  main;

        error_log   /home/httpd/logs/nginx/aaa.xftz.cn-error.log debug ;

        root /home/httpd/aaa.xftz.cn/;

       index index.html index.php;

                                 }

server {

        listen       80;

       server_name  bbb.xftz.cn;

        access_log  /home/httpd/logs/nginx/bbb.xftz.cn-access.log  main;

        error_log   /home/httpd/logs/nginx/bbb.xftz.cn-error.log debug ;

        root /home/httpd/bbb.xftz.cn/;

       index index.html index.php;

        }

在後端節點伺服器的nginx設定檔裡添加日誌格式

log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

                      ‘$status $body_bytes_sent "$http_referer" ‘

                      ‘"$http_user_agent"  $proxy_add_x_forwarded_for "$http_x_forwarded_for"‘;

4、添加host檔案測試訪問

192.168.1.112aaa.xftz.cn

192.168.1.112bbb.xftz.cn

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/5B/80/wKioL1UKq6Hww0jLAAEC9QBZ40w369.jpg" style="float:none;" title="1.png" alt="wKioL1UKq6Hww0jLAAEC9QBZ40w369.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/5B/86/wKiom1UKqnfBU9VBAAEQA35HWes785.jpg" style="float:none;" title="2.png" alt="wKiom1UKqnfBU9VBAAEQA35HWes785.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/5B/80/wKioL1UKq6HSYy9WAAERWxjAxsI167.jpg" style="float:none;" title="3.png" alt="wKioL1UKq6HSYy9WAAERWxjAxsI167.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/5B/86/wKiom1UKqneAU1AXAAETzBIZLTA234.jpg" title="4.png" alt="wKiom1UKqneAU1AXAAETzBIZLTA234.jpg" />


5、查看伺服器日誌

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/5B/86/wKiom1UKxlnwVcvSAAL3BWaWofQ460.jpg" style="float:none;" title="1.png" alt="wKiom1UKxlnwVcvSAAL3BWaWofQ460.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/5B/81/wKioL1UKx4SxWetKAAKWmxUmTHk889.jpg" style="float:none;" title="2.png" alt="wKioL1UKx4SxWetKAAKWmxUmTHk889.jpg" />

這裡upstream的配置完成。


安裝Keepalived

tar zxvf keepalived-1.1.19.tar.gz

cd keepalived-1.1.19

./configure --sysconf=/etc

make && make install

修改設定檔/etc/keepalived/keepalived.conf

! Configuration File for keepalived


global_defs {

   router_id LVS_DEVEL

}

vrrp_script chk_nginx {                //監控nginx指令碼

                script "/etc/init.d/nginx.sh"

                interval 2

                weight 2

}

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

track_script {   

    chk_nginx

}

track_interface {

     eth0

     }

    virtual_ipaddress {

        192.168.1.200

    }

}


virtual_server 192.168.1.200 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

 persistence_timeout 50

    protocol TCP


    real_server 192.168.1.114 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

        real_server 192.168.1.115 80 {

        weight 1

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

}

}

}

備伺服器的設定檔和主的差別在紅色部分。

建立nginx監控檔案

vim /etc/init.d/nginx.sh

#!/bin/bash

netstat -anpt | grep nginx

if [ $? != 0 ]

then

/home/webserver/nginx/sbin/nginx

sleep 3

netstat -anpt | grep nginx

if [ $? != 0 ]

then

/etc/init.d/keepalived stop

fi

fi

只在主配置即可 

測試keepalived 關閉nginx

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/5B/9A/wKiom1UNTBWR07pZAAL26z7cuYE915.jpg" title="1.png" alt="wKiom1UNTBWR07pZAAL26z7cuYE915.jpg" />到此配置完成

nginx+upstream+keepalived實現負載平衡

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.