consul+consul-template+nginx代理

consul集群

三台主机分别使用docker部署consul组成集群

主节点
docker run -d --name consul1 -h=node1 --net=host -e CONSUL_BIND_INTERFACE=ens34 consul agent -server=true -client=0.0.0.0 -bind=10.98.4.1 -ui -bootstrap-expect=3
node节点
docker run -d --name consul2 -h=node2 --net=host -e CONSUL_BIND_INTERFACE=ens34 consul agent -server=true -client=0.0.0.0 -join=10.98.4.1 -u
docker run -d --name consul3 -h=node3 --net=host -e CONSUL_BIND_INTERFACE=ens34 consul agent -server=true -client=0.0.0.0 -join=10.98.4.1 -ui
#-net=host直接使用宿主机网络
#-bind:这是给其他consul server来加入集群的ip
#-client:使用此配置,Consul的客户端接口将绑定到网桥IP,并可供该网络上的其他容器使用,但不能在主机网络上使用。
#-bootstrap-expect 设置服务数量,当达到设定数量启动集群开始选举,-bind的这台机器成为leader
#-ui管理界面
#-h:设置node的名称,集群的服务器不能取同名的node名称
#CONSUL_BIND_INTERFAC,指定集群通信网卡

查看集群组成

docker exec -t consul1 consul members
Node   Address         Status  Type    Build   Protocol  DC   Partition  Segment
node1  10.98.4.1:8301  alive   server  1.11.1  2         dc1  default    <all>
node2  10.98.4.2:8301  alive   server  1.11.1  2         dc1  default    <all>
node3  10.98.4.3:8301  alive   server  1.11.1  2         dc1  default    <all>

查看集群选举状态

docker exec -t consul1 consul operator raft list-peers
Node   ID                                    Address         State     Voter  RaftProtocol
node2  666875f2-7dcd-8bdf-a035-f349741a7ef1  10.98.4.2:8300  follower  true   3
node3  9a313a2e-d3c3-3afc-f444-6cff78817d65  10.98.4.3:8300  follower  true   3
node1  6f3d4a86-ee28-c974-68a8-4f454af7f22e  10.98.4.1:8300  leader    true   3

consul-template+nginx

使用consul-template指定模板文件生成nginx配置文件
range service "consul"中的consul是根据镜像名称匹配出的容器

#模板文件nginx.yml
upstream consul_cluster {
    {{range service "consul"}}
    server {{ .Address }}:8500;
    {{ end }}
}
server {
    listen 10500;
    server_name 10.144.100.90;
    location / {
         proxy_pass http://consul_cluster;
         root html;
         index index.html index.htm;
    }
}

consul-template -consul-addr 10.144.100.90:8500 -template “./nginx.yml:/etc/nginx/conf.d/consul.conf:/usr/sbin/nginx -s reload” -log-level=info

#这里是consul-template生成的配置文件
upstream consul_cluster {
    
    server 10.144.100.90:8500;
    
    server 10.144.100.203:8500;
    
    server 10.144.100.211:8500;
    
}
server {
    listen 10500;
    server_name 10.144.100.90;
    location / {
         proxy_pass http://consul_cluster;
         root html;
         index index.html index.htm;
    }
}

nginx代理8500端口,用于常用的服务注册和获取服务注册信息。

8300:集群内数据的读写和复制
8301:单个数据中心gossip协议通讯
8302:跨数据中心gossip协议通讯
8500:提供获取服务列表、注册服务、注销服务等HTTP接口;提供UI服务
8600:采用DNS协议提供服务发现功能
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

背锅攻城师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值