Client
Server 和 Client 只是 Consul 集群层面的区分, 与搭建在 Cluster 之上的应用服务无关
2.consul-template
是基于 Consul 的自动替换配置文件的应用
可以查询 Consul 中的服务目录, Key,Key-values 等
特别适合动态的创建配置文件
3.Gliderlabs/Registrator
检查容器运行状态
自动注册和注销 docker 容器的服务到服务配置中心
目前支持 Consul,Etcd 和 SkyDNS2
实验部署
部署 consul 服务, 实现部署 nginx 服务的自动发现和实现 nginx 的负载均衡服务
一, 构建自动发现的 Docker 服务架构
1. 建立 Consul 服务
- [[email protected] ~]# mkdir consul
- [[email protected] ~]# cp /opt/consul_0.9.2_linux_amd64.zip /root/consul/
- [[email protected] ~]# cd /root/consul/
- [[email protected] consul]# unzip consul_0.9.2_linux_amd64.zip
- [[email protected] consul]# ls
- consul consul_0.9.2_linux_amd64.zip
- [[email protected] consul]# mv consul /usr/bin/
- [[email protected] consul]# consul agent \ // 使用代理功能
- -server \ // 提供 server 功能
- -Bootstrap \ // 参与选举为领袖
- -ui \ // 提供 web 访问界面
- -data-dir=/var/lib/consul-data \ // 参数存储位置
- -bind=192.168.7.129 \ // 绑定地址
- -client=0.0.0.0 \ // 面对所有节点终端
- -node=consul-server01 &> /var/log/consul.log & // 指定本地节点名称, 指定日志文件目录, 放在后台运行
2. 查看集群信息
- [[email protected] consul]# consul members
- Node Address Status Type Build Protocol DC
- consul-server01 192.168.7.129:8301 alive server 0.9.2 2 dc1
- [[email protected] consul]# consul info | grep leader
- leader = true
- leader_addr = 192.168.7.129:8300
3. 通过 httpd API 获取集群信息
- [[email protected] ~]# curl 127.0.0.1:8500/v1/status/peers // 查看集群 server 成员
- [[email protected] ~]# curl 127.0.0.1:8500/v1/status/leader // 查看领导者信息
- [[email protected] ~]# curl 127.0.0.1:8500/v1/catalog/services // 注册的所有服务
- [[email protected] ~]# curl 127.0.0.1:8500/v1/catalog/nginx // 查看 nginx 服务信息
- [[email protected] ~]# curl 127.0.0.1:8500/v1/catalog/nodes // 集群节点详细信息
二, 容器服务自动添加到 nginx 集群
1. 安装 Gliderlabs/Registrator
- [[email protected] ~]# docker run -d --name=registrator \ // 指定名称
- --net=host \ // 指定网络
- -v /var/run/docker.sock:/tmp/docker.sock \ // 指定数据卷
- --restart=always \ //restart 功能
- gliderlabs/registrator:latest -ip=192.168.7.168 \ // 指定本地 IP 地址
- consul://192.168.7.129:8500
2. 添加 nginx 服务
- [[email protected] ~]# docker run -itd -p:83:80 --name test-01 -h test01 nginx
- [[email protected] ~]# docker run -itd -p:84:80 --name test-02 -h test02 nginx
- [[email protected] ~]# docker ps -a
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 960d01d212c4 nginx "nginx -g'daemon of..." 4 seconds ago Up 3 seconds 0.0.0.0:84->80/tcp test-02
- bc075c1c3670 nginx "nginx -g'daemon of..." 59 seconds ago Up 57 seconds 0.0.0.0:83->80/tcp test-01
- 5dd120cd1232 gliderlabs/registrator:latest "/bin/registrator -i..." 3 minutes ago Up 3 minutes registrator
3. 验证 nginx 服务是否注册到 consul
在 consul 服务器上查看服务
- [[email protected] consul]# curl 127.0.0.1:8500/v1/catalog/services
- {
- "consul":[],"nginx":[]
- }
4. 准备 template nginx 模板文件
- [[email protected] ~]# VIM /root/consul/nginx.ctmpl
- upstream http_backend {
- {{range service "nginx"}}
- server {{.Address}}:{{.Port}};
- {{end}}
- }
- server {
- listen 83;
- server_name localhost 192.168.7.129;
- 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;
- }
- }
5. 编译安装并配置 nginx
- [[email protected] ~]# yum install gcc pcre-devel zlib-devel -y
- [[email protected] ~]# tar zxvf nginx-1.12.0.tar.gz -C /opt
- [[email protected] ~]# cd /opt/nginx-1.12.0
- [[email protected] ~]# ./configure --prefix=/usr/local/nginx
- [[email protected] ~]# make && make install
- [[email protected] ~]# VIM /usr/local/nginx/conf/nginx.conf
- http {
- include mime.types;
- include vhost/*.conf; // 添加虚拟主机目录
- default_type application/octet-stream;
- # 创建虚拟主机目录
- [[email protected] ~]# mkdir /usr/local/nginx/conf/vhost
- # 创建日志文件目录
- [[email protected] ~]# mkdir /var/log/nginx
- # 启动 nginx
- /usr/local/nginx/sbin/nginx
6. 配置并启动 template
上传 consul-template_0.19.3_linux_amd64.zip 到 / root/consul 目录下
- [[email protected] consul]# unzip consul-template_0.19.3_linux_amd64.zip
- [[email protected] consul]# mv consul-template /usr/bin
- [[email protected] consul]# consul-template -consul-addr 192.168.7.129:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/aaa.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
- # 启动后, 会在 vhost 目录下自动生成 aaa.conf 配置文件
- [[email protected] ~]# ls /usr/local/nginx/conf/vhost/
- abc.conf
7. 增加一个 nginx 容器节点, 测试服务发现及配置更新功能 (在 registrator 服务端增加
[[email protected] ~]# docker run -itd -p:85:80 --name test-03 -h test03 nginx`
consul 服务器监控会有提示自动更新
来源: http://www.bubuko.com/infodetail-3520479.html