目录
3、以下在zwb_docker执行(服务器自动加入cosul集群)
4、在zwb_nginx端安装nginx服务,手工编译,之前博客都有
5、测试:增加一个 nginx 容器节点,测试服务发现及配置更新功能
一、consul介绍
Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其它分布式服 务注册与发现的方案,Consul 的方案更“一站式”,内置了服务注册与发现框架、分布一致性协议实 现、健康检查、Key/Value 存储、多数据中心方案,不再需要依赖其它工具(比如 ZooKeeper 等)。使用起来也较 为简单。Consul 使用 Go 语言编写,因此具有天然可移植性(支持Linux、windows和 Mac OS X);安装包仅包含一个可执行文件,方便部署,与 Docker 等轻量级容器可无缝配合。
当tomcat4服务器加入进来时(变更时),consul的组件便会采集到数据变化(consul启动时,会丢一个小弟在应用层的节点上,进行监听工作),首先,小弟先把04先注册,并且汇报给consul,
consul-agent会把变更信息展示在web页面中,然后template模板会进行更新配置,在给与NGXIN。NGINX通过reload更新配置。
二、consul的特性
1、服务发现
2、监控检查
3、key/value存储
4、多数据中心
5、基于Raft算法
6、支持http和dns协议接口
三、consul部署
1、实验环境
主机IP | 安装服务 |
zwb_docker/192.168.159.68 | docker-ce、registrator(自动发现、注册的组件) |
zwb_nginx/192.168.159.159 | nginx、consul、consul-template |
2、主机名为nginx端的配置
①、上传安装包到opt下
[root@zwb_nginx opt]# ls
consul_0.9.2_linux_amd64.zip
②、解压
[root@zwb_nginx opt]# unzip consul_0.9.2_linux_amd64.zip
③、把执行文件剪切到/usr/bin下
[root@zwb_nginx opt]# mv consul /usr/bin/
④、consul代理的配置,启动防止后台执行
[root@zwb_nginx opt]# consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.159.159 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
[1] 22356
⑤、查看集群信息
[root@zwb_nginx opt]# consul members
查看端口
或者这样查看:
[root@zwb_nginx opt]# consul info | grep leader
查看节点信息
[root@zwb_nginx opt]# curl 127.0.0.1:8500/v1/catalog/nodes
⑥、登录web页面查看
3、以下在zwb_docker执行(服务器自动加入cosul集群)
①、安装Gliderlabs/Registrator,可检测容器运行状态自动注册,还可注销docker容器的服务到服务配置中心。
[root@zwb_docker harbor]# docker run -d \
> --name=registrator \
> --net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> --restart=always \
> gliderlabs/registrator:latest \
> -ip=192.168.159.68 \
> consul://192.168.159.159:8500
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.159.68 \
consul://192.168.159.159:8500
②、测试服务是否正常 、运行两台nginx和两台阿帕奇
docker run -itd -p:83:80 --name test-01 -h test01 nginx
docker run -itd -p:84:80 --name test-02 -h test02 nginx
docker run -itd -p:88:80 --name test-03 -h test03 httpd
docker run -itd -p:89:80 --name test-04 -h test04 httpd
查看web页面
4、在zwb_nginx端安装nginx服务,手工编译,之前博客都有。
①、修改nginx.conf文件内容
创建vhost目录:
[root@zwb_nginx conf]# mkdir vhost
②、创建一个consul目录
[root@zwb_nginx opt]# mkdir consul
创建模板
[root@zwb_nginx consul]# vim nginx.ctmpl
upstream http_backend {
{
{range service "nginx"}}
server {
{.Address}}:{
{.Port}};
{
{end}}
}
server {
listen 86;
server_name localhost 192.168.159.68;
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;
}
}
[root@zwb_nginx consul]# nginx # 开启nginx服务
③、上传consul-template到consul目录下
[root@zwb_nginx consul]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@zwb_nginx consul]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
④、移动位置
[root@zwb_nginx consul]# mv consul-template /usr/bin/
⑤、关联nginx虚拟目录中的子配置文件操作
consul-template -consul-addr 192.168.159.159:8500 \
-template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/ceshi.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
[root@zwb_nginx consul]# consul-template -consul-addr 192.168.159.159:8500 -template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/ceshi.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
2022/10/23 17:22:13.717713 [INFO] consul-template v0.19.3 (ebf2d3d)
2022/10/23 17:22:13.717735 [INFO] (runner) creating new runner (dry: false, once: false)
2022/10/23 17:22:13.718242 [INFO] (runner) creating watcher
2022/10/23 17:22:13.718477 [INFO] (runner) starting
2022/10/23 17:22:13.718502 [INFO] (runner) initiating run
2022/10/23 17:22:13.720804 [INFO] (runner) initiating run
2022/10/23 17:22:13.721707 [INFO] (runner) rendered "/opt/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/ceshi.conf"
2022/10/23 17:22:13.721729 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/opt/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/ceshi.conf"
2022/10/23 17:22:13.721789 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
复制会话,打开另个界面进行查看,ceshi.conf文件已经生成
[root@zwb_nginx consul]# cd /usr/local/nginx/conf/vhost/
[root@zwb_nginx vhost]# ls
ceshi.conf
[root@zwb_nginx consul]# cat /usr/local/nginx/conf/vhost/ceshi.conf
upstream http_backend {
server 192.168.159.68:83;
server 192.168.159.68:84;
}
server {
listen 86;
server_name localhost 192.168.159.68;
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;
}
}
访问web页面,跳转完成
5、测试:增加一个 nginx 容器节点,测试服务发现及配置更新功能
在zwb_docker节点增加一个nginx
docker run -itd -p 85:80 --name test-05 -h test05 nginx
自动增加。
在节点上删除一个服务
[root@zwb_docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c4c134478f2 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:85->80/tcp, :::85->80/tcp test-05
7ab78cbf9a7b httpd "httpd-foreground" 22 minutes ago Up 22 minutes 0.0.0.0:89->80/tcp, :::89->80/tcp test-04
18048616c880 httpd "httpd-foreground" 22 minutes ago Up 22 minutes 0.0.0.0:88->80/tcp, :::88->80/tcp test-03
adce82f7031b nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:84->80/tcp, :::84->80/tcp test-02
b8708ba1a234 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:83->80/tcp, :::83->80/tcp test-01
e2dc3187acc5 gliderlabs/registrator:latest "/bin/registrator -i…" 24 minutes ago Up 24 minutes registrator
[root@zwb_docker ~]# docker rm -f test-03
test-03
[root@zwb_docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c4c134478f2 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:85->80/tcp, :::85->80/tcp test-05
7ab78cbf9a7b httpd "httpd-foreground" 23 minutes ago Up 23 minutes 0.0.0.0:89->80/tcp, :::89->80/tcp test-04
adce82f7031b nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:84->80/tcp, :::84->80/tcp test-02
b8708ba1a234 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:83->80/tcp, :::83->80/tcp test-01
e2dc3187acc5 gliderlabs/registrator:latest "/bin/registrator -i…" 24 minutes ago Up 24 minutes registrator
在web上查看
自动减少一个。