10 Steps To Create Multiple Virtual Machines Using Vagrant

Last updated on Dec 15,2021 37.3K Views

10 Steps To Create Multiple Virtual Machines Using Vagrant

edureka.co

By now you must have seen how to install a single development environment (VM) using vagrant. In case you haven’t please refer to my previous blog before going through this one.

In this blog we are going to 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.

Requirement 1: Hadoop is one of the cutting-edge technologies today and it can be used by creating a hadoop cluster. At times you do not require a full-fledged cluster, but can manage with two servers only. However, you will need at least three datanodes. In this case, you may use one server as Namenode (since namenode has be a high-end machine) and in the other server you can create 3 VMs and use them as 3 datanodes.

Requirement 2: 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.

It is assumed that you have Vagrant and Virtual Box already installed. 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 vagrant_multi_edureka
$ cd vagrant_multi_edureka

Step 3: Initialize a new VagrantFile.

$ vagrant init

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

$ vagrant box add chef/centos-6.5

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 "edureka_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 edureka_vm{i}
				node.name = "edureka_vm#{i}"  
			end
		end
	end
end

Step 6: Let’s start the edureka_vms namely: edureka_vm1, edureka_vm2 and edureka_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:
                         edureka_vm1        –>        Host : 127.0.0.1    |   Port  : 2222
                         edureka_vm2        –>        Host : 127.0.0.1    |   Port  : 2200
                         edureka_vm3        –>        Host : 127.0.0.1    |   Port  : 2201

Step 7: Download putty (windows shh 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.

Got a question for us? Please mention it in the comments section and we will get back to you.

Related Posts:

Mystery Of Devops

Devops-Redefining-your-It-Strategy

Get Started with Devops

Upcoming Batches For Linux Administration Certification Training Course
Course NameDateDetails
Linux Administration Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES