天天看點

基于Consul+Upsync+Nginx實作動态負載均衡

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 (IP 位址為本地位址)

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

臨時關閉防火牆 

systemctl stop firewalld      

浏覽器通路192.168.100.121:8500

使用PostMan 注冊Http服務:​​http://192.168.100.121:8500/v1/catalog/register​​

{"Datacenter": "dc1",
 "Node":"tomcat", "Address":"192.168.5.165","Service": {"Id" :"192.168.5.165:8080", "Service": "product","tags": ["dev"], "Port": 8080}}      

Datacenter指定資料中心,Address指定服務IP,Service.Id指定服務唯一辨別,Service.Service指定服務分組,Service.tags指定服務标簽(如測試環境、預發環境等),Service.Port指定服務端口。

nginx-upsync-module簡介

Upsync是新浪微網誌開源的基于Nginx實作動态配置的三方子產品。Nginx-Upsync-Module的功能是拉取Consul的後端server的清單,并動态更新Nginx的路由資訊。此子產品不依賴于任何第三方子產品。Consul作為Nginx的DB,利用Consul的KV服務,每個Nginx Work程序獨立的去拉取各個upstream的配置,并更新各自的路由。

安裝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

./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=../nginx-upsync-module-master
make && make install      

編譯的是報錯

./configure: error: SSL modules require the OpenSSL library.

解決辦法

yum -y install openssl openssl-devel


make && make instal      

Upstream 動态配置

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;
        }
    }
}      
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   代表需要負載的上遊伺服器位址以及端      

繼續閱讀