Vagrant is an awesome tool when you need to have a development or test environment setup locally quickly. For more detailed information about Vagrant itself and how to install/use it, I refer you to the official documenation.

The use case: How nice would it be to test some multi-node deployment on your local machine without too much hassle? Vagrant to the rescue! Just create a file called “Vagrantfile” in any directory on your system with the following content:

nodes = {
    node: { count: 5, ip_start: 11, box: 'centos6'}
}

Vagrant.configure("2") do |config|
  nodes.each do |prefix, cfg|
    cfg[:count].times do |i|
      hostname = "%s-%02d" % [prefix, (i+1)]

      config.vm.define hostname do |box|
        box.vm.box = cfg[:box]
        box.vm.host_name = "#{hostname}.vagrant.internal"
        box.vm.network :private_network, 
                       ip: "33.33.33.#{cfg[:ip_start]+i}"
        box.vm.provider 'virtualbox' do |vb|
          vb.customize [ 'modifyvm', :id,
                         '--name', hostname, 
                         '--memory', cfg[:memory] || 512
                       ]
        end
      end
    end
  end
end

If you don’t have any base images (in this case “centos6”), you can either roll your own or get one from here.

The above Vagrantfile defines 5 VM nodes named “node-01” through “node-05″. Each node gets an IP address assigned (from 33.33.33.11 to 33.33.33.15) and a hostname. By default, each node will get 512 MB of memory allocated to it

$ vagrant status
Current machine states:
 
node-01                   not created (virtualbox)
node-02                   not created (virtualbox)
node-03                   not created (virtualbox)
node-04                   not created (virtualbox)
node-05                   not created (virtualbox)

To launch 1 or more of these VMs:

$ vagrant start node-01
$ vagrant start node-02
$ vagrant start node-03

To connect to one of them

$ vagrant ssh node-03

From this point on you can do whatever you want with the VMs to perform your tests. After you’re done testing, simply destroy the VMs again

$ vagrant destroy node-01
$ vagrant destroy node-02
$ vagrant destroy node-03

Enjoy!