The Day Your Laptop Became A Micro-Datacenter

Dan Aschwanden
HackerNoon.com
11 min readJul 11, 2017

--

Did you know that you can push your laptop from being an ordinary computer to a Micro-Datacenter powering a Kubernetes cluster and then back to an ordinary computer in only 15 minutes?

Yes, you read right.

15 minutes to get your own private Kubernetes cluster up and running on your laptop.

Microservices and Kubernetes are the hype.

But how do you get started with learning more about these two exciting topics?

I will show you every step of the way and get you up to speed with a learning environment on your laptop. By the end of this post you will be all set to experiment and explore Kubernetes on your laptop.

The diagram below gives you an overview of what your Micro-Datacenter will look like when we are done setting it up.

We will leverage a virtualisation layer (consisting of Docker containers and virtual machines) running on your laptop hardware.

On this virtualisation layer we will run Rancher which will help us with setting up and managing our Micro-Datacenter as well as the Kubernetes cluster.

Here are the 5 short steps to build your Micro-Datacenter

  1. Install the dependencies (Docker & Virtualbox)
  2. Install Rancher Server
  3. Create a Kubernetes environment in Rancher
  4. Add two nodes (virtual machines) to the Kubernetes cluster
  5. Run a ‘Hello World’ nginx service in Kubernetes

First things first.

1. Install the Dependencies

There are only two dependencies to get us on our way:

  1. We need Docker installed on your laptop (click here and follow the instructions to install Docker)
  2. We need Virtualbox installed on your laptop (click here and follow the instructions to install Virtualbox)

Take a minute and install them both if you have not done so already.

When you are done with installing go ahead and start Docker.

Docker is running

The screen shots and terminal commands in this post are taken from a Mac. However, with very few exceptions (which I will point out) you should be fine following along on either Linux or Windows.

Let’s get started (check your watch :)

2. Install Rancher Server

Rancher Server will manage our Micro-Datacenter and our Kubernetes cluster. Let’s fire up Rancher in a Docker container.

Open a terminal on your laptop and copy-paste the following command (one line):

Once Rancher is up and running you can point your browser to localhost port 8080 and access the Rancher Server’s web interface.

Later on we want our two Kubernetes nodes (virtual machines) to connect to the Rancher Server. The two nodes will not be able to use the localhost address to connect. Therefore we need to add an alias to the localhost interface on our laptop. Let’s assign a private IP address as an alias.

I have chosen 10.145.10.1 as a random private IP address. Note it does not matter what private IP address you choose. You will be fine as long as you stay clear of the more common ones (like 192.168…. or 10.0….). Staying clear of these ranges of private IPs will help avoiding networking conflicts.

Note for Windows users: Assigning the alias might be somewhat different on Windows. Please use caution and read up on the details for your OS. For Linux users the below commands should look very similar.

Verify that you can reach the Rancher Server web interface by pointing your browser to the alias on 10.145.10.1 port 8080

3. Create the Kubernetes environment in Rancher

At the top left corner on the Rancher web interface click on Environment then select Manage Environments > Add Environment

Give the new environment a name (ie ‘Kubernetes Demo’). Choose the Kubernetes Environment Template, then scroll down and click ‘Create’.

Create a Kubernetes Environment

You will now see new ‘Kubernetes Demo’ environment in the Environment drop down in the top left corner. Click on it.

At this stage Rancher is waiting for us to add infrastructure nodes to the newly created Kubernetes environment.

Rancher waits for infrastructure nodes to be added

4. Add two nodes (virtual machines) to the Kubernetes cluster

We create the two virtual machines using the docker-machine command from Docker (note you could add as many nodes as your hardware can support, but for the demo two are enough).

For our two Kubernetes nodes we could use any Linux distro machine image that comes with Docker installed. However, Rancher distributes a tiny container optimised image called Rancher OS. We’ll use Rancher OS. It is perfect for our use case.

Go back to your terminal and copy-paste the command from the box below (one line).

Note: run the command twice once with kube-box-1 and then with kube-box-2 to create our two nodes.

Rancher OS comes with Docker pre-installed. Unfortunately, the default version of Docker is newer than Kubernetes likes its Docker to be.

We have to switch Docker on the two nodes to a suitable version (1.13.1).

Login to each of the two nodes and change the Docker version.

Fantastic. Our two nodes are now ready to join the Kubernetes cluster.

In the next step we will run a little script on both of the two nodes to register them with Rancher and join the Kubernetes environment.

For the registration script we will need the nodes’ IP addresses (note your nodes’ IP addresses might be different).

We have got all the info we need. It’s time to go back to the Rancher web interface and click on ‘Add a host’

Add a host

Keep the default ‘Custom’ setting for the new node.

Then scroll down to step 4. There specify the IP address for kube-box-1 as retrieved above (192.168.99.100 or whatever the value is that docker-machine has allocated in your case).

Rancher needs the IP address to communicate with its infrastructure nodes.

Enter the node’s IP address and copy the registration script

Copy the command from step 5. This is the registration script to run the Rancher agent on kube-box-1.

Go back to the terminal on your laptop and login to kube-box-1 using the docker-machine ssh command we already used above.

Do all of the above (starting from ‘Add a host’) again to register kube-box-2.

Important! Remember to copy-paste the IP address for kube-box-2 (ie 192.168.99.101) in step 5. Bad things will happen if you register kube-box-2 with kube-box-1’s IP address.

Rancher is now busy provisioning the two nodes with the Kubernetes services.

Switch to Infrastructure > Hosts to watch the Kubernetes services coming up on the two nodes.

Rancher provisions Kubernetes services on the two nodes

It will take some time for Rancher to download and install the images for the Kubernetes services on the two nodes.

Be patient. Depending on your internet connection this might take quite some time. There are several ways to tell when your Kubernetes cluster is configured and up and running.

One way is when Rancher will start providing a link to open the Kubernetes dashboard (Kubernetes > Dashboard).

You have some time while Rancher is hard at work spinning up your Kubernetes cluster. Have a play with Rancher’s web interface to see what else it offers that we did not discuss yet.

Kubernetes is ready. You might want to checkout the Kubernetes dashboard.

5. Run a ‘Hello World’ nginx service in Kubernetes

Now, to the exiting part! We have Kubernetes running in our Micro-Datacenter.

In good old fashioned tradition let’s run the ‘Hello World’ of Microservices: A simple nginx web server (serving a default web page).

Kubernetes provides the kubectl command line tool to configure its resources.

With Rancher Server we can either choose to

  • run kubectl from the console in the web interface or
  • if you have kubectl installed on your laptop then Rancher gives us the kubectl configuration file to copy over to the laptop.

Both options are available from the Kubernetes > CLI menu in the Rancher web interface.

For simplicity we will use Rancher’s web console for running kubectl.

Of course you are free to install kubectl on your laptop and configure it with the config provided by Rancher (click on the ‘Generate Config’ button as shown on the screen shot below). Having kubectl installed on your laptop will come in handy when you start doing more interesting stuff with your Kubernetes cluster later.

kubectl shell in the Rancher web interface (or check out the config file if you run kubectl on your laptop)

For your convenience I created a github repo with the Kubernets configuration files (*.yaml) to run the nginx ‘Hello World’ example.

All you need to run the nginx example (a Kubernetes deployment and Kubernetes service) is executing the two ‘kubectl create -f …’ commands in Rancher’s web console.

Clone the github repo if you either intend to run kubectl from your laptop or you want to study the content of the two yaml files.

Note the output of the ‘kubectl get services’ command above. It tells us two things:

  • The nginx service (svc-nginx) is exposed on the external IP <nodes>
  • on port 32767 (might be different in your case).

This means nginx is now available on both our nodes on port 32767.

Let’s verify that we can reach nginx by pointing the browser to either of the nodes’ IP addresses at the port the Kubernetes service has assigned.

For example http://192.168.99.100:32767 to access nginx via kube-box-1. Note that your IP address and port might be different. Change them accordingly.

Et voila

Congratulations. Your laptop is running a Micro-Datacenter with Kubernetes and nginx as a simple show case service.

Over to you now to play with the setup at your heart’s content. We barely touched the surface of what can be done. Go wild.

I hope you will enjoy your new datacenter powers.

Once you are done you probably want to clean up and free up the resources. Cleaning up is just as easy as it was to set up the environment. Simply follow the 5 steps below.

Cleaning up

As promised we want to leave your laptop as pristine as we found it at the beginning of this post.

Let’s get the broom out.

a) Stop the Kubernetes service and deployment.

b) Stop the two nodes and remove them

c) Remove the alias on your laptop’s localhost interface. If you are on Windows or Linux this step might be slightly different. Please use caution and read up on it for your OS.

d) Stop the Rancher Server container and remove it

Done. I hope you enjoyed the journey (have you checked the time?)

PS: As always: “The opinions stated here are my own, not those of my company”.

Useful Links

--

--