Vagrant:使用 vagrant 将 docker run 参数传递给多个容器 [英] Vagrant: Passing docker run arguments to multiple containers using vagrant

查看:31
本文介绍了Vagrant:使用 vagrant 将 docker run 参数传递给多个容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 vagrant 来部署多 VM 环境,用于研究目的,到目前为止效果很好.但是现在我需要将每个 docker 容器固定到特定的 CPU 核心,但我不知道如何使用 vagrant 来做到这一点.我知道我可以使用 Vagrantfile 上的args"子句将--cpuset"参数传递给 docker run 命令,但我不知道如何在循环中使用它,因为我正在启动多个容器和我需要将每个容器固定到不同的 CPU 核心(例如,节点 1 固定到核心 #0,节点 2 固定到核心 #1 等).

I'm using vagrant to deploy a multiple VM environment, for research purposes, and it was great so far. But now i need to pin each docker container to a specific CPU core, but i don't know how to do that using vagrant. I know i can use the "args" clause on the Vagrantfile to pass the "--cpuset" parameter to the docker run command, but i don't know how to use it in a loop, since i'm launching multiple containers and i need to pin each container to a different CPU core (eg. node1 pins to core #0, node2 pins to core #1, etc).

我目前的 Vagrantfile 如下,没有 CPU Pinning 的东西:

My current Vagrantfile is as follows, without the CPU Pinning thing:

# 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

推荐答案

最后,我找错了地方.添加--cpuset-cpus"的正确位置在 config.vm.define 块中.

In the end, i was looking in the wrong place. The correct spot to add the "--cpuset-cpus" was in the config.vm.define block.

代码最终是这样的:

# 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:使用 vagrant 将 docker run 参数传递给多个容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆