.Net Core微服务入门——Consul集群搭建(四)
多台服务器集群搭建
1、先关闭几台集群服务器防火墙
当然,不关闭也可以,那就必须开通端口权限
consul 涉及的端口:8500、8300、8301、8302、8600
不开通防火墙,几台服务器见将无法通信,比如报如下错误:
Failed to join 192.168.8.25: dial tcp 192.168.8.25:8301: connect: connection refuse
2、查看主机IP信息
ip addr
具体信息如下:
记住 ens33(ip 192.168.8.25) 这个,很关键,马上要用到
2、启动consul服务,192.168.8.25
docker run -d --name consul25 -h=consul25 --net=host -e CONSUL_BIND_INTERFACE=ens33 consul agent -server=true -client=0.0.0.0 -bind=192.168.8.25 -ui -bootstrap-expect=3
注:-h=consul25 --net=host -e CONSUL_BIND_INTERFACE=ens33 这几个必须指定,否则会出现连不上,找到leader
failed to sync remote state: error="No cluster leader"
为什么出现这个问题,这就涉及到Docker 和 宿主机的网络通信问题
2.启动consul服务,192.168.8.27,加入25
docker run -d --name consul27 -h=consul27 --net=host -e CONSUL_BIND_INTERFACE=ens33 consul agent -server=true -client=0.0.0.0 -join=192.168.8.25 -ui
3.启动consul服务,192.168.8.28,加入25
docker run -d --name consul28 -h=consul28 --net=host -e CONSUL_BIND_INTERFACE=ens33 consul agent -server=true -client=0.0.0.0 -join=192.168.8.25 -ui
查看结果:
在这3台都建立一个client
docker run -d --restart=always --net=host --name=consul25-client -e CONSUL_BIND_INTERFACE=ens33 consul agent -bind=192.168.8.25 -client=0.0.0.0 -server-port=18300 -http-port=18500 -dns-port=18600 -serf-lan-port=18301 -serf-wan-port=18302 -retry-join=192.168.8.25:8301 -retry-join=192.168.8.27:8301 -retry-join=192.168.8.28:8301 -node=consul25-client
docker run -d --restart=always --net=host --name=consul27-client -e CONSUL_BIND_INTERFACE=ens33 consul agent -bind=192.168.8.27 -client=0.0.0.0 -server-port=18300 -http-port=18500 -dns-port=18600 -serf-lan-port=18301 -serf-wan-port=18302 -retry-join=192.168.8.25:8301 -retry-join=192.168.8.27:8301 -retry-join=192.168.8.28:8301 -node=consul27-client
docker run -d --restart=always --net=host --name=consul28-client -e CONSUL_BIND_INTERFACE=ens33 consul agent -bind=192.168.8.28 -client=0.0.0.0 -server-port=18300 -http-port=18500 -dns-port=18600 -serf-lan-port=18301 -serf-wan-port=18302 -retry-join=192.168.8.25:8301 -retry-join=192.168.8.27:8301 -retry-join=192.168.8.28:8301 -node=consul28-client
注:这里端口需要特别指定,否则会端口冲突
在其他服务上新建Client
docker run -d --restart=always -p 8500:8500 --name consul31-client --net=host -e CONSUL_BIND_INTERFACE=ens33 consul agent -node=consul31-client -client=0.0.0.0 -bind=192.168.8.31 -join 192.168.8.25:8301
docker run -d --restart=always -p 8500:8500 --name consul32-client --net=host -e CONSUL_BIND_INTERFACE=ens33 consul agent -node=consul32-client -client=0.0.0.0 -bind=192.168.8.32 -retry-join=192.168.8.25:8301 -retry-join=192.168.8.27:8301 -retry-join=192.168.8.28:8301
注: 如果consul client 和 api在一台服务器上,利用网关ocelot时,会把node当成IpAddress查找主机,node=consul32-client 这种格式,会出现找不到主机的情况,需要把node改成ip
docker run -d --restart=always -p 8500:8500 --name consul31-client --net=host -e CONSUL_BIND_INTERFACE=ens33 consul agent -node=192.168.8.31 -client=0.0.0.0 -bind=192.168.8.31 -join 192.168.8.25:8301
docker run -d --restart=always -p 8500:8500 --name consul32-client --net=host -e CONSUL_BIND_INTERFACE=ens33 consul agent -node=192.168.8.32 -client=0.0.0.0 -bind=192.168.8.32 -retry-join=192.168.8.25:8301 -retry-join=192.168.8.27:8301 -retry-join=192.168.8.28:8301
注册服务:
在31上注册一个服务
docker run --name myservice31 -d -p 5001:80 192.168.8.25:5000/myapi --ip=192.168.8.31 --port=5001 --consulhost=192.168.8.31 --consulport=8500
在32 上注册一个服务
docker run --name myservice32 -d -p 5001:80 192.168.8.25:5000/myapi --ip=192.168.8.32 --port=5001 --consulhost=192.168.8.32 --consulport=8500
查看服务