config.vm.define :web do |web_config|
web_config.vm.box = "saucy"
web_config.vm.host_name = "web"
web_config.vm.network "private_network", ip:"192.168.100.10"
end
config.vm.define :db do |db_config|
db_config.vm.box = "saucy"
db_config.vm.host_name = "db"
db_config.vm.network "private_network", ip:"192.168.100.20"
end
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "1024"]
vb.customize ["modifyvm", :id, "--cpus", "2"]
end
我配置了两个VM,'db'和'web'。 我可以为不同的VM设置不同的内存大小吗?
答案 0 :(得分:26)
2016-08-31:更新了每个@DarkForce请求包含整个Vagrantfile的答案。
您可以通过在每个vm.provider
块中移动vm.define
定义来实现。例如,此配置将内存设置为2048MB,用于" web"和#34; db":
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.define :web do |web_config|
web_config.vm.host_name = "web"
web_config.vm.network "private_network", ip:"192.168.100.10"
web_config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048"]
vb.customize ["modifyvm", :id, "--cpus", "2"]
end
end
config.vm.define :db do |db_config|
db_config.vm.host_name = "db"
db_config.vm.network "private_network", ip:"192.168.100.20"
db_config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "1024"]
vb.customize ["modifyvm", :id, "--cpus", "2"]
end
end
end
注意:此示例(与Vagrant文档中的许多内容一样)仅适用于VirtualBox。如果您希望Vagrantfile也可以与VMware或其他提供程序一起使用,则将单独列出自定义参数。例如:
x.vm.provider "vmware_fusion" do |v|
v.vmx["memsize"] = "3000"
end
x.vm.provider :virtualbox do |v|
v.customize ["modifyvm", :id, "--memory", "3000"]
end
答案 1 :(得分:5)
最好的方法是使用像这里一样的Ruby哈希数组 - http://sysadm.pp.ua/linux/sistemy-virtualizacii/vagrantfile.html。您可以定义如下数组:
servers=[
{
:hostname => "web",
:ip => "192.168.100.10",
:box => "saucy",
:ram => 1024,
:cpu => 2
},
{
:hostname => "db",
:ip => "192.168.100.11",
:box => "saucy",
:ram => 2048,
:cpu => 4
}
]
然后,您只需迭代服务器数组中的每个项目并定义配置:
Vagrant.configure(2) do |config|
servers.each do |machine|
config.vm.define machine[:hostname] do |node|
node.vm.box = machine[:box]
node.vm.hostname = machine[:hostname]
node.vm.network "private_network", ip: machine[:ip]
node.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", machine[:ram]]
答案 2 :(得分:4)
Vagrant还具有方便的内存和CPU设置快捷方式,采用以下形式:
config.vm.provider "virtualbox" do |v|
v.memory = 1024
v.cpus = 2
end
因此,在您的情况下,并且为CPU框提供两倍的CPU和内存而不是Web框,它看起来像:
config.vm.define :web do |web_config|
web_config.vm.box = "saucy"
web_config.vm.host_name = "web"
web_config.vm.network "private_network", ip:"192.168.100.10"
web_config.vm.provider :virtualbox do |vb|
vb.memory = 1024
vb.cpus = 2
end
end
config.vm.define :db do |db_config|
db_config.vm.box = "saucy"
db_config.vm.host_name = "db"
db_config.vm.network "private_network", ip:"192.168.100.20"
db_config.vm.provider :virtualbox do |vb|
vb.memory = 2048
vb.cpus = 4
end
end
答案 3 :(得分:4)
我只是尝试使用上述解决方案并意识到,在我的环境中,所有内存设置都是相同的。代码段似乎有一个小错字 - 不要使用config.vm.provider ...
,而是使用<yourConfigVariable>.vm.provider...
来设置与机器相关的值。
AFAIK正确的代码如下所示:
config.vm.define :web do |web_config|
web_config.vm.box = "saucy"
web_config.vm.host_name = "web"
web_config.vm.network "private_network", ip:"192.168.100.10"
web_config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048"]
vb.customize ["modifyvm", :id, "--cpus", "2"]
end
end
config.vm.define :db do |db_config|
db_config.vm.box = "saucy"
db_config.vm.host_name = "db"
db_config.vm.network "private_network", ip:"192.168.100.20"
db_config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "1024"]
vb.customize ["modifyvm", :id, "--cpus", "2"]
end
end