Consul
是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置
Consul的特性
●支持健康检查,允许存储键值对
Consul
●基于Golong语言,可移植性强
支持ACL访问控制
与Docker等轻量级容器可无缝配合
构建自动发现的Docker服务架构
建立Consul服务
- 每个提供服务的节点上都要部署和运行Consul的agent
Consul agent有两种运行模式
Server
Client
Server和Client只是Consul集群层面的区分,与搭建在Cluster上的应用服务无关
consul原理图
部署consul发现自动集群
- consul服务器:20.0.0.19
- node节点服务器:20.0.0.18
在consul服务器上
unzip consul_0.9.2_linux_amd64.zip
mv consul /usr/bin/
consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=20.0.0.19 -client=0.0.0.0 -node=consul-server01 &> /var/log/consul.log &
jobs
[1]+ 运行中 consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=20.0.0.19 -client=0.0.0.0 -node=consul-server01 &>/var/log/consul.log &
consul members
Node Address Status Type Build Protocol DC
consul-server01 20.0.0.19:8301 alive server 0.9.2 2 dc1
在node节点服务器上
docker run -d --name=registrator --net=host -v /var/run/docker.sock:/tmp/docker.sock --restart=always gliderlabs/registrator:latest -ip=20.0.0.18 consul://20.0.0.19:8500
[root@server1 nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
de120e0f521a gliderlabs/registrator:latest "/bin/registrator -i…" 17 minutes ago Up 17 minutes registrator
50d00c942934 compose_nginx_nginx "/run.sh" 2 hours ago Up 2 hours 0.0.0.0:4399->80/tcp, 0.0.0.0:4400->443/tcp compose_nginx_nginx_1
799a7d726ab1 mysql:new "init" 4 hours ago Up 4 hours 0.0.0.0:32780->3306/tcp mysql_server
36dd10a2e6b4 tomcat:centos7 "/usr/local/tomcat8/…" 3 days ago Up 3 days 0.0.0.0:32779->8080/tcp wonderful_williams
使用浏览器查看图形化的网页 验证服务
为template的nginx集群模板
在consul服务器上
vim /root/consul/nginx.ctmpl #编译nginx集群模板
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 88;
server_name localhost 20.0.0.18;
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;
}
}
保存退出
编译安装nginx
yum install gcc gcc-c++ pcre pcre-devel zlib-devel
tar xf nginx-1.12.0.tar.gz -C /opt
cd /opt/nginx-1.12.0
./configure --prefix=/usr/local/nginx #编译安装指定安装位置
make && make install
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
include vhost/*.conf; #新增:指定vhost配置文件位置
default_type application/octet-stream;
保存退出
mkdir /usr/local/nginx/conf/vhost #创建vhost目录
mkdir /var/log/nginx
/usr/local/nginx/sbin/nginx #启动nginx
consul-template -consul-addr 20.0.0.19:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info #开启监听
**自动创建nginx集群配置文件,持续监听 并自动修改**
监听开启后另起终端,查看新建的nginx集群配置文件
如果监听启动失败可以多起几次 如果在运行中失败,检查模板的格式和路径 以及权限问题
docker run -itd -p:82:80 --name nginx-2 -h nginx-2 nginx
docker run -itd -p:83:80 --name nginx-3 -h nginx-3 nginx
docker run -itd -p:83:80 --name nginx-3 -h nginx-3 nginx
vi /usr/local/nginx/conf/vhost/kgc.conf
upstream http_backend {
server 20.0.0.18:82; #通过consul发现的容器服务被自动添加进来了
server 20.0.0.18:83;
server 20.0.0.18:84;
server 20.0.0.18:85;
}
server {
listen 88;
server_name localhost 20.0.0.19;
access_log /var/log/nginx/whh.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;
}
}
验证集群是否成功 使用浏览器访问20.0.0.19:88端口
在查看集群的nginx容器的日志 确认轮询
docker logs -f nginx-1
docker logs -f nginx-2
docker logs -f nginx-3
docker logs -f nginx-5
配置多node节点集群
新起一台拥有完整docker环境和consul的服务器
然后直接输入
consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=20.0.0.17 -client=0.0.0.0 -node=consul-server02 -enable-script-checks=true -datacenter=dc1 -join 20.0.0.19 &> /var/log/consul.log &
jobs查看后台运行状态 必须为运行中
[1]+ 运行中 consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=20.0.0.17 -client=0.0.0.0 -node=consul-server02 -enable-script-checks=true -datacenter=dc1 -join 20.0.0.19 &>/var/log/consul.log &
验证
查看浏览器中的consul图形化界面的node选项
注:此处集群时记得第二台docker服务器重启网卡 配置安装consul 并且启动一个registrator容器,看不到第二台server可以尝试重启
或者直接使用curl 127.0.0.1:8500/v1/catalog/nodes查看群集节点详细信息
然后启动一个nginx服务测试
docker run -itd -p:89:80 --name nginx-9 -h nginx-9 nginx
再多次浏览测试网页20.0.0.19:88
查看nginx-9的日志是否更新
docker logs -f nginx-9
20.0.0.19 - - [03/Dec/2020:07:25:29 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36" "20.0.0.1"
20.0.0.19 - - [03/Dec/2020:07:25:30 +0000] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36" "20.0.0.1"
确认正常轮询
查看集群配置文件