動態負載均衡(Nginx+Consul+UpSync)環境搭建

首先 安裝好 Consul upsynchtml

而後:node

 一、配置安裝Nginxlinux

 須要作配置,包括分組之類的,建立目錄,有些插件是須要存放在這些目錄的 nginx

groupadd nginx服務器

useradd -g nginx -s /sbin/nologin nginxapp

mkdir -p /var/tmp/nginx/client/負載均衡

mkdir -p /usr/local/nginx curl

 

二、編譯Nginxtcp

    cd  /home/nginx/nginx-1.9.0 post

./configure   --prefix=/usr/local/nginx   --user=nginx   --group=nginx   --with-http_ssl_module   --with-http_flv_module   --with-http_stub_status_module   --with-http_gzip_static_module   --with-http_realip_module   --http-client-body-temp-path=/var/tmp/nginx/client/   --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi   --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre --add-module=/home/upsync/nginx-upsync-module-master

   

解讀:

 

 

 

注意,若是出現這個錯誤:

解決辦法

yum -y install openssl openssl-devel

 

最後: make && make install

 

 


 

下面開始配置Nginx了

 Upstream(上游服務器) 動態配置

##動態去consul 獲取註冊的真實反向代理地址

upstream toov5{

#本身虛擬出的 做爲監聽用的 端口號 固定死的
server 127.0.0.1:11111;

#鏈接consul server動態獲取upstream配置負載均衡信息 間隔0.5秒讀取一次  使用192.168.212.134:8500 原生的 他本身虛擬出來的 不要亂改!  讀取的是toov5 這個分組的!!!
upsync 192.168.91.5:8500/v1/kv/upstreams/toov5 upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;

#動態拉取consulServer相關負載均衡配置信息持久化到硬盤上
upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
}
server {
listen 80;
server_name localhost;
location / {

#配置反向代理
proxy_pass http://toov5;
index index.html index.htm;

}

}

 

解讀:

upsync指令指定從consul哪一個路徑拉取上游服務器配置;upsync_timeout配置從consul拉取上游服務器配置的超時時間;upsync_interval配置從consul拉取上游服務器配置的間隔時間;upsync_type指定使用consul配置服務器;strong_dependency配置nginx在啓動時是否強制依賴配置服務器,若是配置爲on,則拉取配置失敗時nginx啓動一樣失敗。upsync_dump_path指定從consul拉取的上游服務器後持久化到的位置,這樣即便consul服務器出問題了,本地還有一個備份。

注意:替換 consul 註冊中心地址

建立upsync_dump_path

mkdir /usr/local/nginx/conf/servers/

upsync_dump_path指定從consul拉取的上游服務器後持久化到的位置,這樣即便consul服務器出問題了,本地還有一個備份。

 

配置時候的狀態:

#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;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

##動態去consul 獲取註冊的真實反向代理地址

upstream toov5{

#本身虛擬出的 做爲監聽用的 端口號 固定死的
server 127.0.0.1:11111;

#鏈接consul server動態獲取upstream配置負載均衡信息 間隔0.5秒讀取一次 
upsync 192.168.91.5:8500/v1/kv/upstreams/toov5 upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;

#動態拉取consulServer相關負載均衡配置信息持久化到硬盤上
upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
}


server {
listen 80;
server_name localhost;


location / {
#配置反向代理
proxy_pass http://toov5;
index index.html index.htm;

}

}





#    server {
#        listen       80;
#        server_name  localhost;

#        #charset koi8-r;

#        #access_log  logs/host.access.log  main;

#        location / {
#            root   html;
#            index  index.html index.htm;
#        }
#
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
#        error_page   500 502 503 504  /50x.html;
#        location = /50x.html {
#            root   html;
#        }
#
#    }



}

  

  

 

而後啓動consul :  ./consul agent -dev -ui -node=consul-dev -client=192.168.91.5 

啓動 Nginx    ./nginx

 

本地啓動三臺:8080 8081 8082

 

 而後開始作動態負載均衡了!添加nginx  Upstream服務開始:

1.使用linux命令方式發送put請求  添加這個 192.168.8.159:8081 到上游服務

curl -X PUT http://192.168.91.5:8500/v1/kv/upstreams/toov5/192.168.8.159:80812\

二、 使用postman方式

 

 

 而後圖形化界面:

 

 

訪問:

看看Nginx的本地:

成功了哦

 

 配置權重:

{"weight":2, "max_fails":2, "fail_timeout":10, "down":0}

   

他能夠實現輪訓,故障轉移哦。默認的~ 

 看這裏:

被同步過來了額

相關文章
相關標籤/搜索