Chapter 5
Virtualisation
There was a time when virtualisation was considered a rather exotic solution for IT systems but nowadays it is considered essential.
In Chapter 2 we installed VirtualBox Oracle’s virtualisation tool. We will be using VirtualBox for all our virtualisation. We also installed Vagrant a handy tool from HashiCorp that simplifies the programmatic definition of our local virtual machines. In this chapter we look at creating a virtual server both manually and using Vagrant.
5.1 Creating a Virtual Server with VBoxManage
VBoxManage is the Command Line Interface (CLI)1 to VirtualBox. I am using the CLI rather than the more common Graphical User Interface (GUI)23 for two reasons:
- Education; using the CLI discloses more about how a VM is put together in VirtualBox, this leads to a better understanding of virtualisation.
- System as Code; a central tenet of the technical aspects of DevOps is the system as code. Graphical interfaces are not easy to handle programmatically whereas a CLI is ideal for scripting.
Readers interested in more detail of virtualisation and VirtualBox specifically may find VirtualBox from Scratch[Boo20f] useful.
5.2 Setting up a simple virtual machine
Let’s create our first virtual machine.
Host operating system
The following commands should work on MacOS, Linux, or Windows 104
First create a directory within which we will create our virtual machine and make this our current working directory.
1mkdir vbclass 2cd vbclass
Now open a command line terminal and download the installation media we need to build our server. Since we are building a Debian server we download a Debian installation ISO.
1curl -O https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/\ 2 debian-10.9.0-amd64-netinst.iso -L
curl is generally available as a default command line tool nowadays but you may need to install it on your host (or just download the ISO via your browser).
Now we run a series of VBoxManage commands to setup the virtual hardware for out virtual machine.
1VBoxManage createvm --name "vbdemo" --register --basefolder "$(pwd)" 2VBoxManage createhd --filename vbdemo/vbdemo.vdi --size 20000 3VBoxManage storagectl "vbdemo" --name "SATA Controller" --add sata 4VBoxManage storageattach "vbdemo" --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium vbdemo/vbdemo.vdi 5VBoxManage storageattach "vbdemo" --storagectl "SATA Controller" --port 1 --device 0 --type dvddrive --medium ./debian-10.9.0-amd64-netinst.iso 6VBoxManage modifyvm "vbdemo" --memory 8192 7VBoxManage modifyvm "vbdemo" --nic1 bridged --bridgeadapter1 en1 8VBoxManage modifyvm "vbdemo" --ostype Debian
We start by creating the base VM settings file and registering the VM with the VirtualBox library (line 1). The settings file is an XML file called vbdemo.vbox in a vbdemo subdirectory for the current working directory. You will also notice that looking in the VirtualBox library that the new VM is registered.
Once this base VM settings file is available we can start adding custom hardware to our virtual machine.
Line 2 creates a virtual disk 20GB in size in the same directory as the settings file. This new disk is empty when created.
Line 3 creates a SATA storage controller in our new VM and then lines 3 and 4 associate disks with this controller. Line 3 associates the virtual disk we just created in line 2 and line 4 associates the Debian installation ISO downloaded previously. Line 4 is the equivalent of loading a Debian installation CD into a CD drive attached to the virtual machine (note the --type dvddrive option on line 4).
Line 5 gives our memory a boost to 8GB, you may need to modify this to suit your host machine (it’s generally a bad idea to assign more virtual memory to a virtual machine than you have physical memory on your host machine).
Line 6 configures a network interface card (nic) on the virtual machine. In this set up we are simply creating a bridged network interface, which means the virtual machine will we network device en1 as if it were attached to your local network.
Line 7 tells VirtualBox to expect a Debian operating system on this virtual machine.
After running these commands we have the following situation.
- A directory containing a virtual machine setting file (vbdemo/vbdemo.vbox) and a virtual disk (vbdemo/vbdemo.vdi).
- The settings file has been modified so that the virtual machine:
- has 8 GB RAM
- a SATA controler with two drives attached:
- the virtual 20GB hard drive (vbdemo/vbdemo.vdi)
- a DVD drive containing the Debian installer (./debian-10.6. 0-amd64-netinst.iso)
- a network interface card (en1) ‘wired’ to your host computer’s network
When we start this virtual machine the hardware will present as specified in the settings file and, as the hard drive has nothing installed on it, the system will boot from the virtual DVD drive, beginning the Debian installer.
Start the virtual machine.
1VBoxHeadless --startvm "vbdemo" &
This will start the virtual machine ‘headless’, without a virtual display attached. This may seem odd, but in future we will access our virtual machines through SSH so none of the machines will be configured to use a display other than its console. This is the situation you are most likely to encounter professionally.
Every machine has a console. The console is a screen and keyboard directly attached to the machine. In a data centre these screens and keyboards are typically physical devices installed into the racks holding the servers and they are used by data centre personnel to monitor and control servers in the rack. Data centres are also commonly configured to allow remote access to consoles in addition to the physical console devices, saving personnel from needing to enter the physical data centre to access the console. In virtual environments (such as ours) the console is accessed virtually.
- Using the VirtualBox library GUI.
- Using the Remote Desktop Protocol (RDP)5 which VirtualBox makes available on port 3389.
Once on the virtual machine’s console you will see the Debian installer and you can walk through the installation steps as you would for a physical machine.
At the end of the installation you will be prompted to remove the installation CD and reboot. The Debian installation ISO mounted in the virtual DVD drive will be automatically ‘ejected’ as the virtual machine reboots, so just select to reboot the virtual machine and you will see for virtual machine restart into a Debian console prompting you for the username and password to login.
5.3 And now the easy way
We just set up a Debian server manually using the VirtualBox CLI. This is useful to learn the nuts and bolts behind setting up a virtual machine and using a CLI means we could put these commands into a script file and run them whenever we wanted to create a new machine.
We have not discussed how to automate the installation of Debian itself, this is a topic dealt with in Packer from Scratch[Boo20c], but we don’t need to consider this now as we’re going to turn our attention to the use of Vagrant.
5.3.1 Introduction to Vagrant
Vagrant is a command line tool for managing virtual machines6.
Hang on? Isn’t that what VirtualBox does?
No. VirtualBox provides the facility to run virtual machines, Vagrant manages virtual machines.
Vagrant is capable of managing virtual machines running on several different virtualisation tools; VirtualBox, VMWare, and Hyper-V being the main ones used to run virtual machines on a local host. In Vagrant terminology these virtualisation tools are ‘providers’. Vagrant has a plugin architecture, so developers are free to create their own provider if the existing ones are unsuitable.
Another key concept to Vagrant is the ‘box’. A box is a packaged up base virtual machine that is used by Vagrant to provide one or more virtual machines to your project.
Let’s start defining a Vagrant system. This system will consist of a single virtual machine. We base our virtual machine on a box supplied by the bento project7. Since we started out with a Debian server above let’s continue and set up a Debian server using Vagrant.
To create a Vagrant managed virtual machine we need to write a Vagrantfile. A Vagrantfile is a configuration file used by Vagrant, written in Ruby (a fact that we will exploit later when defining more complex Vagrant setups).
Vagrant can create a Vagrantfile for us as a starting point. This is helpful when starting out but I’m sure you will soon find this template unnecessary, even irritating because of all the comment lines it contains. That said, let’s create our first Vagrantfile using Vagrant.
1mkdir myvagrant 2cd myvagrant 3vagrant init bento/debian-10
Simple as that, the init command instructs Vagrant to initialise a Vagrantfile such that it specifies a virtual machine based on the bento/debian-10 box.
Where does Vagrant find these boxes? By default they are hosted on the HashiCorp Vagrant Cloud server.
Before taking a look at the Vagrantfile let’s start our virtual machine.
1vagrant up
You should now see Vagrant downloading the bento/debian-10 base box, make a clone copy for our project, and start up the new virtual machine. At the end of all this we have a running machine but we are returned to our command line prompt.
As with our manually constructed virtual machine the Vagrant machine is started headless8. We access our new virtual machine using SSH, fortunately Vagrant makes this trivial by supplying the vagrant ssh command.
1vagrant ssh
You should be immediately connected to the Debian virtual machine we just created. Your command line prompt while on this virtual server will be vagrant@debian-10: $
To leave the server (but have it remain running) simply logout.
1exit
You will be returned to your host computer’s command prompt.
1Application interface using the computers command shell.
2User interface using a visual desktop metaphor.
3If you are interested in using the VirtualBox GUI there are plenty of examples online.
4Assuming you have a recently up-to-date Windows 10 installation.
5A network display protocol developed by Microsoft.
6We are using Vagrant as a tool in this book but I have written another book dealing with Vargant in more detail, Vagrant from Scratch[Boo20e]
7The bento project is run by the people who develop the Chef configuration management system. I have found these boxes to be quite sound for my needs.
8Virtual machines can be started with a display, but we almost exclusively want headless servers, so Vagrant’s default behaviour is ideal.