基於Consul+Upsync+Nginx實現動態負載均衡


基於Consul+Upsync+Nginx實現動態負載均衡

1、Consul環境搭建

    下載consul_0.7.5_linux_amd64.zip到/usr/local/src目錄

cd /usr/local/src
wget https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip

     

解壓consul_0.7.5_linux_amd64.zip

需要先確認是否安裝了unzip,如果沒有安裝先安裝zip unzip

yum install -y zip unzip
mkdir /usr/local/consul
unzip consul_0.7.5_linux_amd64.zip -d /usr/local/consul

     

啟動consul

/usr/local/consul/consul agent -dev -ui -node=consul-dev -client=192.168.100.121   

     (此ip為當前服務器的局域網ip,請勿配置公網ip)

   

臨時關閉防火牆    

systemctl stop firewalld

     測試:瀏覽器訪問192.168.100.121:8500

2、安裝nginx1.9.10以上版本,並且安裝nginx-upsync-module模塊

    nginx-upsync-module是新浪微博開源插件,在此作用為:

    拉取 consul 的后端 KV的列表,並更新 Nginx 的路由信息。此模塊不依賴於任何第三方模塊。

    consul 作為 Nginx 的 db,利用 consul 的 KV 服務,每個 Nginx work 進程獨立的去拉取各個upstream 的配置,並更新各自的路由信息從而達到動態負載均衡,不去重啟nginx。

下載nginx: 

wget http://nginx.org/download/nginx-1.9.10.tar.gz

   

下載nginx-upsync-module: 

wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip

     

解壓nginx-upsync-module 

unzip master.zip

     

解壓nginx

tar -zxvf nginx-1.9.10.tar.gz

 

配置nginx前置環境目錄

  groupadd nginx

   useradd -g nginx -s /sbin/nologin nginx

   mkdir -p /var/tmp/nginx/client

   mkdir -p /usr/local/nginx

 

    編譯Nginx 

cd /usr/local/src/nginx-1.9.10

./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=/usr/local/src/nginx-upsync-module-master



編譯的是報錯
./configure: error: SSL modules require the OpenSSL library.
解決辦法
yum -y install openssl openssl-devel

make
&& make instal

 

創建持久化日志目錄:

mkdir -p /usr/local/nginx/conf/servers/

 

配置nginx.conf文件:

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream product{
        #用於心跳檢測
        server 127.0.0.1:11111;
        #去consul的KV拉取上游服務器信息
        upsync 192.168.100.121:8500/v1/kv/upstreams/itchao 
                   upsync_timeout=6m upsync_interval=500ms 
                   upsync_type=consul strong_dependency=off;
        #存放拉取到的上游服務器信息,/usr/local/nginx/conf/servers  這個目錄需要創建
        upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
           # root   html;
           #反向代理配置
           proxy_pass http://product;
           index  index.html index.htm;
        }
    }
}

進入consul管理端創建KV服務信息

也可以通過postman添加集群服務器信息:

PUT 請求:

http://192.168.199.10:8500/v1/kv/upstreams/product/192.168.100.1:8080

1.使用linux命令方式發送put請求
curl -X PUT http://192.168.199.10:8500/v1/kv/upstreams/product/192.168.100.1:8080
curl -X PUT http://192.168.199.10:8500/v1/kv/upstreams/product/192.168.100.1:8081


2.使用postmen 發送put請求
http://192.168.199.10:8500/v1/kv/upstreams/product/192.168.100.1:8080
http://192.168.199.10:8500/v1/kv/upstreams/product/192.168.100.1:8081

192.168.100.1:8080   代表需要負載的上游服務器地址以及端

 

查看從consul拉取的上游服務器信息:

cat /usr/local/nginx/conf/servers/servers_test.conf

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM