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

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

一、Consul環境搭建html

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

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.ziplinux

須要先確認是否安裝了unzip,若是沒有安裝先安裝zip unzipnginx

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

     

啓動consulgit

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

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

   

臨時關閉防火牆    後端

systemctl stop firewalld

     測試:瀏覽器訪問192.168.100.121:8500瀏覽器

二、安裝nginx1.9.10以上版本,而且安裝nginx-upsync-module模塊bash

    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
相關文章
相關標籤/搜索