Vagrant:使用 vagrant 将 docker 运行 参数传递给多个容器
Vagrant: Passing docker run arguments to multiple containers using vagrant
出于研究目的,我正在使用 vagrant 部署多 VM 环境,到目前为止效果很好。但现在我需要将每个 docker 容器固定到特定的 CPU 核心,但我不知道如何使用 vagrant 来做到这一点。我知道我可以使用 Vagrantfile 上的 "args" 子句将“--cpuset”参数传递给 docker 运行 命令,但我不知道如何在循环,因为我要启动多个容器,我需要将每个容器固定到不同的 CPU 核心(例如,node1 固定到核心 #0,node2 固定到核心 #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
假设你想在循环中配置vagrant vm,因为vagrantfile是基于ruby语言的,所以你可以涉及到vagrantfile的开发可用性,这里是一个例子,你可以添加您在 vm 定义中的“--cpuset”配置。
# -*- 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
}
}
}
结果我找错地方了。添加“--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
出于研究目的,我正在使用 vagrant 部署多 VM 环境,到目前为止效果很好。但现在我需要将每个 docker 容器固定到特定的 CPU 核心,但我不知道如何使用 vagrant 来做到这一点。我知道我可以使用 Vagrantfile 上的 "args" 子句将“--cpuset”参数传递给 docker 运行 命令,但我不知道如何在循环,因为我要启动多个容器,我需要将每个容器固定到不同的 CPU 核心(例如,node1 固定到核心 #0,node2 固定到核心 #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
假设你想在循环中配置vagrant vm,因为vagrantfile是基于ruby语言的,所以你可以涉及到vagrantfile的开发可用性,这里是一个例子,你可以添加您在 vm 定义中的“--cpuset”配置。
# -*- 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
}
}
}
结果我找错地方了。添加“--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