Your instant Kubernetes cluster

This is a condensed and updated version of my previous tutorial Kubernetes in 10 minutes. I've removed just about everything I can so this guide still makes sense. Use it when you want to create a cluster on the cloud or on-premises as fast as possible.

1.0 Pick a host

We will be using Ubuntu 16.04 for this guide so that you can copy/paste all the instructions. Here are several environments where I've tested this guide. Just pick where you want to run your hosts.

Civo is a relatively new developer cloud and one thing that I really liked was how quickly they can bring up hosts - in about 25 seconds. I'm based in the UK so I also get very low latency.

1.1 Provision the machines

You can get away with a single host for testing but I'd recommend at least three so we have a single master and two worker nodes.

Here are some other guidelines:

  • Pick dual-core hosts with ideally at least 2GB RAM
  • If you can pick a custom username when provisioning the host then do that rather than root. For example Civo offers an option of ubuntu, civo or root.

Now run through the following steps on each machine. It should take you less than 5-10 minutes. If that's too slow for you then you can use my utility script kept in a Gist:

$ curl -sL | sh

1.2 Login and install Docker

Install Docker from the Ubuntu apt repository. This will be an older version of Docker but as Kubernetes is tested with old versions of Docker it will work in our favour.

$ sudo apt-get update \
  && sudo apt-get install -qy

1.3 Disable the swap file

This is now a mandatory step for Kubernetes. The easiest way to do this is to edit /etc/fstab and to comment out the line referring to swap.

To save a reboot then type in sudo swapoff -a.

Disabling swap memory may appear like a strange requirement at first. If you are curious about this step then read more here.

1.4 Install Kubernetes packages

$ sudo apt-get update \
  && sudo apt-get install -y apt-transport-https \
  && curl -s | sudo apt-key add -

$ echo "deb kubernetes-xenial main" \
  | sudo tee -a /etc/apt/sources.list.d/kubernetes.list \
  && sudo apt-get update 

$ sudo apt-get update \
  && sudo apt-get install -y \
  kubelet \
  kubeadm \

1.5 Create the cluster

At this point we create the cluster by initiating the master with kubeadm. Only do this on the master node.

Despite any warnings I have been assured by Weaveworks and Lucas (the maintainer) that kubeadm is suitable for production use.

$ sudo kubeadm init

If you missed a step or there's a problem then kubeadm will let you know at this point.

Take a copy of the Kube config:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Make sure you note down the join token command i.e.

$ sudo kubeadm join --token c30633.d178035db2b4bb9a --discovery-token-ca-cert-hash sha256:<hash>

2.0 Install networking

Many networking providers are available for Kubernetes, but none are included by default, so let's use Weave Net from Weaveworks which is one of the most popular options in the Kubernetes community. It tends to work out of the box without additional configuration.

$ kubectl apply -f "$(kubectl version | base64 | tr -d '\n')"

If you have private networking enabled on your host then you may need to alter the private subnet that Weavenet uses for allocating IP addresses to Pods (containers). Here's an example of how to do that:

$ curl -SL "$(kubectl version | base64 | tr -d '\n')&env.IPALLOC_RANGE=" \
| kubectl apply -f -

Weave also have a very cool visualisation tool called Weave Cloud. It's free and will show you the path traffic is taking between your Pods. See here for an example with the OpenFaaS project.

2.2 Join the worker nodes to the cluster

Now you can switch to each of your workers and use the kubeadm join command from 1.5. Once you run that log out of the workers.

3.0 Profit

That's it - we're done. You have a cluster up and running and can deploy your applications. If you need to setup a dashboard UI then consult the Kubernetes documentation.

$ kubectl get nodes
openfaas1   Ready     master    20m       v1.9.2
openfaas2   Ready     <none>    19m       v1.9.2
openfaas3   Ready     <none>    19m       v1.9.2

If you want to see my running through creating a cluster step-by-step and showing you how kubectl works then checkout my video below and make sure you subscribe

You can also get an "instant" Kubernetes cluster on your Mac for development using Minikube or Docker for Mac Edge edition. Read my review and first impressions here.

4.0 Keep learning

You can keep learning and get head with weekly emails from me as part of a GitHub Sponsorship. Sign up below to learn more about Kubernetes, Raspberry Pi and Docker from me.

Alex Ellis

Read more posts by this author.

Subscribe to Alex Ellis' Blog

Subscribe to keep in touch. By providing your email, you agree to receive marketing emails from OpenFaaS Ltd

or subscribe via RSS with your favourite RSS reader

Learn Go with my new eBook

eBook cover

"Everyday Go" is the fast way to learn tools, techniques and patterns from real tools used in production based upon my experience of building and running OpenFaaS at scale.

Buy a copy on Gumroad