Setting Up My Virtual Laboratory

By | June 23, 2016
My virtual laboratory in its finished state. Photo by Yovko Lambrew.

My virtual laboratory in its finished state. Photo by Yovko Lambrew.

Some time ago I began thinking about setting up an environment in which I could test, for instance, running Dockerized applications on multiple computers.
For a while I even considered a real server until I realised that it not only costs a lot of money to buy a server, but that the electricity bills will be affected as well.
In addition some simpler tests revealed that my quad-core i7 CPU will last quite a long way, so I decided to set up a virtual laboratory consisting of four virtual machines.

These instructions are applicable regardless of whether your main(host) operating system is Mac OS X, Windows or Linux. Some adjustments may be necessary.

Virtualization

I chose VirtualBox because I only want to run virtual servers and thus do not need any graphics facilities and because it is free. In addition, VirtualBox runs on Windows, Mac OS and Linux and I recall having read that, provided the hardware requirements are satisfied, I can move/copy a virtual machine from one host OS to another. In addition to the basic VirtualBox installation, I also installed the extensions pack.

Operating System

The operating system of choice for my virtual laboratory is Ubuntu Server 16.04 64-bit. It is a Long-Term Support version, so it will be around for some time. The main motivation for the choice of OS is that I have more experience with Ubuntu than with other Linux distributions.
Since I want to be able to run Docker on my virtual machines, I made sure that Ubuntu is one of the supported Linux distributions and that 16.04 is indeed a supported version.

Create the First Virtual Machine

A virtual machine. Photo by Feliciano Guimarães.

A virtual machine in my laboratory. Photo by Feliciano Guimarães.

Most aspects of the configuration of the virtual machines used for my lab can be modified to suit your needs, your hardware specifications etc. What should not be changed is the network configuration, which has been chosen to allow for communication both to and from the virtual machines, including communication between the virtual machines.

I started out with creating one virtual machine, which were later cloned.

  • In VirtualBox click New in the upper left corner of the Manager window.
  • If there is a button with the label Expert Mode in the window that appears, click that button.
    This will give us more detailed control over the configuration of the new virtual machine.
  • Enter the name for the virtual machine.
    I chose Laboratory 1.
  • Select Linux for the Type.
  • Select Ubuntu (64-bit) for the Version.
    If you are planing on using another Linux distribution, change this accordingly.
  • Enter 5120 as the memory size in megabytes.
    If I have four virtual machines running at the same time, they may consume up to 20GB of RAM. Adjust the size according to your needs. It is possible to modify the memory size of a virtual machine at some later point in time. If the memory cannot be adjusted, discard the saved state of the virtual machine by right-clicking on the virtual machine in the Manager window and selecting Discard Saved State.
  • Select the Create a Virtual Hard Disk Now option in the Hard Disk section.
  • Click the Create button.

– Create a New Hard-Disk

  • Name the new hard-disk.
    I’ll call mine Laboratory 1 HD. In addition you may select a custom location for the image file.
  • Set the size of the new hard-disk.
  • Select the hard-disk file type.
    I left mine at the default, VDI, type.
  • Select storage on physical hard-disk.
    I also left this one at the default, dynamically allocated, setting.
  • Click Create.

The new virtual machine and its new hard-disk is now created, but it needs some additional configuration.

– System Settings

  • Select the new virtual machine in the Manager window and click the Settings symbol.
  • Select the System tab.
  • In the System tab, select the Processor tab.
  • Select the number of CPU cores for the virtual machine.
    The VirtualBox documentation suggests not to chose more cores than the number of physical cores in your system.
  • Select an execution cap, if desired.
    I left mine at 100%, that is no execution cap.
  • Check the enable PAE/NX checkbox.
    According to the VirtualBox documentation and other sources, Ubuntu Server require Physical Address Extension.

– Network Settings

This is the important part that will allow the virtual machines in the laboratory to communicate with each other.

  • Select the Network tab.
  • For Adapter 1, select Bridged Adapter at Attached To.
Laboratory virtual machine network configuration.

Laboratory virtual machine network configuration in VirtualBox.

– Install Ubuntu Server

The configuration of the virtual machine is now done and we are ready to install Ubuntu Server, or the OS of your choice.

  • Download Ubuntu Server.
  • Open the Storage Settings for the Laboratory 1 virtual machine.
  • Click the small CD icon to the right of the Optical Drive popup-menu and select the Ubuntu Server iso image you just downloaded.
  • Click the OK button.
Selecting the Ubuntu Server installation ISO.

Selecting the Ubuntu Server installation ISO in VirtualBox.

  • Click the Start icon in the icon-bar of the VirtualBox Manager window to start the virtual machine.
  • Install Ubuntu Server according to these instructions.
    When being asked for a host name, I entered “laboratory1” (without quotes).
    Make note of the user-name and password entered during the installation process.
    When being asked about disk partitioning, I chose “Guided – use entire disk and set up LVM”.
    In the Software Selection dialog, I chose to install the following predefined collections of software: standard system utilities and OpenSSH server.
    Finally, I chose to install the GRUB boot-loader onto the virtual hard-disk.
  • Reboot the virtual machine.
  • Log in to the virtual machine in the VirtualBox terminal window.
  • Find the IP of the virtual machine using the command “ifconfig”.
    The following is an example of output showing the IP of my virtual machine:
  • If you prefer using a better terminal program to connect to the virtual machine:
    Log out of the virtual machine in the VirtualBox terminal window.
    Open a terminal window using your favourite program (examples: Terminal in OS X, PuTTY in Windows).
    Connect to the virtual machine using the IP, user-name and password.
    Example: In OS X or Linux, I use the SSH command: ssh user@192.168.1.67
  • Update the software in the virtual machine.
    In addition to the commands below, I had to explicitly install some packages that were held back.

    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get autoremove
    sudo apt-get autoclean

– Install Docker

  • Install Docker using the instructions here.
  • Enable the Docker remote API using instructions in a previous article.

– Install Metricbeat for Monitoring

I want to be able to monitor the machines in my virtual laboratory, so I will install Metricbeat. Feel free to skip this step.

  • Add Elastic’s Beats repository:
    This is to get automatic updates when a stable version of Metricbeat is one day released.

    curl https://packages.elasticsearch.org/GPG-KEY-elasticsearch | sudo apt-key add -
    echo "deb https://packages.elastic.co/beats/apt stable main" |  sudo tee -a /etc/apt/sources.list.d/beats.list
    sudo apt-get update
  • Install Metricbeat:
    If there is a realse of Metricbeat, you should be able to install it using “sudo apt-get install metricbeat” or similar, otherwise use the following commands.

    wget https://download.elastic.co/beats/metricbeat/metricbeat-5.0.0-alpha3-amd64.deb
    sudo dpkg -i metricbeat-5.0.0-alpha3-amd64.deb
    rm metricbeat-5.0.0-alpha3-amd64.deb

– Set a Static IP

For convenience, I am going to set a static IP address of the virtual machine.

  • Find the IP of your router.
    In OS X, the router IP is displayed in the Network preferences panel.
    On Windows, use the “ipconfig” command in a terminal window and look for the default gateway IP address.
    My router’s IP address is 192.168.1.254.
  • In the virtual machine, issue the command:
    sudo vi /etc/network/interfaces
    The original file looks like this in my Ubuntu Server installation:

    # This file describes the network interfaces available on your system
    # and how to activate them. For more information, see interfaces(5).
    
    source /etc/network/interfaces.d/*
    
    # The loopback network interface
    auto lo
    iface lo inet loopback
    
    # The primary network interface
    auto enp0s3
    iface enp0s3 inet dhcp

    Modify the file inserting the desired IP address of your virtual machine (192.168.1.180 in my case), the netmask and the router (gateway) IP address. My resulting file looks like this:

    # This file describes the network interfaces available on your system
    # and how to activate them. For more information, see interfaces(5).
    
    source /etc/network/interfaces.d/*
    
    # The loopback network interface
    auto lo
    iface lo inet loopback
    
    # The primary network interface
    auto enp0s3
    iface enp0s3 inet static
    address 192.168.1.180
    netmask 255.255.255.0
    gateway 192.168.1.254
    dns-nameservers 8.8.8.8
  • Shut down the virtual machine.

The first virtual machine in the laboratory is now ready.

Create a Virtual Machine Clone

The procedure outlined in this section describes how to make one clone of the first virtual machine in the laboratory. Repeat it until you have the desired number of virtual machines in your laboratory, adjusting parameters where indicated.

  • In the VirtualBox Manager window, right-click the Laboratory 1 virtual machine and select Clone.
  • If there is a button saying Expert Mode in the dialog that appears, click this button.
  • Enter “Laboratory 2” as name of the new machine.
    I will adjust the digit at the end of the names so I will have virtual machines named Laboratory 1, Laboratory 2, Laboratory 3 etc.
  • Select Full Clone for the Clone type.
  • Select Current machine state under Snapshots.
  • Check the checkbox Reinitialize the MAC address of all network cards.

    Clone a laboratory virtual machine.

    Clone a laboratory virtual machine.

  • Click the Clone button.
  • Wait.

Once the clone has been created, we need to make some adjustments to the new virtual machine, in order for it to be run in parallel with it’s predecessor. Start the virtual machine and log into

– Modify the Hostname

Each virtual machine in the virtual laboratory need to have an unique hostname, otherwise certain software may confuse two virtual machines.

  • Edit the file /etc/hostname.
    sudo vi /etc/hostname

    This file should contain “laboratory1”. For the first virtual machine in the laboratory I change this to “laboratory2”, then “laboratory3” for the next etc.

  • Edit the file /etc/hosts.
    sudo vi /etc/hosts

    Change the name, which should be “laboratory1”, to the right of the IP address 127.0.1.1 in so that it matches the name in /etc/hostname.

– Change the Static IP Address

Each virtual machine in the laboratory needs an unique IP address.

  • Edit the file /etc/network/interfaces:
    sudo vi /etc/network/interfaces

    As earlier, the contents should look like this:

    # This file describes the network interfaces available on your system
    # and how to activate them. For more information, see interfaces(5).
    
    source /etc/network/interfaces.d/*
    
    # The loopback network interface
    auto lo
    iface lo inet loopback
    
    # The primary network interface
    auto enp0s3
    iface enp0s3 inet static
    address 192.168.1.180
    netmask 255.255.255.0
    gateway 192.168.1.254
    dns-nameservers 8.8.8.8
  • Modify the highlighted line that starts with the word “address”.
    For the “Laboratory2” virtual machine I set the IP address to “192.168.1.181”, then “192.168.1.182” for the next virtual machine in the laboratory etc.

– Remove the Docker Node Identity Information

Once run, Docker will create a file containing information that for instance is used to identify the node in a Docker swarm. This file must be recreated, in order for Docker not to confuse two Docker nodes. The easiest way is to delete the file – Docker will then create a new file next time the Docker daemon is started.

  • Stop the Docker daemon.
    sudo service docker stop
  • Remove the Docker key-file.
    sudo rm -f /etc/docker/key.json

– Shut Down the Clone

We have made the necessary modifications to the new virtual machine and can now shut it down.

  • Shut down the virtual machine.
    sudo shutdown
    exit

Repeat the process for any additional virtual machines you want in your laboratory. I will create a total of four virtual machines for my laboratory.

Snapshot the Virtual Machines of the Laboratory

When you are done creating the virtual machines of your laboratory, you will want to take a snapshot of each of the machines. This will enable you to restore the state of one or more of the virtual machines in case something goes wrong or in case you simply want to clean up and start something from scratch.

  • Select a virtual machine in the VirtualBox Manager window.
    I will start selecting the “Laboratory 1” virtual machine.
  • Click the Snapshots button in the upper right corner of the VirtualBox Manager window.
  • Click the little camera icon marked in the figure below to create a snapshot of the selected virtual machine.
    Taking a snapshot of a virtual machine in VirtualBox.
  • Name the snapshot.
    I’ll call my snapshots “Base state”.
  • Repeat for the remaining virtual machines.

Group the Virtual Machines of the Laboratory

VirtualBox allows for grouping of virtual machines and, for instance, running and stopping all the machines in a group as a unit.

  • In the VirtualBox Manager window, select the virtual machines of your laboratory.
  • Go to the Machine menu and apply the Group command.
  • Right-click the group title bar and select Rename Group…
  • Give your group a suitable name.
    I’ll simply call mine “Virtual Laboratory”.
  • Again, right-click the group title bar but now select Start -> Normal Start.
  • Watch all the virtual machines start.

Occasionally I have encountered file not found errors for hard-disk images of virtual machines that have been added to a group. To fix such problems, edit the vbox-file of the virtual machine. If the name of your virtual machine is “Laboratory 1”, then the name of the vbox-file will be “Laboratory 1.vbox”.

  • Stop all virtual machines.
  • Quit VirtualBox.
    This is important, since if you do not quit VirtualBox it will overwrite any changes you make to vbox-files.
  • Open the vbox-file in question with a text-editor.
  • Locate the configuration of the virtual machine’s hard-disk.
    It should look something like this – note the location-attribute of the <HardDisk> element on row three:

        <MediaRegistry>
          <HardDisks>
            <HardDisk uuid="{e79ba341-0a9d-4ba7-88bd-82b49c77f6b3}" location="Laboratory 1.vdi" format="VDI" type="Normal">
              <HardDisk uuid="{90cdb471-5bba-43d7-afd6-5fc1200144a4}" location="Snapshots/{90cdb471-5bba-43d7-afd6-5fc1200144a4}.vdi" format="VDI"/>
            </HardDisk>
          </HardDisks>
          <DVDImages>
  • If the value of the location-attrbute contains a fully qualified path, remove everything except for the actual file name of the hard-disk image.
    The result should be like in the example above.
  • Save the file.

Virtual Laboratory CentOS Version

The following are adaptions needed for a CentOS-based virtual laboratory.

CentOS First Virtual Machine

  • Download a CentOS ISO image.
    I downloaded the “Everything ISO” from this webpage.
  • Install CentOS on the first virtual machine in the laboratory.
    Configure the installation to set a static IP address for the virtual machine. I used 192.168.1.190 as to be able to run my Ubuntu virtual laboratory at the same time.
  • Boot into the newly installed CentOS virtual machine.
  • Update the OS software.
    sudo yum update
    sudo yum upgrade
    sudo yum clean
  • Install Docker.
    Follow the instructions here, but do not start the Docker daemon.
  • Set the hostname.
    I set the hostname to “laboratory1”:

    hostnamectl set-hostname laboratory1 --static
  • Optionally create keys for SSH login.
    If you are to use these keys with DC/OS then do not set a password.

    ssh-keygen -t rsa
  • Shut down the CentOS virtual machine.

Create CentOS Clones

As with the Ubuntu version of my virtual laboratory, I create three more virtual machines which I name “laboratory2”, “laboratory3” and “laboratory4”. These machines get the static IPS 192.168.1.191, 192.168.1.192 and 192.168.1.193 respectively.

I use the following procedure to create one clone:

  • In VirtualBox, create a clone of the “laboratory1” virtual machine.
    The procedure is identical to that of the Ubuntu virtual laboratory above, creating a full clone with a reinitialized MAC address.
  • Start the new virtual machine.
  • Log in to the new virtual machine using SSH.
  • Change the IP address of the new virtual machine.
    Change the IPADDR value using the VI editor:

    sudo vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
  • Change the hostname of the new virtual machine.
    Replace “laboratory2” in the following command with the desired hostname.

    hostnamectl set-hostname laboratory2 --static
  • Shut down the new virtual machine.
  • Repeat as desired.

Finally take a snapshot of all the virtual machines in the laboratory using the same procedure as with the Ubuntu virtual laboratory.

Final Words

Your brand new, shiny, virtual laboratory is now ready for use. I will put mine to use in an upcoming article, so stay tuned!

If you are using Mac OS X, this article shows you how to create a Window Group that can be used to arrange windows and open SSH connections to the virtual machines in your laboratory in a very convenient way.

Happy coding!

Leave a Reply

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