**
持续集成、自动化部署、弹性伸缩教程 http://edu.youkuaiyun.com/course/detail/6452
**
1:下载对应安装包
nginx-upsync-module对nginx的版本有相关要求
我们这里采用的是1.8x的版本
wget http://nginx.org/download/nginx-1.8.0.tar.gz
wget https://releases.hashicorp.com/consul/0.9.2/consul_0.9.2_linux_amd64.zip
下载完成后解压如下:
2:安装对应安装包
安装nginx依赖包,编译安装nginx并且启动
yum install zlib pcre pcre-devel openssl openssl-devel zlib-devel git
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --add-module=/root/nginx-upsync-module-nginx-upsync-1.8.x
make && make install
/usr/local/nginx/sbin/nginx
安装consul并启动
由于我们采用的是测试模式,所以dev模式启动就行了
cd consul/
cp consul /usr/local/bin
nohup consul agent -advertise=192.168.150.10 -client=0.0.0.0 -dev &
nginx配置文件如下:
user root;
worker_processes 2;
error_log logs/error.log warn;
events {
worker_connections 4096;
multi_accept on;
use epoll;
}
http {
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
access_log logs/access.log main;
sendfile on;
tcp_nodelay on;
keepalive_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;
charset UTF-8;
gzip on;
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 6;
gzip_types ext/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
include /usr/local/nginx/conf/conf.d/*;
fastcgi_intercept_errors on;
upstream hellowh.war {
server 127.0.0.1:11111;
upsync 192.168.150.10:8500/v1/kv/upstreams/hellowh.war/ upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
upsync_dump_path /usr/local/nginx/conf/servers/hellowh.war.conf;
}
server {
listen 80 ;
server_name test01.th.com;
charset utf8;
location /HelloWH {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://hellowh.war/HelloWH;
}
access_log /usr/local/nginx/logs/access-upsync.log;
location /stub-status {
stub_status on;
}
location = /upstream_show {
upstream_show;
}
}
}
操作命令:
添加一个后端服务器
# default: weight=1 max_fails=2 fail_timeout=10 down=0 backup=0;
curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port
示例:
curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://192.168.150.10:8500/v1/kv/upstreams/hellowh.war/192.168.140.195:8082
删除一个后端服务器
curl -X DELETE http://$consul_ip:$port/v1/kv/upstreams/$upstream_name/$backend_ip:$backend_port
示例:
curl -X DELETE http://192.168.150.10:8500/v1/kv/upstreams/hellowh.war/192.168.140.195:8082
修改权重
#default: weight=1 max_fails=2 fail_timeout=10 down=0 backup=0;
curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10}' http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port
示例:
curl -X PUT -d '{"weight":10, "max_fails":20, "fail_timeout":11}' http://192.168.150.10:8500/v1/kv/upstreams/hellowh.war/192.168.140.195:8082
停止后端提供服务,使后端服务停止提供服务,但是没被删除
curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10, "down":1}' http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port
示例
curl -X PUT -d '{"weight":10, "max_fails":20, "fail_timeout":11,"down":1 }' http://192.168.150.10:8500/v1/kv/upstreams/hellowh.war/192.168.140.195:8082
consul CHECK操作:
curl http://$consul_ip:$port/v1/kv/upstreams/$upstream_name?recurse
curl http://192.168.150.10:8500/v1/kv/upstreams/hellowh.war/192.168.140.195:8082
示例
查看upstream信息,nginx配置文件里面定义的路径查看既可以