三、consul-template
Consul-Template是基于Consul的自动替换配置文件的应用。Consul-Template是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行 shell 命令执行更新操作,重新加载 Nginx。
Consul-Template可以查询Consul中的服务目录、Key、Key-values 等。这种强大的抽象功能和查询语言模板可以使 Consul-Template 特别适合动态的创建配置文件。例如:创建Apache/Nginx Proxy Balancers 、 Haproxy Backends等。
安装template在consul服务器
#上传并解压 [root@localhost opt]# unzip consul-template_0.19.3_linux_amd64.zip Archive: consul-template_0.19.3_linux_amd64.zip inflating: consul-template #解压完就是一个可执行文件 [root@localhost opt]# mv consul-template /usr/local/bin/
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 8080; server_name www.stevelu.com; access_log /var/log/nginx/stevelu.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.2 yum安装nginx
#导入nginx.repo源文件到yum.repo.d下 #下载nginx服务 yum install -y nginx systemctl start nginx netstat -natp |grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 58472/nginx: master
3.3 配置并启动 template
#在前台启动 template 服务,启动后不要按 ctrl+c 中止 consul-template 进程。 consul-template --consul-addr 192.168.109.137:8500 \ --template "/opt/consul/nginx.ctmpl:/etc/nginx/conf.d/stevelu-consul.conf:/usr/sbin/nginx -s reload" \ --log-level=info //另外打开一个终端查看生成配置文件 cd /etc/nginx/conf.d/ cat stevelu-consul.conf upstream http_backend { server 192.168.109.133:83; server 192.168.109.133:84; } server { listen 8080; server_name www.stevelu.com; access_log /var/log/nginx/stevelu.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; } }
字段解释
consul-template --consul-addr 192.168.80.15:8500 \
#指定consul的ip和端口号;8500既是UI界面端口也是管理服务端口
–template “/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload” \
#/opt/consul/nginx.ctmpl:模板文件路径
#/usr/local/nginx/conf/vhost/kgc.conf:生成的配置文件的路径
#/usr/local/nginx/sbin/nginx -s reload:重载
–log-level=info
#日志的级别
3.5 测试
#再次在registrator开两个容器 docker run -itd -p:85:80 --name test-05 -h test05 nginx docker run -itd -p:86:80 --name test-06 -h test06 nginx #consul端再次查看文件,两个容器自动添加进来了 cat stevelu-consul.conf upstream http_backend { server 192.168.109.133:83; server 192.168.109.133:84; server 192.168.109.133:85; server 192.168.109.133:86; }
#在nginx容器中的index.html文件中写入不同的数据,测试负载 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES be29e10a1d42 nginx "/docker-entrypoint.…" 8 minutes ago Up 7 minutes 0.0.0.0:86->80/tcp, :::86->80/tcp test-06 14e16a17f6d2 nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 0.0.0.0:85->80/tcp, :::85->80/tcp test-05 41919685ef10 httpd "httpd-foreground" 7 hours ago Up 7 hours 0.0.0.0:89->80/tcp, :::89->80/tcp test-04 f29239f4b1c3 httpd "httpd-foreground" 7 hours ago Up 7 hours 0.0.0.0:88->80/tcp, :::88->80/tcp test-03 1e039dadebfb nginx "/docker-entrypoint.…" 7 hours ago Up 7 hours 0.0.0.0:84->80/tcp, :::84->80/tcp test-02 f3475a9f9055 nginx "/docker-entrypoint.…" 7 hours ago Up 7 hours 0.0.0.0:83->80/tcp, :::83->80/tcp test-01 0d54ff80e159 gliderlabs/registrator:latest "/bin/registrator --…" 7 hours ago Up 7 hours registrator [root@localhost ~]# docker exec -it test-01 bash root@test01:/# cd /usr/share/nginx/html/ root@test01:/usr/share/nginx/html# echo "this is test-01 web" > index.html root@test01:/usr/share/nginx/html# exit exit [root@localhost ~]# docker exec -it test-02 bash root@test02:/# cd /usr/share/nginx/html/ root@test02:/usr/share/nginx/html# echo "this is test-02 web" > index.html root@test02:/usr/share/nginx/html# exit exit [root@localhost ~]# docker exec -it test-05 bash root@test05:/# cd /usr/share/nginx/html/ root@test05:/usr/share/nginx/html# echo "this is test-05 web" > index.html root@test05:/usr/share/nginx/html# exit exit [root@localhost ~]# docker exec -it test-06 bash root@test06:/# cd /usr/share/nginx/html/ root@test06:/usr/share/nginx/html# echo "this is test-06 web" > index.html root@test06:/usr/share/nginx/html# exit exit
四、consul多节点
//添加一台已有docker环境的服务器192.168.109.134/24加入已有的群集中 consul agent \ -server \ -ui \ -data-dir=/var/lib/consul-data \ -bind=192.168.109.134 \ -client=0.0.0.0 \ -node=consul-server02 \ -enable-script-checks=true \ -datacenter=dc1 \ -join 192.168.109.137 &> /var/log/consul.log & ------------------------------------------------------------------------ -enable-script-checks=true :设置检查服务为可用 -datacenter : 数据中心名称 -join :加入到已有的集群中 ------------------------------------------------------------------------