我出于研究目的使用vagrant来部署多个VM环境,到目前为止它还很棒。但是现在我需要将每个docker容器固定到一个特定的CPU核心,但我不知道如何使用vagrant来做到这一点。我知道我可以使用" args" Vagrantfile上的子句传递" - cpuset" docker run命令的参数,但我不知道如何在循环中使用它,因为我启动了多个容器,我需要将每个容器固定到不同的CPU核心(例如,node1引脚到核心#0,节点2引脚到核心#1等。
我目前的Vagrantfile如下,没有CPU Pinning的东西:
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
# choose how many machines the cluster will contain
N_VMS = 32
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "tknerr/baseimage-ubuntu-14.04"
config.vm.network "private_network", ip: "192.168.121.2"
config.vm.provider "docker" do |v|
v.has_ssh = true
end
hosts_file = []
1.upto(N_VMS) do |i|
config.vm.define vm_name = "node#{i}" do |config|
config.vm.hostname = vm_name
end
end
script = <<-SCRIPT
apt-get -y update
apt-get -y install libcr-dev mpich2 mpich2-doc arp-scan openssh-server nano make
SCRIPT
script.sub! 'N_VMS', N_VMS.to_s
config.vm.provision "shell", inline: script
end
答案 0 :(得分:0)
假设你想在循环中配置vagrant vm,因为vagrantfile是基于ruby语言的,所以你可以将开发可用性包含在vagrantfile中,这里有一个例子,你可以添加你的“--cpuset”在vm define中配置。
# -*- mode: ruby -*-
# vi: set ft=ruby :
# read vm and chef configurations from JSON files
nodes_config = (JSON.parse(File.read("nodes.json")))['nodes']
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
nodes_config.each do |node|
node_name = node[0] # name of node
node_values = node[1] # content of node
# puts node_name
# puts node_values['box']
config.vm.define node_name do |config|
config.vm.box = node_values['box']
config.vm.hostname = node_name
config.vm.network :private_network, ip: node_values['ip']
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", node_values['memory']]
vb.customize ["modifyvm", :id, "--name", node_name]
end
end
end
nodes.json
定义vm,您可以定义
{
"nodes": {
"jenkins.example.com": {
"info": "jenkins master server",
"box": "../Vgrant-boxes/centos65_virtualbox_50G.box",
"ip": "192.168.35.101",
"ports": [],
"memory": 512
},
"node01.example.com": {
"info": "tomcat app host server",
"box": "../Vgrant-boxes/centos65_virtualbox_50G.box",
"ip": "192.168.35.121",
"ports": [],
"memory": 512
},
"node02.example.com": {
"info": "jboss app host server",
"box": "../Vgrant-boxes/centos65_virtualbox_50G.box",
"ip": "192.168.35.122",
"ports": [],
"memory": 512
},
"node03.example.com": {
"info": "oracle xe server",
"box": "../Vgrant-boxes/centos65_virtualbox_50G.box",
"ip": "192.168.35.123",
"ports": [],
"memory": 512
},
"node04.example.com": {
"info": "artifactory server",
"box": "../Vgrant-boxes/centos65_virtualbox_50G.box",
"ip": "192.168.35.124",
"ports": [],
"memory": 512
}
}
}
答案 1 :(得分:0)
最后,我在找错了地方。添加“--cpuset-cpus”的正确位置在config.vm.define块中。
代码结束如下:
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
# choose how many machines the cluster will contain
N_VMS = 32
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "tknerr/baseimage-ubuntu-14.04"
config.vm.network "private_network", ip: "192.168.121.2"
config.vm.provider "docker" do |v|
v.has_ssh = true
end
2.upto(N_VMS+1) do |i|
config.vm.define vm_name = "node#{i}" do |config|
# CPU PINNING CONFIG
config.vm.provider "docker" do |docker|
docker.create_args = ['--cpuset-cpus=' + ((i/2)-1).to_s]
end
config.vm.hostname = vm_name
end
end
script = <<-SCRIPT
apt-get -y update
apt-get -y install libcr-dev mpich2 mpich2-doc arp-scan openssh-server nano make
SCRIPT
script.sub! 'N_VMS', N_VMS.to_s
i=1
config.vm.provision "shell", inline: script
end