架构拓扑
主机名 | 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
nginx-lb上部署服务
安装依赖
# 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
安装nginx
# 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
启动consul
# 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
配置nginx
# 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
启动nginx
# /app/nginx/sbin/nginx
查看consul页面
部署后端服务
部署简单的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;
查看consul页面
调整权重
# 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
时间: 2024-10-08 03:18:27