Luis Carlos Jersak
Luis Carlos Jersak

Reputation: 51

Vagrant: Passing docker run arguments to multiple containers using vagrant

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).

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

Upvotes: 1

Views: 1133

Answers (2)

Luis Carlos Jersak
Luis Carlos Jersak

Reputation: 51

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.

The code ended being like this:

# 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

Upvotes: 1

Liping Huang
Liping Huang

Reputation: 4476

Suppose you want to config the vagrant vm in the loop, as the vagrantfile is based on the ruby language, so you can involve the development availablty to the vagrantfile, here is an example, you can add your "--cpuset" config in the 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 to define the vm, you can define your

{
  "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
    }
  }
}

Upvotes: 0

Related Questions