我有一个Consul服务器在VM内运行,而客户端代理则在Vagrant内的其他VM上运行。
其他VM上运行的客户端代理都可以在Consul服务器上注册。
我试图在本地计算机上运行客户端代理以进行本地开发。但是,我无法让客户端代理连接到Consul的Vagrant实例。我尝试过公共和私有网络和端口转发的组合,但是我做不到。
如何配置Vagrantfile / Consul客户端进行注册?
剥离了Vagrantfile :
Vagrant.configure("2") do |config|
consul_servers = ["consul1"]
consul_servers.each.with_index(1) do |consul_server, index|
config.vm.define consul_server do |server|
server.vm.box = "centos/7"
server.vm.box_version = "1809.01"
# vars['consul'][consul_server]['hostname'] is set to "consul1"
server.vm.hostname = vars['consul'][consul_server]['hostname']
# vars['consul'][consul_server]['ip_address'] is set to 192.168.23.17
server.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)", ip: vars['consul'][consul_server]['ip_address']
server.vm.network "forwarded_port", guest: 8500, host: "850#{index}"
# I've tried with these forwarded ports as well.
# Leaving them here for info
# server.vm.network "forwarded_port", guest: 8300, host: 8310
# server.vm.network "forwarded_port", guest: 8301, host: 8311, protocol: "tcp"
# server.vm.network "forwarded_port", guest: 8301, host: 8311, protocol: "udp"
# server.vm.network "forwarded_port", guest: 8302, host: 8311, protocol: "tcp"
# server.vm.network "forwarded_port", guest: 8302, host: 8311, protocol: "udp"
# server.vm.network "forwarded_port", guest: 8400, host: 8410
server.vm.provision :ansible do |ansible|
# ...
end
end
end
Consul服务器配置:
datacenter = "dc1"
data_dir = "/opt/consul"
server = true
bootstrap_expect = 1
ui = true
client_addr = "0.0.0.0"
retry_join = ["192.168.23.17"]
bind_addr = "192.168.23.17"
领事客户端配置(在本地运行)
datacenter = "dc1"
data_dir = "/consul/data"
retry_join = ["192.168.23.17"] # Tried "localhost", "0.0.0.0"
node_name = "local-dev"
bind_addr = "192.168.23.1" # This is the IP for the vboxnet1 interface.
# For bind_addr I've also tried 192.168.1.12 (en0 ip address), localhost, 0.0.0.0