Consul 是一个支持多数据中心分布式高可用的 服务发现 和 配置共享 的服务软件,由 HashiCorp 公司用 Go 语言开发,基于 Mozilla Public License 2.0 的协议进行开源。
主要特性
Consul 支持健康检查,并允许 HTTP 、GRPC 和 DNS 协议调用 API 存储键值对。
一致性协议采用 Raft 算法,用来保证服务的高可用。 使用 GOSSIP 协议管理成员和广播消息, 并且支持 ACL 访问控制。
consul的实例叫agent,agent有两种运行模式:server和client 。
官方建议每个Consul Cluster数据中心至少有3个或以上的运行在Server Mode的Agent,Client节点不限。
使用场合
Docker 容器的注册与配置共享
Coreos 实例的注册与配置共享
SaaS 应用的配置共享、服务发现和健康检查。
vitess 集群
与 confd 服务集成,动态生成 nginx 和 haproxy 配置文件
consul服务部署
主机 | 主机IP | 安装工具 |
consul服务器 | 192.168.110.10 | docker、consul、consul-template模板、NGINX代理服务 |
容器服务器 | 192.168.110.20 | docker、registrator、NGINX容器 |
部署consul服务
上传文件到目录中
解压consul_0.9.2_linux_amd64.zip
[root@server1 consul]# unzip consul_0.9.2_linux_amd64.zip
Archive: consul_0.9.2_linux_amd64.zip
inflating: consul
使 consul 命令可以在任何目录下识别
[root@server1 consul]# mv consul /usr/bin
[root@server1 consul]# consul agent \
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.110.10 \
> -clint=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &
[1] 15492
consul服务已经启动
查看群集信息
consul members
consul info |grep leader
容器服务自动加入nginx集群
在容器服务器上安装 gliderlabs/registrator
检查容器运行状态
自动注册和注销docker容器的服务到服务配置中心
[root@server2 ~]# docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.110.20 \
consul://192.168.110.10:8500
创建容器
[root@server2 ~]# docker run -dit -p:83:80 --name tom1 -h tom-01 nginx
[root@server2 ~]# docker run -dit -p:84:80 --name tom2 -h tom-02 nginx
浏览器访问:http://192.168.110.10:8500
安装consul-template工具
Consul-Template是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上系统上任意数量的指定模板,生成配置文件。更新完成后,可以选择允许shell命令执行更新,重新加载Nginx。Consul-Template可以查询Consul中的服务目录,Key、Key-Values等,这种抽象的功能和查询语言模板可以使Consul-Template特别适合动态的创建配置文件
准备template NGINX 模板文件
在consul上操作
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 83;
server_name localhost 192.168.110.10;
access_log /var/log/nginx/tom.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-Fprwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
编译安装一个nginx服务【做反向代理】
安装环境
[root@server1 ~]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y
解压缩
[root@server1 opt]# tar zxvf nginx-1.12.2.tar.gz
编译安装
[root@server1 opt]# cd nginx-1.12.2/
[root@server1 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[root@server1 nginx-1.12.2]# make && make install
优化路径
[root@server1 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
配置NGINX
[root@server1 nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
创建虚拟主机目录
[root@server1 ~]# mkdir /usr/local/nginx/conf/vhost
创建日志文件目录
mkdir /var/log/nginx
启动NGINX
[root@server1 ~]# /usr/local/nginx/sbin/nginx
[root@server1 ~]# netstat -anpt |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 18625/nginx: master
配置并启动template
[root@server1 consul]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@server1 consul]# mv consul-template /usr/local/bin
启动服务,进入监控状态,再打开一个终端
[root@server1 consul]# consul-template -consul-addr 192.168.110.10:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/tom.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
添加一个容器
[root@server2 ~]# docker run -dit -p:89:80 --name tom4 -h tom-04 nginx
223fa623e2de62d36ed2cd16e6f5c9f3f1d55afc7312ad930e9c632f3aa30998
去网页查看
验证consul-template的更新功能,在新的终端上查看
刷新几次NGINX首页,查看日志
原文:https://www.cnblogs.com/liuwei186/p/14070479.html