标签:负载均衡 nginx-upsync-module reload nginx
主机名 | IP | 角色 |
---|---|---|
nginx-lb | 10.0.1.5 | 负载均衡 |
node001 | 10.0.1.6 | web服务器 |
node002 | 10.0.1.7 | web服务器 |
说明:
nginx-lb安装nginx和consul服务,作为负载均衡。node001和node002安装nginx作为后端web服务器
# uname -rm 3.10.0-229.el7.x86_64 x86_64 # cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core)
# mkdir /app # mkdir /soft
# yum install -y pcre pcre-devel openssl openssl-devel
# groupadd nginx # useradd -g nginx -M -s /sbin/nologin nginx
# cd /soft/ # git clone https://github.com/weibocom/nginx-upsync-module.git # wget https://codeload.github.com/xiaokai-wang/nginx_upstream_check_module/zip/master # unzip master # wget http://nginx.org/download/nginx-1.10.1.tar.gz # wget https://releases.hashicorp.com/consul/0.8.1/consul_0.8.1_linux_amd64.zip
# cd /soft/ # tar xf nginx-1.10.0.tar.gz # cd nginx-1.10.1 # patch -p0 < /soft/nginx_upstream_check_module-master/check_1.9.2+.patch # ./configure --prefix=/app/nginx-1.10.1 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-pcre --add-module=/soft/nginx-upsync-module/ --add-module=/soft/nginx_upstream_check_module-master/ # make # make install # ln -s /app/nginx-1.10.1/ /app/nginx # cd
# cd /soft/ # unzip consul_0.8.1_linux_amd64.zip # mkdir /app/consul/{conf,bin,,data} -p # cp consul /app/consul/bin/ # cd /app/consul/bin/ # ./consul agent -server -bootstrap-expect=1 -node=nginx-lb -bind=10.0.1.5 -client 0.0.0.0 -ui
# cd /app/nginx-1.10.1/conf/ # cp nginx.conf nginx.conf.ori # vim nginx.conf worker_processes 1; 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"‘ ‘$upstream_addr $upstream_status $upstream_response_time $request_time‘; access_log logs/access.log main; sendfile on; keepalive_timeout 65; upstream test { server 127.0.0.1:11111; upsync 10.0.1.5:8500/v1/kv/upstreams/test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off; upsync_dump_path /app/nginx/conf/servers/servers.conf; } upstream bar { server 10.0.1.6:8080 weight=1 fail_timeout=10 max_fails=3; } server { listen 80; location = / { proxy_pass http://test; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; add_header real $upstream_addr; } location ~ /Content|Scripts/ { proxy_pass http://test; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location = /bar { proxy_pass http://bar; } location = /upstream_show { upstream_show; } location = /upstream_status { stub_status on; access_log off; } } }
# mkdir server # cd
# /app/nginx/sbin/nginx
部署简单的nginx服务很简单,这里就不用大量篇幅进行阐述了。
# curl -X PUT http://10.0.1.5:8500/v1/kv/upstreams/test/10.0.1.6:8080 true # curl -X PUT http://10.0.1.5:8500/v1/kv/upstreams/test/10.0.1.7:8080 true # cat /app/nginx/conf/servers/servers.conf server 10.0.1.6:8080 weight=1 max_fails=2 fail_timeout=10s; server 10.0.1.7:8080 weight=1 max_fails=2 fail_timeout=10s;
# curl -X PUT -d "{\"weight\":2, \"max_fails\":2, \"fail_timeout\":10}" http://10.0.1.5:8500/v1/kv/upstreams/test/10.0.1.6:8080 true # cat /app/nginx/conf/servers/servers.conf server 10.0.1.6:8080 weight=2 max_fails=2 fail_timeout=10s; server 10.0.1.7:8080 weight=1 max_fails=2 fail_timeout=10s;
# curl -X PUT -d "{\"weight\":2, \"max_fails\":2, \"fail_timeout\":10, \"down\":1}" http://10.0.1.5:8500/v1/kv/upstreams/test/10.0.1.6:8080 true # cat /app/nginx/conf/servers/servers.conf server 10.0.1.6:8080 weight=2 max_fails=2 fail_timeout=10s down; server 10.0.1.7:8080 weight=1 max_fails=2 fail_timeout=10s;
# curl -X DELETE http://10.0.1.5:8500/v1/kv/upstreams/test/10.0.1.6:8080 true # cat /app/nginx/conf/servers/servers.conf server 10.0.1.7:8080 weight=1 max_fails=2 fail_timeout=10s;
更多精彩请关注DevOpsView
本文出自 “长街听风人” 博客,请务必保留此出处http://kevinhao.blog.51cto.com/5204735/1917993
标签:负载均衡 nginx-upsync-module reload nginx
原文地址:http://kevinhao.blog.51cto.com/5204735/1917993