餐饮网站方案,实惠的网站建设公司,做网站推销话术,群晖wordpress 站点目录 一.Docker consul的介绍
1.1template模板(更新)
1.2registrator#xff08;自动发现#xff09; 1.3agent(代理) 二.consul的工作原理 三.Consul的特性 四.Consul的使用场景 五.搭建Consul的集群
5.1需求 5.2部署consul
5.3主服务器[192.168.40.20]
5.4client部署自动发现 1.3agent(代理) 二.consul的工作原理 三.Consul的特性 四.Consul的使用场景 五.搭建Consul的集群
5.1需求 5.2部署consul
5.3主服务器[192.168.40.20]
5.4client部署192.168.40.21 5.5配置template模板自动更新 六.测试访问代理服务器
七 consul 多节点 一.Docker consul的介绍
1.1template模板(更新)
先发现再更新发现的是后端节点上容器的变化registrator更新的是nginx配置文件agent
1.2registrator自动发现
是consul安插在docker容器里的眼线用于监听监控节点上容器的变化增加或减少或者宕机一旦有变化会把这些信息告诉并注册在consul server端使用回调和协程的方式所以它的延迟和资源消耗会很少consul server发生一旦发生注册列表的变化后会把注册的信息告诉agent。后端每构建出一个容器会向registrator进行注册控制consul 完成更新操作consul会触发consul template模板进行热更新 1.3agent(代理)
用来控制consul template模板用template组件去和nginx.conf来进行对接模板里全是变量用变量的方式去加载后端由注册到consul server端之后server端会把信息告诉agent,agent和template进行对接写入templatetemplate就有了镜像更新完之后会作为nginx.conf子配置文件被前端的nginx识别consul agent会控制reload之后会识别nginx.conf配置文件中的变化相当于识别后端的节点就可以在地址池中动态调整自己后端资源。 总结:核心机制consul 自动发现、自动更新为容器提供服务添加、删除、生命周期 二.consul的工作原理
数据流向1、registrator用于监控返现和注册到consul服务内2、consul服务存储reg注册的变化的容器信息然后变化信息以参数的方式传给template3、template会接收参数然后将参数改为具体的upstream的配置放入ng的子配置文件中1.conf最后nginx -s reload 三.Consul的特性
支持健康检查允许存储键值对基于Golong语言可移植性强支持ACL访问控制 四.Consul的使用场景
consul的应用场景包括服务发现服务隔离服务配置
服务发现场景中consul作为注册中心服务地址被注册到consul中以后可以使用consul提供的dns、http接口查询consul支持health check。服务隔离场景中consul支持以服务为单位设置访问策略能同时支持经典的平台和新兴的平台支持tls证书分发service-to-service加密。服务配置场景中consul提供key-value数据存储功能并且能将变动迅速地通知出去借助Consul可以实现配置共享需要读取配置的服务可以从Consul中读取到准确的配置信息。Consul可以帮助系统管理者更清晰的了解复杂系统内部的系统架构运维人员可以将Consul看成一种监控软件也可以看成一种资产资源管理系统。 五.搭建Consul的集群
建立consul服务
每个提供服务的节点上都要部署和运行Consul的agent Consul agent有两种运行模式
ServerClient Server和Client只是Consul集群层面的区分与搭建在Cluster之上的应用服务无关
5.1需求
使用 Docker 将 Consul、Consul template、Registrator 与 Nginx 组成一个值得新人且可扩展的服务架构添加或者移除服务时不需要重写任何配置也不需要重启任何服务一切都能够正常运行以实现自动化运维 5.2部署consul
主服务器192.168.40.20 Docker-ce、Compose 3、Consul、Consul-template
服务器192.168.40.21 Docker-ce、registrator
5.3主服务器[192.168.40.20]
#两台节点上都安装 Docker-ce记得关闭防火墙
systemctl stop firewalld systemctl disable firewalld
setenforce 0 sed -i s/SELINUX*/SELINUXdisabled/g /etc/selinux/config在主节点上部署consul
mkdir /opt/consul
cd /opt/consul
rz consul_0.9.2_linux_amd64.zipunzip consul_0.9.2_linux_amd64.zipmv consul /usr/local/bin consul agent \
-server \
-bootstrap \
-ui \
-data-dir/var/lib/consul-data \
-bind192.168.40.20 \
-client0.0.0.0 \
-nodeconsul-server01 /var/log/consul.log consul members
consul info | grep leader 查看集群server成员curl 127.0.0.1:8500/v1/status/peers集群Raf leadercurl 127.0.0.1:8500/v1/status/leader注册的所有服务curl 127.0.0.1:8500/v1/catalog/services查看nginx服务信息curl 127.0.01:8500/v1/catalog/nginx集群节点详细信息curl 127.0.0.1:8500:/v1/cata;pg/nodes
5.4client部署192.168.40.21
容器服务自动加入nginx集群
1、安装Gliderlabs/Registrator Gliderlabs/Registrator
可检查容器运行状态自动注册还可注销docker容器的服务 到服务配置中心
目前支持Consul、Etcd和SkyDNS2在192.168.40.21节点上执行以下操作docker run -d \
--nameregistrator \
--nethost \
-v /var/run/docker.sock:/tmp/docker.sock \
--restartalways \
gliderlabs/registrator:latest \
-ip192.168.40.21 \
consul://192.168.40.20:8500systemctl restart docker
docker run -itd -p:81:80 --name test-01 -h test01 nginx
docker run -itd -p:82:80 --name test-02 -h test02 nginx
docker run -itd -p:83:80 --name test-03 -h test03 httpd
docker run -itd -p:84:80 --name test-04 -h test04 httpd 真机访问http://192.168.40.20:8500 此时应该可以发现5个服务 5.5配置template模板自动更新 server192.168.40.20
Consul-Template是一个守护进程用于实时查询Consul集群信息并更新文件系统上任意数量的指定模板生成配置文件更新完成以后可以查询Consul中的服务目录Key、Key-values等。
cd consul/
vim nginx.ctmplupstream http_backend {{{range service nginx}}server {{.Address}}:{{.Port}};{{end}}
}server {listen 100;server_name localhost 192.168.40.20;access_log /var/log/nginx/lic.com-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;}
} yum -y install gcc pcre-devel zlib-devel
rz nginx-1.12.2.tar.gz
tar zxvf nginx-1.12.2.tar.gz -C /opt
cd /opt/nginx-1.12.2./configure --prefix/usr/local/nginxmake make install cd /usr/local/nginx/conf/
mkdir vhost
mkdir /var/log/nginx/usr/local/nginx/sbin/nginxcd /opt
rz consul-template_0.19.3_linux_amd64.zipunzip consul-template_0.19.3_linux_amd64.zip mv consul-template /usr/bin #将可指向文件移动到路径环境变量中便于系统识别consul-template -consul-addr 192.168.40.20:8500 -template /opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/hehe.conf:/usr/local/nginx/sbin/nginx -s reload --log-levelinfo 此时在192.168.40.21上操作
docker run -itd -p:85:80 --name test-05 -h test05 nginx六.测试访问代理服务器
是否可以完成代理访问轮询主服务器192.168.40.20
docker logs -f test-01
docker logs -f test-02
docker logs -f test-05 七 consul 多节点
#添加一台已有docker环境的服务器192.168.40.22加入已有的群集中consul agent \
-server \
-bootstrap \
-ui \
-data-dir/var/ib/consul-data \
-bind192.168.40.22 \
-client0.0.0.0 \
-nodeconsul-server02 \
-enable-script-checkstrue \
-datacenterdc1 \
-join 192.168.40.20 /var/log/consul.log --解释--
-enable-script-checkstrue: 设置检查服务为可用
-datacenter: 数据中心名称
-join: 加入到已有的集群中