我正在尝试在 2 个 vagrant 盒子上配置静态 IP,以便它们可以相互通信。使用以下配置,“db”节点分别带有 192.168.50.4 和 192.168.50.5 的 eth1 和 eth2。第二个“web”节点正确地只带有 192.168.50.5 的 eth1。为什么第一个节点有两个地址?
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: "echo Hello"
config.vm.define "web" do |web|
web.vm.box = "ubuntu/trusty64"
config.vm.network "private_network", ip: "192.168.50.5"
end
config.vm.define "db" do |db|
db.vm.box = "ubuntu/trusty64"
config.vm.network "private_network", ip: "192.168.50.4"
end
end
答案1
我刚刚测试过,它对我有用。以下是代码:
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.define "web" do |web|
web.vm.box = "ubuntu14"
web.vm.network "private_network", ip: "192.168.50.5"
config.vm.hostname = "web"
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "256"]
vb.customize ["modifyvm", :id, "--cpus", "1"]
# vb.gui = true
end
end
config.vm.define "db" do |db|
db.vm.box = "ubuntu14"
db.vm.network "public_network", ip: "192.168.50.4"
db.vm.hostname = "db"
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "512"]
vb.customize ["modifyvm", :id, "--cpus", "2"]
# vb.gui = true
end
end
end
编辑:
最终的 IP 配置:
vagrant@web:~$ sudo ip ad show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:c3:8f:e9 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fec3:8fe9/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:96:2d:54 brd ff:ff:ff:ff:ff:ff
inet 192.168.50.5/24 brd 192.168.50.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe96:2d54/64 scope link
valid_lft forever preferred_lft forever
vagrant@db:~$ sudo ip ad show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:c3:8f:e9 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fec3:8fe9/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:d6:b3:d1 brd ff:ff:ff:ff:ff:ff
inet 192.168.50.4/24 brd 192.168.50.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fed6:b3d1/64 scope link
valid_lft forever preferred_lft forever
答案2
这次我将采用不同的方法。我使用 vagrant 插件 vagrant-hostmanager。您可以使用它进行安装,vagrant plugin install vagrant-hostmanager
我的 vagrant 文件如下所示。我现在可以通过主机名 (例如 ssh nfs1) ping/ssh/other。
#-----------------------------------------------------#
#- Project: vagrant-based
#- Author.: jlawson
#- Date...: 2015-03-16
#- Notes..:
#-----------------------------------------------------#
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |c|
#-- BEGIN Plugin Config -------------------------------------------------------#
#- Configure vagrant-hostmanager plugin
if Vagrant.has_plugin?("vagrant-hostmanager")
c.hostmanager.enabled = true
c.hostmanager.manage_host = true
c.hostmanager.ignore_private_ip = false
c.hostmanager.include_offline = true
else
puts "FATAL: Vagrant-hostmanager plugin not detected. Please install the plugin with\n 'vagrant plugin install vagrant-hostmanager' from any other directory\n before continuing."
exit
end
#-- END Plugin ----------------------------------------------------------------#
c.vm.define "app1", primary: true do |app1|
#- Instance Base & DNS
app1.vm.box = "app1"
app1.vm.box_url = "file:///DevOps/vm-boxes/aws/opscode_ubuntu-14.04_chef-provisionerless.box"
app1.vm.hostname = "dev-application1.local.vm"
app1.hostmanager.aliases = %w(dev-app1.localdomain dev-app1)
#- Ports and IPs
app1.vm.network(:forwarded_port, {:guest=>80, :host=>8001})
app1.vm.network(:forwarded_port, {:guest=>443, :host=>8443})
app1.vm.network(:private_network, {:ip=>"199.10.0.2"})
#- Misc
app1.ssh.forward_agent = true
#- Instance Customizations
app1.vm.provider :virtualbox do |p|
p.name = app1.vm.box
p.customize ["modifyvm", :id, "--memory", "1024"]
p.customize ["modifyvm", :id, "--cpus", "1"]
end
end
c.vm.define "nfs1", autostart: false do |nfs1|
#- Instance Base & DNS
nfs1.vm.box = "nfs1"
nfs1.vm.box_url = "file:///DevOps/vm-boxes/aws/opscode_ubuntu-14.04_chef-provisionerless.box"
nfs1.vm.hostname = "dev-nfs1.local.vm"
nfs1.hostmanager.aliases = %w(dev-nfs1.localdomain dev-nfs1)
#- Ports & IPs
nfs1.vm.network(:private_network, {:ip=>"199.10.0.4"})
#- Instance Customizations
nfs1.vm.provider :virtualbox do |p|
p.customize ["modifyvm", :id, "--memory", "512"]
p.customize ["modifyvm", :id, "--cpus", "1"]
end
end
end
答案3
为什么第一个节点有两个地址?
config.vm.define "web" do |web|
web.vm.box = "ubuntu/trusty64"
config.vm.network "private_network", ip: "192.168.50.5"
end
config.vm.define "db" do |db|
db.vm.box = "ubuntu/trusty64"
config.vm.network "private_network", ip: "192.168.50.4"
end
因为你同时使用了config.vm.network
两者(即一选项值)而不是唯一web.vm.network
/db.vm.network
选项。
从https://developer.hashicorp.com/vagrant/docs/multi-machine
config.vm.define 接受一个包含另一个变量的块。此变量(如上面的 web)与 config 变量完全相同,只是内部变量的任何配置仅适用于被定义的机器。因此,web 上的任何配置都只会影响 web 机器。