consul 自动发现与自动注册
Consul 服务注册与发现
一: Consul 概述
1.1 什么是服务发现与注册
服务注册与发现是微服务架构中不可或缺的重要组件。
起初服务都是单节点的,不保障高可用性,也不考虑服务的压力承载,服务之间调用单纯的通过接口访问。直到后来出现了多个节点的分布式架构,起初的解决手段是在服务前端负载均衡,这样前端必须要知道所有后端服务的网络位置,并配置在配置文件中。
这里就会有几个问题:
- 如果需要调用后端服务A-N,就需要配置N个服务的网络位置,配置很麻烦
- 后端服务的网络位置变化,都需要改变每个调用者的配置
既然有这些问题,那么服务注册与发现就是解决这些问题的。后端服务A-N可以把当前自己的网络位置注册到服务发现模块,服务发现就以K-V的方式记录下来, K一般是服务名, v就是IP: PORT,服务发现模块定时的进行健康检查,轮询查看这些后端服务能不能访问的了。前端在调用后端服务A-N的时候,就跑去服务发现模块问下它们的网络位置,然后再调用它们的服务。这样的方式就可以解决上面的问题了,前端完全不需要记录这些后端服务的网络位置,前端和后端完全解耦!
1.2 什么是consul
什么是consul:
- consul是google开源的一个使用go语言开发的服务管理软件。支持多数据中心、分布式高可用的、服务发现和配置共享。
- 采用Raft算法,用来保证服务的高可用。
- 内置了服务注册与发现框架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如zookeeper等) 。
- 服务部署简单,只有一个可运行的二进制的包。
- 每个节点都需要运行agent,他有两种运行模式server和client。
- 每个数据中心官方建议需要3或5个server节点以保证数据安全,同时保证server-leader的选举能够正确的进行。
consul的的模式(client模式和sever模式):
- 在client模式下,所有注册到当前节点的服务会被转发到server节点,本身是不持久化这些信息。
- 在server模式下,功能和client模式相似,唯一不同的是,它会把所有的信息持久化到本地,这样遇到故障,信息是可以被保留的。
- server-leader是所有server节点的老大,它和其它server节点不同的是,它需要负责同步注册的信息给其它的server节点,同时也要负责各个节点的健康监测。
consul提供的一些关键特性:
- 服务注册与发现: consul通过DNS或者HTTP接口使服务注册和服务发现变的很容易,一些外部服务,例如saas提供的也可以一样注册。
- 健康检查: 健康检测使consu1可以快速的告警在集群中的操作。和服务发现的集成,可以防止服务转发到故障的服务上面。
- Key/Value存储:一个用来存储动态配置的系统。提供简单的HTTP接口,可以在任何地方操作。
- 多数据中心:无需复杂的配置,即可支持任意数量的区域。
安装consul是用于服务注册,也就是容器本身的一些信息注册到consul里面,其他程序可以通过consul获取注册的相关服务信息,这就是服务注册与发现。
二: consul 的部署
2.1 拓扑
节点 | 服务 |
---|---|
consul服务器: 192.168.23.103 | consul服务,nginx服务,consul-template守护进程 |
registraotor服务器:192.168.23.104 | 运行registrator容器,nginx容器 |
2.2 consul服务器建立consul服务
2.2.1 安装并启动服务
复制#解压软件包,安装服务
[root@host103 ~]# mkdir /opt/consul
[root@host103 ~]# cp /opt/consul_0.9.2_linux_amd64.zip /opt/consul/
[root@host103 ~]# cd /opt/consul/
[root@host103 consul]# unzip consul_0.9.2_linux_amd64.zip
[root@host103 consul]# mv consul /usr/local/bin/
#设置代理,在后台启动consul 服务端
[root@host103 consul]# consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.23.103 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
-server: 以server身份启动。默认是client。
-bootstrap :用来控制一个server是否在bootstrap模式,在一个数据中心中只能有一个server处于bootstrap模式,当一个server处于bootstrap模式时,可以自己选举为server-leader.
-bootstrap-expect=2 :集群要求的最少server数量,当低于这个数量,集群即失效。
-ui:指定开启UI界面,这样可以通过http://localhost:8500/ui这样的地址访问consul自带的web UI界面。
-data-dir:指定数据存储目录。
-bind :指定用来在集群内部的通讯地址,集群内的所有节点到此地址都必须是可达的,默认是0.0.0.0
-client :指定consul绑定在哪个client地址上,这个地址提供HTTP, DNS,RPC等服务,默认是127.0.0.1.
-node:节点在集群中的名称,在一个集群中必须是唯一的,默认是该节点的主机名。
-datacenter :指定数据中心名称,默认是dc1.
2.2.2 consul监听的5个端口
复制#查看consul 默认监听的5个端口
[root@host103 docker]# netstat -natp | grep consul
tcp 0 0 192.168.23.103:8300 0.0.0.0:* LISTEN 48211/consul
tcp 0 0 192.168.23.103:8301 0.0.0.0:* LISTEN 48211/consul
tcp 0 0 192.168.23.103:8302 0.0.0.0:* LISTEN 48211/consul
tcp6 0 0 :::8500 :::* LISTEN 48211/consul
tcp6 0 0 :::8600 :::* LISTEN 48211/consul
2.3 集群的状态信息查看
复制#memebers状态
[root@host103 consul]# consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.23.103:8301 alive server 0.9.2 2 dc1
#查看集群状态
[root@host103 consul]# consul operator raft list-peers
Node ID Address State Voter RaftProtocol
consul-server01 192.168.23.103:8300 192.168.23.103:8300 leader true 2
[root@host103 consul]# consul info | grep leader
leader = true
leader_addr = 192.168.23.103:8300
2.2.4 通过http api获取集群信息
复制#查看集群server成员
[root@host103 consul]# curl 127.0.0.1:8500/v1/status/peers
["192.168.23.103:8300"]
#查看集群server-leader
[root@host103 consul]# curl 127.0.0.1:8500/v1/status/leader
"192.168.23.103:8300"
#查看注册的所有服务(当前还没有)
[root@host103 consul]# curl 127.0.0.1:8500/v1/status/services
#查看nginx的服务信息
[root@host103 consul]# curl 127.0.0.1:8500/v1/status/nginx
#查看集群的节点详细信息
[root@host103 consul]# curl 127.0.0.1:8500/v1/status/nodes
2.3 registrator 服务器配置
容器服务自动加入Nginx集群
2.3.1 安装Gliderlabs/Registrator
Gliderlabs/Registrator可检查容器运行状态自动注册,还可以注销docker 容器的服务到服务配置中心。目前支持Consul,Etcd和SkyDNS2
复制[root@host104 ~]# docker run -d \
> --name=registrator \
> --net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> --restart=always \
> gliderlabs/registrator:latest \
> --ip=192.168.23.104 \
> consul://192.168.23.103:8500
--net=host :把运行的docker容器设定为host网络模式。
-v /var/run/docker.sock:/tmp/docker.sock :把宿主机的Docker守护进程(Docker daemon)默认监听的Unix域套接字挂载到容器中。
--restart=always :设置在容器退出时总是重启容器。
--ip :刚才把network指定了host模式,所以我们指定ip为宿主机的ip。
consul :指定consul服务器的IP和端口。
2.3.2 consul 为什么可以实现自动发现
被监控的节点服务器上,nginx容器的服务启动后,并做了端口映射后,会将映射的信息写入到宿主机的docker.sock文件
registrator自动发现模块会监控宿主机的docker.sock ,就会发现nginx服务。
registrator 会将信息写入到consul的自动注册模块,通过8500 web ui 展示
2.3.3 测试服务发现功能
复制#--name 指定容器名,-h指定容器主机名
[root@host104 ~]# docker run -itd -p:83:80 --name web-01 -h test01 nginx
[root@host104 ~]# docker run -itd -p:84:80 --name web-02 -h test02 nginx
[root@host104 ~]# docker run -itd -p:88:80 --name web-03 -h test03 httpd
[root@host104 ~]# docker run -itd -p:89:80 --name web-04 -h test04 httpd
2.3.4 验证http和nginx服务是否注册到consul
浏览器中,输入 http://192.168.23.103:8500,在 Web 页面中“单击 NODES”,然后单击“consurl-server01”,会出现 5 个服务。
//在consul服务器使用curl测试连接服务器
curl 127.0.0.1:8500/v1/catalog/services
三: consul-temple
Consul-Template是基于Consul的自动替换配置文件的应用。Consul-Template是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行 shell 命令执行更新操作,重新加载 Nginx。
Consul-Template可以查询Consul中的服务目录、Key、Key-values 等。这种强大的抽象功能和查询语言模板可以使 Consul-Template 特别适合动态的创建配置文件。例如:创建Apache/Nginx Proxy Balancers 、 Haproxy Backends等。
3.1在 Consul 服务器上,准备template nginx模板文件
复制[root@host103 ~]# vim /opt/consul/nginx.ctmpl
#定义nginx的upstream 模板
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
#定义一个server,监听8000端口,反向代理到upstream
server {
#监听8000端口,访问就使用端口8000
listen 8000;
server_name 192.168.23.103;
access_log /var/log/nginx/mynet.com-access.log; #定义首页文件
index index.html index.php;
location / {
#记录所有转发路径,使得后端的节点服务器可以获取真正的客户端地址,而不是去获得代理端的ip地址。
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 进行7层转发
proxy_pass http://http_backend;
}
}
3.2 编译安装nginx
复制[root@host103 nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ make
[root@host103 ~]# useradd -M -s /sbin/nologin nginx
[root@host103 opt]# ls nginx-1.12.0.tar.gz
nginx-1.12.0.tar.gz
[root@host103 opt]#tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@host103 nginx-1.12.0]#cd /opt/nginx-1.12.0/
[root@host103 nginx-1.12.0]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[root@host103 nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
3.3 consul 服务端配置nginx
复制[root@host103 nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
http{
......
#配置添加虚拟主机目录
19// include vhost/*.conf;
.....
}
#创建虚拟主机目录
[root@host103 nginx-1.12.0]# mkdir /usr/local/nginx/conf/vhost
#创建日志文件目录
[root@host103 nginx-1.12.0]# mkdir /var/log/nginx
#启动nginx
[root@host103 sbin]# cd /usr/local/nginx/sbin/
[root@host103 sbin]# ./nginx
[root@host103 sbin]# netstat -natp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 52564/nginx: master
3.4 配置并启动template
复制#上传软件包到 /opt目录
[root@host103 ~]# cd /opt/
[root@host103 opt]# ls consul-template_0.19.3_linux_amd64.zip
consul-template_0.19.3_linux_amd64.zip
#解压软件包
[root@host103 opt]# unzip consul-template_0.19.3_linux_amd64.zip
[root@host103 opt]# mv consul-template /usr/local/bin/
#//在前台启动 template 服务,启动后不要按 ctrl+c 中止 consul-template 进程
#consul-addr 指定consul服务器的IP和端口
#--template指定模板文件:nginx配置文件: nginx重载配置文件命令
#--log-level=info :配置日志信息级别
[root@host103 opt]# consul-template --consul-addr 192.168.23.103:8500 \
--template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/mynet.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
#重开终端,查看生成的配置文件
[root@host103 ~]# vim /usr/local/nginx/conf/vhost/mynet.conf
upstream http_backend {
server 192.168.23.104:83;
server 192.168.23.104:84;
}
server {
listen 8000;
server_name 192.168.23.103;
access_log /var/log/nginx/mynet.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;
}
}
3.5 访问template-nginx
复制#为nginx容器写入网页文件(容器的nginx网页目录是/usr/share/nginx/html)
[root@host104 ~]# docker exec -it web-01 bash -c "echo 'this is web-01' > /usr/share/nginx/html/index.html"
[root@host104 ~]# docker exec -it web-02 bash -c "echo 'this is web-02' > /usr/share/nginx/html/index.html"
#访问测试
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-02
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-01
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-02
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-01
3.6 增加一个nginx容器节点
3.6.1 加一个 nginx 容器节点,测试服务发现及配置更新功能
复制#增加一个nginx容器节点,测试服务发现及配置更新功能。
[root@host104 ~]# docker run -itd -p 85:80 --name web-05 -h test05 nginx
#为新的nginx容器写入网站文件
[root@host104 ~]# docker exec -it web-05 bash -c "echo 'this is web-05' > /usr/share/nginx/html/index.html"
#访问测试
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-01
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-02
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-05
3.6.2在consul端 观察 template 服务
复制#在consul端观察 template 服务,会从模板更新/usr/local/nginx/conf/vhost/kgc.conf 文件内容,并且重载 nginx 服务。
[root@host103 ~]# cat /usr/local/nginx/conf/vhost/mynet.conf
upstream http_backend {
server 192.168.23.104:83;
server 192.168.23.104:84;
#新加入的节点
server 192.168.23.104:85;
}
.........
3.6.3 查看三台 nginx 容器日志,请求正常轮询到各个容器节点上
复制docker logs -f test-01
docker logs -f test-02
docker logs -f test-05
四: consul多节点
4.1 添加一台已有docker环境的服务器加入已有的群集中
复制[root@host105 opt]# ls consul_0.9.2_linux_amd64.zip
consul_0.9.2_linux_amd64.zip
[root@host105 opt]# unzip consul_0.9.2_linux_amd64.zip
[root@host105 opt]# mv consul /usr/local/bin/
#将192.168.23.105/24加入已有的群集中
[root@host105 opt]# consul agent \
-server \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.23.105 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true \
-datacenter=dc1 \
-join 192.168.23.103 &> /var/log/consul.log &
-enable-script-checks=true :设置检查服务为可用
-datacenter : 数据中心名称
-join :加入到已有的集群中
-node: 指定节点名(节点名唯一,集群里不可以重复)
4.2 查看集群状态
复制[root@host105 opt]# consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.23.103:8301 alive server 0.9.2 2 dc1
consul-server02 192.168.23.105:8301 alive server 0.9.2 2 dc1
[root@host105 opt]# consul operator raft list-peers
Node ID Address State Voter RaftProtocol
consul-server01 192.168.23.103:8300 192.168.23.103:8300 leader true 2
consul-server02 192.168.23.105:8300 192.168.23.105:8300 follower true 2
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现