动态负载均衡(Nginx+Consul+UpSync)环境搭建详解程序员

首先 安装好 Consul upsync

然后:

 1、配置安装Nginx

 需要做配置,包括分组之类的,创建目录,有些插件是需要存放在这些目录的 

groupadd nginx

useradd -g nginx -s /sbin/nologin nginx

mkdir -p /var/tmp/nginx/client/

mkdir -p /usr/local/nginx 

 

2、编译Nginx

    cd  /home/nginx/nginx-1.9.0 

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

   

解读:

动态负载均衡(Nginx+Consul+UpSync)环境搭建详解程序员

 

 

 

注意,如果出现这个错误:

动态负载均衡(Nginx+Consul+UpSync)环境搭建详解程序员

解决办法

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+Consul+UpSync)环境搭建详解程序员

 

 然后开始做动态负载均衡了!添加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/

2、 使用postman方式

动态负载均衡(Nginx+Consul+UpSync)环境搭建详解程序员

 

 

 然后图形化界面:

 动态负载均衡(Nginx+Consul+UpSync)环境搭建详解程序员

 

访问:

动态负载均衡(Nginx+Consul+UpSync)环境搭建详解程序员

看看Nginx的本地:

动态负载均衡(Nginx+Consul+UpSync)环境搭建详解程序员

成功了哦

 

 配置权重:

{“weight”:2, “max_fails”:2, “fail_timeout”:10, “down”:0}

   动态负载均衡(Nginx+Consul+UpSync)环境搭建详解程序员

他可以实现轮训,故障转移哦。默认的~ 

 看这里:

动态负载均衡(Nginx+Consul+UpSync)环境搭建详解程序员

被同步过来了额

动态负载均衡(Nginx+Consul+UpSync)环境搭建详解程序员

 

  

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/6453.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论