我尝试使用vagrant创建一个多vm设置,其中只有服务器的公开端口需要转发到指定的主机端口。无需公开客户端端口。但是当我尝试使用附加的Vagrantfile执行此操作时,由于某种原因,它会评估我的if条件以过滤客户端,对客户端也是如此。有人能指出我在这里做错了吗?
Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = '2'
BASEBOX = 'centos-6.7'
BOX_MEMORY = '256'
# Declare the cluster config in a hash
HOST_CONFIG = {
'some_server' => '192.168.205.10',
'some_client' => '192.168.205.11'
}
# Create the vms
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = BASEBOX
HOST_CONFIG.each do |hostname, hostip|
config.vm.network "forwarded_port", guest: 80, host: 8080 if hostname == "some_server"
config.vm.define hostname do |hname|
hname.vm.provider 'virtualbox' do |v|
v.name = hostname
v.customize [ 'modifyvm', :id, '--cpus', '1' ]
v.customize [ 'modifyvm', :id, '--memory', BOX_MEMORY ]
end
hname.vm.network 'private_network', ip: hostip
hname.vm.provision :hosts do |provisioner|
provisioner.autoconfigure = true
provisioner.sync_hosts = true
end
hname.vm.provision 'ansible' do |ansible|
ansible.playbook = 'bootstrap.yml'
end
end
end
end
输出:
$ vagrant up
Bringing machine 'server' up with 'virtualbox' provider...
Bringing machine 'client' up with 'virtualbox' provider...
==> server: Importing base box 'centos-6.7'...
==> server: Matching MAC address for NAT networking...
==> server: Setting the name of the VM: server
==> server: Clearing any previously set network interfaces...
==> server: Preparing network interfaces based on configuration...
server: Adapter 1: nat
server: Adapter 2: hostonly
==> server: Forwarding ports...
server: 80 (guest) => 8080 (host) (adapter 1)
server: 22 (guest) => 2222 (host) (adapter 1)
==> server: Running 'pre-boot' VM customizations...
==> server: Booting VM...
==> server: Waiting for machine to boot. This may take a few minutes...
server: SSH address: 127.0.0.1:2222
server: SSH username: vagrant
server: SSH auth method: private key
server: Warning: Remote connection disconnect. Retrying...
server: Warning: Remote connection disconnect. Retrying...
==> server: Machine booted and ready!
==> server: Checking for guest additions in VM...
==> server: Configuring and enabling network interfaces...
==> server: Mounting shared folders...
server: /vagrant => /Users/ANJUWAA/Projects/Nagios
==> server: Running provisioner: hosts...
==> client: Importing base box 'centos-6.7'...
==> client: Matching MAC address for NAT networking...
==> client: Setting the name of the VM: client
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 8080 is already in use
on the host machine.
To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:
config.vm.network :forwarded_port, guest: 80, host: 1234
Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding.
答案 0 :(得分:2)
现在,您有效地为所有计算机设置vm.network
值,如果其中一个计算机名为some_server
。
您应将vm.network
设置放在vm.define
- 循环中:
HOST_CONFIG.each do |hostname, hostip|
config.vm.define hostname do |hname|
hname.vm.network "forwarded_port", guest: 80, host: 8080 if hostname == "some_server"
hname.vm.provider 'virtualbox' do |v|
v.name = hostname
v.customize [ 'modifyvm', :id, '--cpus', '1' ]
v.customize [ 'modifyvm', :id, '--memory', BOX_MEMORY ]
end