How to setup a local Linux cluster with VirtualBox on windows

Okezie Okpara
7 min readMar 24, 2019

--

Introduction

Virtualbox logo

In this tutorial, we will learn how to build a cluster of Linux machines on our local environment using Virtualbox running on a windows machine. We will them assign private static IP address and connect to them from our host windows machine via SSH.

This is necessary If you want to try out a clustered application which requires multiple Linux machines like kubernetes on your local/dev environment.

Ordinarily, you could easily spin new virtual machines in the cloud with digitalocean or simply use a managed kubernetes solution like digitalocean.

But if you are like me who like to have a local version handy as a fallback.

Goals

In this tutorial, we are going to create a cluster of four Linux virtual machines.

  1. Each machine is capable of connecting to the internet and able to connect with each other privately as well as can be reached from the host machine.
  2. Our machines will be named kube-master, kube-node-one, kube-node-two, kube-node-three.
  3. The first machine: kube-master will act as a master node and will have 2vCPUs, 2GB of RAM and 10 GB hard disk.
  4. The other machines: kube-node-one, kube-node-two, kube-node-three will act as worker nodes will have 1vCPUs, 1GB of RAM and 10 GB hard disk.
  5. We will assign our machines static IP address: 192.168.0.50, 192.168.0.51, 192.168.0.52. 192.168.0.53.

Prerequisites

  • A windows machine with at least 8GB of RAM.
  • Virtualbox for windows installed.
  • Familiarity with setting up virtual machines with Virtualbox.
  • An SSH client for windows eg putty, git bash or windows subsystem for Linux (WSL). But in this tutorial, we will be using WSL with a Cmder as a command line tool.
  • An Ubuntu server 64-bit image (16.04+). In this tutorial, we use 16.04.

Step 1 — Create virtual machines on Virtualbox

Create the master virtual machine which we will name kube-master with 2vCPUs, 2GB of RAM and 10 GB hard disk. Make sure to set up the network as follows:

  1. On “Adapter 1” Change the “Attached To” field to “Bridged Adapter” and for “Name” select the primary network adapter of the host computer (in this case Intel dual band wireless).
  2. Select “Adapter 2” Tab and make sure “Enable Network Adapter” is enabled. Use same settings from 1 above.
Adapter settings on virtualbox

Adapter 1 will be the primary network adapter with a dynamically assigned IP address while adapter 2 will be the secondary adapter with a statically assigned IP address which we will assign to it.

Then go ahead and create the other virtual machines kube-node-one, kube-node-two, kube-node-three using the above steps. Giving them 1vCPUs, 1GB of RAM and 10 GB hard disk.

Step 2 — Boot the virtual machines

When you start the virtual machines for the first time you are prompted to instal and setup Ubuntu. Follow through with the installation until you get to the “configure network prompt”.

When you get to the configure network prompt, select the first network adapter as the primary adapter.

Select primary adapter screen

Also, enable open ssh server in the software selection prompt

Enable open SSH server prompt

Start the newly created machine and make sure it is running. Repeat this for all your virtual machines.

Step 2 — Configure the network adapters

When your VM is all set up, log in to the VM and test the network by pinging any public site. e.g `ping google.com`. If all works well, this should be successful.

You can check the DHCP-assigned IP address by entering the following command:

hostname -I

You will get an output similar to this:

198.51.100.7

This is the default IP address assigned by your network DHCP. Note that this IP address is dynamic and can change or worst still, get assigned to another machine. But for now, you can connect to this IP from your host machine via SSH.

Next, we will configure the secondary network adapter with a static IP. To do this we will:

1. Edit the `/etc/network/interfaces` file. Using your favourite text editor, open this file as root user (we use nano here).

sudo nano /etc/network/interfaces

The initial content of the file will look similar to 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 interfaceauto loiface lo inet loopback# The primary network interfaceauto enp0s3iface enp0s3 inet dhcp

From the original file contents, you will notice that there are two sections starting with # comments. The first is The loopback network interface and the second is The primary network interface. Now we will add another section that configures the secondary network adapter assigning a static IP address for us.

Append the following snippet to the end of the document.

# The secondary network interfaceauto enp0s8iface enp0s8 inet staticaddress 192.168.0.50netmask 255.255.255.0network 192.168.0.0broadcast 192.168.0.255

The important part of this snippet is that it targets our secondary network adapter named enp0s8, it is static and assigns the IP address 192.168.0.50 to our host. Save the file and exit.

2. Run `sudo service networking restart` for the changes to take effect.

3. Verify that both network interfaces are now configured and IP addresses assigned to them by running: `hostname -I` again. Now you will get an output similar to below:

198.51.100.7 192.168.0.50

4. Do same for the other virtual machines assigning them 192.168.0.51, 192.168.0.52, 192.168.0.53 respectively.

5. Now try to SSH from one node to the other using their newly assigned static IP address. If all goes well this should be successful.

Step 3 — Setup host machine network

But at this point, you won’t be able to SSH or ping any of the virtual machines from the host using their respective static IP addresses. To do this:

  1. Go to your windows command prompt and run the command:
ipconfig /all

You will get an output similar to:

Connection-specific DNS Suffix  . :Description . . . . . . . . . . . : Intel(R) Dual Band Wireless-AC 8265Physical Address. . . . . . . . . :DHCP Enabled. . . . . . . . . . . : YesAutoconfiguration Enabled . . . . : YesIPv4 Address. . . . . . . . . . . : 198.51.100.128(Preferred)Subnet Mask . . . . . . . . . . . : 255.255.255.0Lease Obtained. . . . . . . . . . : 07 March 2019 8:48:22 PMLease Expires . . . . . . . . . . : 07 March 2019 9:48:22 PMDefault Gateway . . . . . . . . . : 192.168.43.1DHCP Server . . . . . . . . . . . : 192.168.43.1DHCPv6 IAID . . . . . . . . . . . : 81012107DNS Servers . . . . . . . . . . . : 192.168.43.1NetBIOS over Tcpip. . . . . . . . : Enabled

From the output, you will notice the currently active network adapter (in this case its the “Wireless LAN adapter Wi-Fi”) has some basic details take note of these details especially the IPv4 Address value.

2. Now go to **Network and sharing centre** from your control panel. Make sure you are currently connected to the internet.

Network and sharing (Windows)

3. Click on the value of Connections which is basically your wifi. This reveals the WiFI status modal popup.

Network properties (Windows)

4. Click Properties to reveal you the WiFi Properties modal.

IP and properties

5. Scroll and select Internet Protocol Version 4 (TCP/IPv4) from the list and click Properties.

6. Now edit your network IPv4 settings as follows:

Change IP V4 properties

7. By default Obtain an address automatically is checked. Change this to Use the following IP address which will enable the other fields for editing.

8. For IP Address, use the IP address you got from Step 1 above. Also for Subnet Mask, Default Gateway etc.

9. Click Ok to close. Note you may be temporarily disconnected from the internet for a few seconds.

10. Now go back to that modal we just closed, making sure all our settings are still intact.

11. Now click on the Advanced.. button to reveal the Advanced TCP/IP Settings.

Advanced TCP/IP

12. In the IP addresses section click Add and enter the following:

13. For the IP Address, field enter 192.168.0.49 and for the Subnet Mask field enter 255.255.255.0. This effectively joins our host machine to our cluster’s private network.

14. Click Ok, save all your changes and close out of the modal.

Conclusion

Now we have 4 Linux virtual machines in our cluster each can connect to the other using a static IP, can access the internet and can be accessed from the host machine. With this setup, you can run any clustered application like kubernetes locally.

We can take things further by assigning DNS names to the machines and creating our own local DNS server with dnsmasq but this is beyond the scope of this tutorial.

--

--

Okezie Okpara

Software Engineer, DevOps Engineer, Software Architect. AWS, Kuberntes, Linux C#, Python, Javascript