3.3 consul-template
Consul-Template是基于Consul的自动替换配置文件的应用。Consul-Template是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行 shell 命令执行更新操作,重新加载 Nginx。
Consul-Template可以查询Consul中的服务目录、Key、Key-values 等。这种强大的抽象功能和查询语言模板可以使 Consul-Template 特别适合动态的创建配置文件。例如:创建Apache/Nginx Proxy Balancers 、 Haproxy Backends等。
3.3.1 准备 template nginx 模板文件
//在consul服务器上操作 vim /opt/consul/nginx.ctmpl #定义nginx upstream一个简单模板 upstream http_backend { {{range service "nginx"}} server {{.Address}}:{{.Port}}; {{end}} } #定义一个server,监听8000端口,反向代理到upstream server { listen 8000; server_name localhost 192.168.147.105; access_log /var/log/nginx/zhangsan.com-access.log; #修改日志路径 index index.html index.php; location / { proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Client-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://http_backend; } }
3.3.2 编译安装nginx
yum -y install pcre-devel zlib-devel gcc gcc-c++ make useradd -M -s /sbin/nologin nginx tar zxvf nginx-1.12.0.tar.gz -C /opt/ cd /opt/nginx-1.12.0/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make -j2 && make install ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
3.3.3 配置 nginx
vim /usr/local/nginx/conf/nginx.conf ...... http { include mime.types; include vhost/*.conf; #添加虚拟主机目录 default_type application/octet-stream; ...... //创建虚拟主机目录 mkdir /usr/local/nginx/conf/vhost //创建日志文件目录 mkdir /var/log/nginx //启动nginx nginx
3.3.4 配置并启动 template
unzip consul-template_0.19.3_linux_amd64.zip -d /opt/ cd /opt/ mv consul-template /usr/local/bin/ //在前台启动 template 服务,启动后不要按 ctrl+c 中止 consul-template 进程。 consul-template --consul-addr 192.168.147.105:8500 \ --template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/zhangsan.conf:/usr/local/nginx/sbin/nginx -s reload" \ --log-level=info
//另外打开一个终端查看生成配置文件 cat /usr/local/nginx/conf/vhost/zhangsan.conf upstream http_backend { server 192.168.147.106:83; server 192.168.147.106:84; } server { listen 8000; server_name 192.168.147.105; access_log /var/log/nginx/kgc.cn-access.log; index index.html index.php; location / { proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Client-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://http_backend; } }
3.3.5 访问 template-nginx
docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fe9d5e134efd httpd "httpd-foreground" 54 minutes ago Up 54 minutes 0.0.0.0:89->80/tcp, :::89->80/tcp test-04 65413265259b httpd "httpd-foreground" 54 minutes ago Up 54 minutes 0.0.0.0:88->80/tcp, :::88->80/tcp test-03 ca2baf553f2e nginx "/docker-entrypoint.…" 54 minutes ago Up 54 minutes 0.0.0.0:84->80/tcp, :::84->80/tcp test-02 97c361e14951 nginx "/docker-entrypoint.…" 54 minutes ago Up 54 minutes 0.0.0.0:83->80/tcp, :::83->80/tcp test-01 c43854d28645 gliderlabs/registrator:latest "/bin/registrator --…" About an hour ago Up About an hour registrator
docker exec -it 97c361e14951 bash echo "this is test1 web" > /usr/share/nginx/html/index.html docker exec -it ca2baf553f2e bash echo "this is test2 web" > /usr/share/nginx/html/index.html 浏览器访问:http://192.168.147.105:8000/,并不断刷新。
3.3.6 增加一个 nginx 容器节点
增加一个 nginx 容器节点,测试服务发现及配置更新功能。
增加一个 nginx 容器节点,测试服务发现及配置更新功能。 docker run -itd -p:85:80 --name test-05 -h test05 nginx 观察 template 服务,会从模板更新/usr/local/nginx/conf/vhost/zhangsan.conf 文件内容,并且重载 nginx 服务。
查看/usr/local/nginx/conf/vhost/zhangsan.conf 文件内容
查看/usr/local/nginx/conf/vhost/zhangsan.conf 文件内容 cat /usr/local/nginx/conf/vhost/zhangsan.conf upstream http_backend { server 192.168.147.106:83; server 192.168.147.106:84; server 192.168.147.106:85; }
查看三台 nginx 容器日志,请求正常轮询到各个容器节点上
查看三台 nginx 容器日志,请求正常轮询到各个容器节点上 docker logs -f test-01 docker logs -f test-02 docker logs -f test-05
3.4 consul 多节点
//添加一台已有docker环境的服务器192.168.147.107/24加入已有的群集中(需要安装consul) consul agent \ -server \ -ui \ -data-dir=/var/lib/consul-data \ -bind=192.168.147.107 \ -client=0.0.0.0 \ -node=consul-server02 \ -enable-script-checks=true \ -datacenter=dc1 \ -join 192.168.147.105 &> /var/log/consul.log & -enable-script-checks=true :设置检查服务为可用 -datacenter : 数据中心名称 -join :加入到已有的集群中
consul members Node Address Status Type Build Protocol DC consul-server01 192.168.147.105:8301 alive server 0.9.2 2 dc1 consul-server02 192.168.147.107:8301 alive server 0.9.2 2 dc1 consul operator raft list-peers Node ID Address State Voter RaftProtocol Node ID Address State Voter RaftProtocol consul-server01 192.168.147.105:8300 192.168.147.105:8300 leader true 2 consul-server02 192.168.147.107:8300 192.168.147.107:8300 follower true 2