consul是HashiCorp公司推出使用go语言编写的开源工具,用于实现分布式系统的服务发现与配置。
具有如下特性:
上方拓扑图是基于Docker完成的,然后将consul、consul template、registrator和nginx组装成一个值得信任且可扩展的服务框架,此架构可以灵活的、不需要重启任何服务、不需要重写任何配置的添加和移除服务。
consul template:配置文件模板
registrator:注册机制
consul server:consul服务
例如:当后方增加了一个容器时,容器会注册registrator,registrator发现增加了一个容器的时候,会通知consul server要更新,consul server使用consul template模板更新
实验环境:
两台 CentOS 7.6机器
consul主机节点:20.0.0.5 Docker-ce、Consul
nginx服务器:20.0.0.6 Docker-ce、registrator
两个节点都安装Docker-ce,注意防火墙不要关闭,容器转发需要iptables,docker的安装可以翻看我前几天的博客
点这里!
[root@consul ~]# mkdir /consul
[root@consul ~]# cd /consul/
[root@consul consul]# rz -E
rz waiting to receive.
[root@consul consul]# ls
consul_0.9.2_linux_amd64.zip
[root@consul consul]# unzip consul_0.9.2_linux_amd64.zip
Archive: consul_0.9.2_linux_amd64.zip
inflating: consul
[root@consul consul]# chmod +x consul
[root@consul consul]# mv consul /usr/bin
[root@consul consul]# consul agent \ ##代理设置
> -server \ ##服务功能
> -bootstrap \ ##参与选举
> -ui \ ##web的ui界面
> -data-dir=/var/lib/consul-data \ ##提供一个代理存储的数据目录
> -bind=20.0.0.5 \ ##本机地址
> client=0.0.0.0 \ ##客户端监听地址,四个0表示监听全部
> -node=consul-server01 &> /var/log/consul.log& ##consul-server01是节点名称,日志混合输出到log,且放入后台运行
[root@consul consul]# jobs
[1]+ 运行中 consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=20.0.0.5 -client=0.0.0.0 -node=consul-server01 &>/var/log/consul.log &
[root@consul consul]# consul members ##查看群集信息
Node Address Status Type Build Protocol DC
consul-server01 20.0.0.5:8301 alive server 0.9.2 2 dc1
[root@consul consul]# consul info ##查看详细信息
……太多了,略!……
[root@consul consul]# consul info | grep leader ##查看leader管理信息
leader = true
leader_addr = 20.0.0.5:8300
[root@nginxser ~]# docker run -d \
> --name=registrator \ ##设置容器名称
> --net=host \ ##设置网络
> -v /var/run/docker.sock:/tmp/docker.sock \ ##指定数据卷位置
> --restart=always
> gliderlabs/registrator:latest \ ##定义
> -ip=20.0.0.6 \
> consul://20.0.0.5:8500
[root@nginxser ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ffff4acdc46 gliderlabs/registrator:latest "/bin/registrator -i…" About a minute ago Up About a minute registrator
##创建容器,用来测试服务发现功能
[root@nginxser ~]# docker run -itd -p 81:80 --name nginx1 -h nginx01 nginx
d33186b2b8f4321420fb9eb27d5a8c99304452e2bd97c09cacb2ea665a349e31
[root@nginxser ~]# docker run -itd -p 82:80 --name nginx2 -h nginx02 nginx
438bc47f6e4884b1f684aa02c24ca95a79eb6e72ae750a7f34cf7d414a438417
[root@nginxser ~]# docker run -itd -p 83:80 --name httpd1 -h httpd01 httpd
1267a529f9f844b9ef670fa449f4e439bc441420850aa88e4e65c8e36b1467c7
[root@nginxser ~]# docker run -itd -p 84:80 --name httpd2 -h httpd02 httpd
9afe6bf4e4f98d13daa5031a5c7b6bf28d3dc112d795ab267657d12b6ca9d405
[root@nginxser ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9afe6bf4e4f9 httpd "httpd-foreground" About a minute ago Up About a minute 0.0.0.0:84->80/tcp httpd2
1267a529f9f8 httpd "httpd-foreground" About a minute ago Up About a minute 0.0.0.0:83->80/tcp httpd1
438bc47f6e48 nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 0.0.0.0:82->80/tcp nginx2
d33186b2b8f4 nginx "/docker-entrypoint.…" 13 minutes ago Up 13 minutes 0.0.0.0:81->80/tcp nginx1
0ffff4acdc46 gliderlabs/registrator:latest "/bin/registrator -i…" 16 minutes ago Up 16 minutes registrator
compose-template是一个守护进程,用于实时查询consul集群信息
并且运维人员可以在consul服务器上,定制任意的模板,生成配置文件
compose可以查询consul中的服务目录、key、key-values等
这种强大的抽象功能和查询语言模板使得consul-template特别适合动态的创建配置文件,也就是自动化增加服务
例如:创建apache/nginx proxy balacers、haproxy backends
[root@consul consul]# rz -E
rz waiting to receive.
[root@consul consul]# ls
consul_0.9.2_linux_amd64.zip consul-template_0.19.3_linux_amd64.zip
[root@consul consul]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@consul consul]# ls
consul_0.9.2_linux_amd64.zip consul-template consul-template_0.19.3_linux_amd64.zip
[root@consul consul]# mv consul-template /usr/bin
[root@consul consul]# chmod +x /usr/bin/consul-template
此模板用于nginx反向代理模板
nginx.ctmpl跟nginx没有直接关系,
consul是docker的一种自动管理机制
nginx.ctmpl中的参数以变量的形式写入
[root@consul consul]# mkdir /var/log/nginx
[root@consul consul]# vim nginx.ctmpl
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 3080;
server_name localhost 20.0.0.5;
access_log /var/log/nginx/tang.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;
}
}
[root@consul consul]# yum -y install gcc gcc-c++ make expat* pcre* perl* zlib*
[root@consul consul]# rz -E
rz waiting to receive.
[root@consul consul]# ls
consul_0.9.2_linux_amd64.zip consul-template_0.19.3_linux_amd64.zip nginx-1.12.2.tar.gz nginx.ctmpl
[root@consul consul]#
[root@consul consul]# tar zxvf nginx-1.12.2.tar.gz
[root@consul nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[root@consul nginx-1.12.2]# make && make install
[root@consul consul]# mkdir /usr/local/nginx/conf/vhost
[root@consul consul]# cd /usr/local/nginx/conf/vhost/
[root@consul vhost]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
include vhost/*.conf; ##添加配置文件路径
[root@consul vhost]# /usr/local/nginx/sbin/nginx ##nginx!!启动!!biu
!biu!!
[root@consul vhost]# netstat -ntap |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 81209/nginx: master
##指定模板路径,/consul/nginx.ctmpl,生成到/usr/locla/nginx/conf/vhost/juejue.conf,然后重载nginx -s reload
[root@consul vhost]# consul-template -consul-addr 20.0.0.5:8500 \
> -template "/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/juejue.conf:/usr/local/nginx/sbin/nginx -s reload" \
> --log-level=info
[root@consul ~]# cd /usr/local/nginx/conf/vhost/
[root@consul vhost]# ls
juejue.conf
[root@consul vhost]# cat juejue.conf
upstream http_backend {
server 20.0.0.6:81;
server 20.0.0.6:82;
}
server {
listen 3080;
server_name localhost 192.168.233.132;
access_log /var/log/nginx/tang.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;
}
}
[root@nginxser ~]# docker logs -f nginx1
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
20.0.0.5 - - [23/Sep/2020:11:45:46 +0000] "GET / HTTP/1.0" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "20.0.0.1"
[root@nginxser ~]# docker run -itd -p 85:80 --name nginx3 -h nginx03 nginx
250aca04091349216897a6aa7222bdb4f481fedf9d1312965d70b430df97e33d
[root@consul vhost]# cd /usr/local/nginx/conf/vhost/
[root@consul vhost]# ls
juejue.conf
[root@consul vhost]# cat juejue.conf
upstream http_backend {
server 20.0.0.6:81;
server 20.0.0.6:82;
server 20.0.0.6:85; ##多了一条
}
server {
listen 3080;
server_name localhost 20.0.0.5;
access_log /var/log/nginx/tang.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;
}
}