当后面容器增加时,registrator发现并注册容器中的服务—》通知consul server更新—》consul template模板进行更新,自动修改nginx.conf中的upstream参数
Registrator简介:
Registrator监控新建的Docker容器,并且检查判定这些容器提供的服务。从我们的目的出发,任何监听在某个端口的程序都是服务。Registrator发现在容器内发现的任务服务,都将被添加到一个服务注册端,比如Consul或etcd
1.consul服务器中安装consul
[root@localhost ~]# mkdir /root/consul
上传consul_0.9.2_linux_amd64.zip软件到/root/consul中
[root@localhost ~]# cp consul_0.9.2_linux_amd64.zip consul
[root@localhost ~]# cd consul/
[root@localhost consul]# ls
consul_0.9.2_linux_amd64.zip
[root@localhost consul]# unzip consul_0.9.2_linux_amd64.zip
[root@localhost consul]# mv consul /usr/bin/
[root@localhost consul]# consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=14.0.0.20 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
#bootstrap:来自 Twitter,是目前最受欢迎的前端框架
#ui:通过网页访问
[root@localhost consul]# consul members
Node Address Status Type Build Protocol DC
consul-server01 14.0.0.20:8301 alive server 0.9.2 2 dc1
[root@localhost consul]# consul info | grep leader
leader = true
leader_addr = 14.0.0.20:8300
2.通过httpd api获取集群信息
[root@localhost consul]# curl 127.0.0.1:8500/v1/status/peers #查看集群server成员
["14.0.0.30:8300"]
[root@localhost consul]# curl 127.0.0.1:8500/v1/status/leader #集群leader
"14.0.0.30:8300"
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/services #注册的所有服务
{"consul":[]}
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/nginx #查看nginx服务信息
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/nodes #集群节点详细信息
[{"ID":"3405c908-c58a-fe44-5cf8-f6b63fe80371","Node":"consul-server01","Address":"14.0.0.30","Datacenter":"dc1","TaggedAddresses":{"lan":"14.0.0.30","wan":"14.0.0.30"},"Meta":{},"CreateIndex":5,"ModifyIndex":6}]
1.安装gliderlabs/registrator
可检查容器运行状态自动注册,还可注销docker容器的服务到服务配置中心
目前支持consul、etcd和SkyDNS2
在14.0.0.30服务器,执行以下操作:
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=14.0.0.30 \
consul://14.0.0.20:8500
2.测试发现服务的功能是否正常
docker run -itd -p:83:80 --name test-01 -h test01 nginx
docker run -itd -p:84:80 --name test-02 -h test02 nginx
docker run -itd -p:88:80 --name test-03 -h test03 httpd
docker run -itd -p:89:80 --name test-04 -h test04 httpd
#回到consul节点查看nginx是否被自动注册
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"nginx":[]}
3.验证nginx服务是否注册到consul,浏览器输入http://14.0.0.20:8500
点击“NODES”,再点击“consul-server01”,会出现自动添加的两个nginx服务
4.准备template nginx模板文件,参数以变量形式写入
在consul服务器节点上操作
[root@localhost consul]# vim /root/consul/nginx.ctmpl
upstream http-server {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 83;
server_name localhost 14.0.0.20;
access_log /var/log/nginx/test-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-server;
}
}
5.Nginx编译安装
#安装环境依赖包
yum -y install gcc gcc-c++ make pcre-devel zlib-devel
#创建运行用户、组
useradd -M -s /sbin/nologin nginx
#编译安装
tar zxf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
#以便管理员直接执行“nginx”命令就可以调用Nginx的主程序
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#测试语法
nginx -t
6.配置nginx
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
include vhost/*.conf; #添加虚拟主机目录
default_type application/octet-stream;
#创建虚拟主机目录
[root@localhost ~]# mkdir /usr/local/nginx/conf/vhost
#创建日志文件目录
[root@localhost ~]# mkdir /var/log/nginx
#启动nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx
7.配置并启动template
consul-template是一个守护进程,用于实时查询consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成后,可以选择运行shell命令执行更新操作,重新加载nginx。consul-template可以查询consul中的服务目录、key、key-values等。这种强大的抽象功能和查询语言模板可以使consul-template特别适合动态的创建配置文件。例如:创建nginx反向代理。
上传consul-template_0.19.3_linux_amd64.zip包到/root目录下
[root@localhost ~]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@localhost ~]# mv consul-template /usr/bin/
[root@localhost ~]# consul-template -consul-addr 14.0.0.20:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/test.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
#指定模板文件:/root/consul/nginx.ctmpl,映射到/usr/local/nginx/conf/vhost/test.conf,然后通过“/usr/local/nginx/sbin/nginx -s reload”进行重载
8.增加一个nginx容器节点,测试服务发现及配置更新功能
#在registrator服务端注册
[root@localhost ~]# docker run -itd -p:85:80 --name test-05 -h test05 nginx
#在consul服务器监控会提示自动更新
2020/09/23 10:22:21.760332 [INFO] (runner) initiating run
2020/09/23 10:22:21.761907 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2020/09/23 10:22:21.761926 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2020/09/23 10:22:21.761986 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
#查看/usr/local/nginx/conf/vhost/test.conf
#查看三台nginx容器日志,请求是否正常轮询到各个容器节点上(开三个终端查看轮询效果)
[root@localhost ~]# docker logs -f test-01
[root@localhost ~]# docker logs -f test-02
[root@localhost ~]# docker logs -f test-05