我有一个 vagrant 配置文件,其中配置了三个虚拟机。除了一个虚拟机使用静态 IP,其他两个使用 DHCP 外,其余虚拟机都相同。由 DHCP 分配 IP 的两个虚拟机可以毫无问题地相互通信,但其他虚拟机无法联系使用静态 IP 的虚拟机。
换句话说 ping client0
->client1
有效
ping client1
->client0
工作
ping client1
->server
不通
这是我的 Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.provision "chef_solo" do |chef|
chef.cookbooks_path = "provision/cookbooks"
chef.add_recipe "apt"
chef.add_recipe "python"
end
config.vm.provision :shell, :path => "provision/python-modules.py"
config.vm.define :server do |server|
server.vm.hostname = "server"
server.vm.network :private_network, ip: "172.28.128.2"
end
config.vm.define :client0 do |client0|
client0.vm.hostname = "client0"
client0.vm.network :private_network, type: :dhcp
end
config.vm.define :client1 do |client1|
client1.vm.hostname = "client1"
client1.vm.network :private_network, type: :dhcp
end
end
有什么想法可以让server
其他机器通过静态 IP 联系我吗?我正在使用 virtualbox 提供商。
答案1
经过大量研究和实验,我们发现 Vagrant 为每个虚拟机分配了不同的子掩码,因此它们无法相互连接。实际上,它们都在不同的网络上。
但事实证明,您可以覆盖这一点。为此,只需配置一个网络掩码,如下所示。
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.provision "chef_solo" do |chef|
chef.cookbooks_path = "provision/cookbooks"
chef.add_recipe "apt"
chef.add_recipe "python"
end
config.vm.provision :shell, :path => "provision/python-modules.py"
config.vm.define :server do |server|
server.vm.hostname = "server"
server.vm.network :private_network, ip: "172.28.128.2", :netmask => "255.255.0.0"
end
config.vm.define :client0 do |client0|
client0.vm.hostname = "client0"
client0.vm.network :private_network, type: :dhcp, :netmask => "255.255.0.0"
end
config.vm.define :client1 do |client1|
client1.vm.hostname = "client1"
client1.vm.network :private_network, type: :dhcp, :netmask => "255.255.0.0"
end
end
目前我不确定您是否可以在全局配置中覆盖所有虚拟机的网络掩码,同时保留一些具有 DHCP 的虚拟机和其他具有静态 IP 的虚拟机。一旦我回到功能更强大的机器上,我将尝试这样做,这样每次销毁和创建虚拟机都会更快。
答案2
如果您使用 DHCP 配置多个 VM,也可以执行类似下面的操作。在可选 VM 部分,您可以提及/命名所需的多个 VM。
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = '2'
DOMAIN = '.example.com'
PUPPETMASTER_HOSTNAME = 'puppet' + DOMAIN
#Requires box with puppetlabs repo setup, but puppet not installed
DEFAULT_BOX = 'precise64'
DEFAULT_BOX_URL = 'http://files.vagrantup.com/precise64.box'
REQUIRED_HOSTS = [
'puppetdb',
'puppetdb-postgres'
]
OPTIONAL_HOSTS = [
'ldap01',
'ldap02'
]
HOSTS = REQUIRED_HOSTS + OPTIONAL_HOSTS
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
#VBox config to avoid mac address collisions
config.vm.provider :virtualbox do |vb|
vb.customize ['modifyvm', :id, '--natnet1', '10.200/16']
#vb.gui = true
end
config.vm.box = DEFAULT_BOX
config.vm.box_url = DEFAULT_BOX_URL
#Add network interface
config.vm.network :private_network, :type => :dhcp, :netmask => '255.255.0.0'
#Defines puppet master
config.vm.define :puppet_master do |puppetmaster|
puppetmaster.vm.hostname = PUPPETMASTER_HOSTNAME
puppetmaster.vm.synced_folder '../puppet/', '/vagrant_sync/puppet', :create => true
puppetmaster.vm.provision :shell, :path => 'scripts/bootstrap_puppetmaster.sh'
end
HOSTS.each do |host_name, opts|
config.vm.define host_name do |box|
fqdn = host_name + DOMAIN
box.vm.hostname = fqdn
box.vm.provision :shell, :inline => 'yum install -y puppet'
box.vm.provision :puppet_server do |puppet|
puppet.puppet_server = PUPPETMASTER_HOSTNAME
end
end
end
end