vagrant 端口转发的缓存请教

2019-09-04 21:25:18 +08:00
 mengzhexin

我尝试使用 vagrant 搭建一个开发环境,其中一段配置文件如下。

 (1..NUMBER_OF_WORKER_INSTANCES).each do |i|
   vm_name = "%s-%02d" % [WORKER_NAME_PREFIX, i]
   vm_static_ip = WORKER_IP_TEMPLATE % [100 + i]
   vm_ssh_host_port = "202%01d" % [i]

   config.vm.define(vm_name) do |worker|
    worker.vm.box = "ubuntu/trusty64"

    worker.vm.hostname = vm_name
    worker.vm.network "private_network", ip: vm_static_ip

    worker.vm.provider :virtualbox do |vb|
      vb.name = vm_name
      vb.memory = 1024
      vb.cpus = 1
      vb.customize ["modifyvm", :id, "--nic2", "hostonly"]
      vb.customize ["modifyvm", :id, "--nicpromisc2", "allow-all"]
    end

    worker.vm.provision "shell", path: "scripts/setup-os.sh", privileged:false
    worker.vm.provision "shell", path: "scripts/setup-java.sh", privileged:false

    worker.vm.provision "shell" do |s|
              s.path = "scripts/setup-flink.sh"
              s.args = " -t #{NUMBER_OF_WORKER_INSTANCES}"
              s.privileged=false
    end

    worker.vm.network "forwarded_port", guest: 22, host: vm_ssh_host_port, id: 'ssh'

   end

在调试 vagrantfile 的过程中,引入的脚本报错,于是修改之后继续使用 vagrant up 启动虚拟机,这个时候会报错,端口被宿主机占用,我尝试多次更换端口之后 up 或者 reload,发现每一个新换的端口都会被报错占用。请教大家一下这是为什么?

1059 次点击
所在节点    问与答
1 条回复
mengzhexin
2019-09-04 21:26:47 +08:00
这是我将 vm_ssh_host_port 替换成手动随机端口 2282 之后的报错
Vagrant cannot forward the specified ports on this VM, since they would collide with some other application that is already listening on these ports. The forwarded port to 2282 is already in use on the host machine.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/598010

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX