Part 5 – Running Multiple VMs using Vagrant

In our earlier posts, we have seen how to run a VM using Vagrant and in this post, we shall see how we can deploy multiple VMs and how to change their configurations. But where do we change these configs? you guessed it right; the same vagrantfile that we used for the first VM.Let’s quickly look at available boxes in the Vagrant.

1

So here, i have two boxes, a Ubuntu and a CentOS. Let’s configure them in the Vagrantfile. I wanna deploy two ubuntu machines and one centos machine. Let’s see how we do that.The current config is as below.

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty32"
end

Let’s add our Ubuntu VM’s and CentOS VM, and the vagrantfile changes as below.

Vagrant.configure("2") do |config|
 config.vm.define "ubuntua" do |ubuntua|
   ubuntua.vm.box = "ubuntu/trusty32"
   ubuntua.vm.hostname = 'Ubuntu-A'
 end
 config.vm.define "ubuntub" do |ubuntub|
   ubuntub.vm.box = "ubuntu/trusty32"
   ubuntub.vm.hostname = 'Ubuntu-B'
 end
 config.vm.define "centos" do |centos|
   centos.vm.box = "centos/7"
   centos.vm.hostname = 'centos-A'
 end
end

Well, these are the minimum settings needed for a vm. Let’s just try running vagrant up now.

1

So, what we did is correct, VMs started coming up fine and if you see the highlited thing, it should change from Default to name of the variable we gave in Vagrantfile. To verify if all the VMs are running fine, use the vagrant status

1

Now, lets look at other configurations like changing the RAM size on VMs and changing the network. Note that, your host system must have sufficient memory available for all the VMs to run smooth.

From the previous screen, observe that, by default, vagrant up starts all the VMs, but most of the times we do not want that to happen and we may have other VMs in vagrant used completely for a different purpose, in such scenarios, we can prevent VMs from starting up. Set autostart to false (next screenshot shows this)

To customize the VM settings, use the VBoxManage utility which can modify the VM setting before booting up. This can be done from command line.

config.vm.provider "virtualbox" do |v|
      v.customize ["modifyvm", :id, "--cpuexecutioncap", "40"]
end

To change the number of CPUs and amount of RAM

 config.vm.provider "virtualbox" do |v|
     v.memory = 1024
     v.cpus = 2
 end

For our testing, we are configuring the RAM for ubuntu VMs as 1024MB and CentOS as 3072MB. After modifications, Vagrantfile should be like below

Vagrant.configure("2") do |config|
 config.vm.define "ubuntua" do |ubuntua|
   ubuntua.vm.box = "ubuntu/trusty32"
   ubuntua.vm.hostname = 'Ubuntu-A'
   config.vm.provider "virtualbox" do |v|
     v.memory = 1024
   end
  end 
 config.vm.define "ubuntub" do |ubuntub|  
   ubuntub.vm.box = "ubuntu/trusty32"  
   ubuntub.vm.hostname = 'Ubuntu-B'
   config.vm.provider "virtualbox" do |v|
     v.memory = 1024
   end
 end 
config.vm.define "centos" ,autostart:false do |centos|  
  centos.vm.box = "centos/7"  
  centos.vm.hostname = 'centos-A'  
  config.vm.provider "virtualbox" do |v|
    v.memory = 3072
  end 
end

Also we can change the forwarding ports from default, this is mainly useful when there are multiple projects running on the same host are using the same ports.

To make a  vm as a default, use the primary keyword.

config.vm.define "ubuntua", primary: true do |ubuntua|
ubuntua.vm.network :forwarded_port, guest: 22, host: 2387, id: "ssh"

Hope this was informative. Thanks!

Part 1 – Install Vagrant on CentOS 7

Part 2 – Install VirtualBox on CentOS 7

Part 3 – Running a VM with Vagrant

Part 4 – Add,Update and Remove a Box in Vagrant

Part 5 – Running Multiple VMs using Vagrant

Advertisements

#box-configs, #portforwarding, #vagrantfile