FoxuTech

Create Multiple Virtual Machines Using Vagrant

Create Multiple Virtual Machines Using Vagrant

Let’s see how to create multiple VMs using vagrant. But before we start, we must understand the requirements where we need to create multiple VMs of the same configuration.

You are working in a production environment where you are dealing with a big project. As all of us are aware that before we deploy the project on the live server, the project has to be developed on the development server and tested on the test server. These servers are of the same configuration. Hence, we may use vagrant to create these servers all together instead of building them individually.

There can be many other requirements where you may leverage this feature. I would encourage you to comment your requirement on this blog so that it can help others as well.

Please make sure you have installed Vagrant and Virtual Box. Now, let’s see how to create multiple VMs using vagrant:

Step 1: Open the terminal (Linux or Mac) or command prompt (Windows)

Step 2: Create a new directory for vagrant:

# mkdir multi_vagrant_foxutech

# cd multi_vagrant_foxutech

Step 3: Initialize a new VagrantFile.

# vagrant init

Step 4: Install a vagrant box. We are using “ubuntu/trusty64” for this blog. You can see the list of boxes here.

# vagrant box add ubuntu/trusty64

Step 5: Update the Vagrant File as below:

# This defines the version of vagrant

Vagrant.configure(2) do |config|

# Specifying the box we wish to use

config.vm.box = “chef/centos-6.5”

# Adding Bridged Network Adapter

config.vm.network “public_network”

# Iterating the loop for three times

(1..3).each do |i|

# Defining VM properties

config.vm.define “foxutech_vm#{i}” do |node|

# Specifying the provider as VirtualBox and naming the VM’s

config.vm.provider “virtualbox” do |node|

# The VM will be named as foxutech_vm{i}

node.name = “foxutech_vm#{i}”

end

end

end

end

Step 6: Let’s start the foxutech_vms namely: foxutech_vm1, foxutech_vm2 and foxutech_vm3:

# vagrant up

Congratulations! You have created three VMs using single vagrant file. You must be wondering how to use it. You can access it using ssh.
You can connect the VMs using the below host and port number:
                         foxutech_vm1        –>        Host : 127.0.0.1    |   Port  : 2222
                         foxutech_vm2        –>        Host : 127.0.0.1    |   Port  : 2200
                         foxutech_vm3        –>        Host : 127.0.0.1    |   Port  : 2201

Step 7: Download putty (windows ssh client) from here. Run the application and connect to the VMs.

Step 8: You need to enter the username and password to login into the VM. It is same for all the three VMs. Please use the below credentials:

                                         Username : vagrant    |   Password : vagrant

Step 9: You need to log in to each VM separately using putty in order to access them.

Step 10: Finally, you need to understand if this fits your requirement or not. Accordingly, you will understand how to use vagrant to get maximum benefit.

Exit mobile version