Nginx + consul + upsync 完成动态负载均衡的方法详解

第一步:安装centos7的基础环境

yum install netstat
  
  netstat -ntlp
  
  ps -ef 可选:[ | grep nginx]
  
  kill -9 pid进程号

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum update
yum install -y yum-utils device-mapper-persistent-data lvm2

yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
yum install wget
yum install unzip

第二步:安装consul

wget -c https://releases.hashicorp.com/consul/1.8.5/consul_1.8.5_linux_amd64.zip

upzip unzip consul_1.8.5_linux_amd64.zip

systemctl stop firewalld

nohup ./consul agent -dev -ui -node=consul-dev -client=192.168.254.134 &

192.168.254.134:8500

jobs -l
kill -9 19854

第三步:安装nginx 并添加upsync模块

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

wget -c http://nginx.org/download/nginx-1.9.9.tar.gz

tar -zxvf nginx-1.9.9.tar.gz

groupadd nginx
useradd -g nginx -s /sbin/nologin nginx

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

cd nginx-1.9.9 

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

upstream myserver {
 server 127.0.0.1:11111;

 upsync 192.168.254.134:8500/v1/kv/upstreams/springbootserver upsync_timeout=3000ms upsync_interval=500ms upsync_type=consul strong_dependency=off;

 upsync_dump_path /usr/local/nginx-1.9.9/conf/upsync_dump.conf;

 }
 
 location / {
 proxy_pass http://myserver;
 index index.html index.htm;
 }

./nginx

第四步:进行测试

curl -X PUT http://192.168.254.134:8500/v1/kv/upstreams/springbootserver/192.168.0.116:8080

curl -X PUT http://192.168.254.134:8500/v1/kv/upstreams/springbootserver/192.168.0.116:8081

http://192.168.254.134:8500/v1/kv/upstreams/springbootserver/192.168.0.116:808

第五步:在consul图形化页面添加key对应的value来指定负载均衡的算法

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

原创文章,作者:网友投稿,如若转载,请注明出处:https://www.cloudads.cn/archives/4170.html

发表评论

登录后才能评论