VirtualBox is a freely available hardware virtualization software from Oracle that is widely used for creating virtual computers. With cloud computing getting more attention by both companies and individual developers, VirtualBox has become a de facto standard tool for cloud computing development. As a developer, if you only need to occasionally create a virtual machine, the graphical user interface provided by the software is very easy to use and gives you a great deal of options for you to change the characteristics of a virtual machine (VM). However, when you try to create a cluster of multiple VirtualBox virtual machines and utilize the various features of the networks provided by the software, the task of setting up the many machines by hand can be very time consuming and error prone. In this blog, I will first go over the types of networks VirtualBox provides, then introduce a set of vagrant scripts to show how you can quickly automate the process of creating a cluster of many VMs for your cloud needs such as setting up a multi-node OpenStack cloud.

VirtualBox allows a VM to have up to four network adapters, that is, a virtual computer can have up to four network interfaces, and each network interface can connect to the same or different network. The following two screenshots show the four available adapters and possible types for each adapter.

Virtual Box Panel 2
Virtual Box Networking Panel

The type of an adapter determines the behavior of that network interface. The types of the adapter provided by VirtualBox are Host-only, NAT, NAT Network, Bridged and Internal Network. Host-only adapter allows the host (the physical computer running VirtualBox) and the VM to communicate with each other but a VM with a Host-only adapter will not be able to connect to outside of the host such as internet. NAT adapter allows the VM to access the internet but no one can access the virtual machine unless port forwarding is setup. NAT network adapter is very similar to NAT but virtual machines on the same NAT network will be able to see each other and can access the internet but cannot be accessed by other machines including the host. Bridged adapter is like an adapter directly hooked up to the network that the host is hooked up to, the adapter will get the IP address in the same segment as the host network adapter, the host and the VM can access each other and can access the internet, the VM behaves just like another physical computer on the same network. The internal network adapter is similar to a NAT network adapter but those networks can only see each other, the VMs using the internal network adapter will not have access to any machines which are not on the same internal network. Because of all these types of network adapters, selecting right network adapters for your VMs can be confusing, having the wrong types of adapters for VMs can make your VMs not behave well, but may also produce security issues. To help make all of this easier, I have created a vagrant project to help with the task. At the bottom of the blog you can find the link to the project.

To use the project, you can simply download it from the link and make a few changes to the configuration file to meet your needs, then you can run a vagrant command to create the VMs that are defined in the configuration file. The project includes the typical Vagrantfile, as well as a configuration and a README file. The Vagrantfile contains code for VM creation and removal according to the configuration file. You will not need to make any changes to the Vagrantfile. All you have to do to create your own desired VMs is to make some changes to the configuration file. The configuration file is named nodes.conf.yml and it is a yaml file. All the VMs to be created should have an entry in this file.

The file nodes.conf.yml file has two sections:

1. Node section: this section defines virtual machine characteristics or types.
2. Machine section: this section defines what types of virtual machines should be actually created, you can have as many types of virtual machines defined in the Node section, but you may choose to only create one type of virtual machine to quickly test if the definition is correct.

Here is the content of the nodes.conf.yml file, the file was created to show various network options.


--- nodes.conf.yml
# The following defines server specifics
controller:
  quantity: 2
  cpus: 2
  customize: ['--nic2', 'natnetwork', '--nat-network2', 'NatNetwork1',
              '--nic3', 'natnetwork', '--nat-network3', 'NatNetwork2']

balancer:
  memory: 4096
  cpus: 1
  networks:
    - nettype: 'private_network'
      ip: '192.168.56.11'
      virtualbox__hostonly: 'vboxnet0'
      netmask: '255.255.255.0'
      adapter: 2
    - nettype: 'private_network'
      ip: '10.5.2.11'
      virtualbox__intnet: 'myintnet'
      netmask: '255.255.255.0'
      adapter: 3

compute:
  quantity: 3
  memory: 4096
  cpus: 2
  networks:
    - nettype: 'public_network'
      bridge: 'en4: Thunderbolt Ethernet'
      adapter: 2


# The following list defines the sequence to provision servers
machines:
  - controller
  - balancer
  - compute

In the example above, the node section defines a total of three VM types, a controller, balancer and computer. The controller type of VMs will have two CPUs, 2GB of memory by default since the memory field is missing, and two additional network cards both using a NAT network adapter. The quantity field indicates that there will be two instances of this type of VM created, the first will be named controller-0, and the second will be named controller-1. The balancer type of VMs should have 4GB of memory, one CPU and two additional network adapters, one network is Host-only and one network is defined as an Internal network. Only one instance of the balancer VM will be created. That instance will be named balancer-0. The compute type of VMs should have 4 GB of memory, 2 CPUs and one additional network adapter using the bridged network. The quantity of 3 indicates that there will be three instances of compute VM created. The type name of the VMs are important since that name will be used as part of the virtual machine hostname, and when you look for VMs in your file system, these names will be the folder names which represent each VM.

The machine section controls the virtual machine creation sequence. The above example shows that VMs of the controller type will be created first, then balancer VM, lastly the compute VMs. If you would only want to create compute VMs, you can simply comment out the line – controller and – balancer in the machine section, run the vagrant script, only compute VMs will be created even though this configuration file has more types defined.

With the project, you can define many virtual machine types with many different configuration options, then specify how many instances for each type by using the quantity field in the configuration file, you can create many virtual machines with very little work.

To create VMs, simply run the following command:

vagrant up

To destroy all the VMs, simply run the following command:

vagrant destroy

More information can be found in the README file of the project. The project is hosted by github.com as an open source project, it can be found by following the below link:

https://github.com/litong01/vbvagrant

References:

1.https://github.com/litong01/vbvagrant vagrant script project to create virtual machines
2.https://www.virtualbox.org/ Official VirtualBox site
3.https://www.vagrantup.com/ Official Vagrant site
4.https://atlas.hashicorp.com/boxes/search Hashicorp vagrant box image site

Join The Discussion

Your email address will not be published. Required fields are marked *