Get Rewarded! We will reward you with up to €50 credit on your account for every tutorial that you write and we publish!

Create a Microk8s Cluster

profile picture
Author
Rob Ericsson
Published
2020-03-16
Time to read
6 minutes reading time

Introduction

MicroK8s is a lightweight Kubernetes that tracks upstream releases and is simple to install and configure for development, prototyping and testing. You can have a simple application running on a Kubernetes cluster on Hetzner Cloud in less than 30 minutes.

Prerequisites

To follow the steps in this tutorial, you will need the following:

  • A Hetzner Cloud account
  • A Hetzner Cloud project for your Kubernetes cluster
  • A configured SSH key for your Hetzner Cloud project
  • An API token for your Hetzner Cloud project
  • A local hcloud CLI install
  • Basic concepts of Kubernetes

The tutorial uses the following conventions to represent variables used in the code below:

  • <network_id> is the name of the network used for the cluster. e.g. network-1
  • <ssh_key_id> is the name of an SSH key in your Hetzner cloud project
  • <master-1_ip> is the IP address of the master-1 server created below
  • <node-1_ip> is the IP address of the node-1 server created below
  • <node-2_ip> is the IP address of the node-2 server created below

This tutorial was created on Ubuntu 18.04 and the instructions were tested there. It should work from any operating system with proper adaption of the instructions.

Step 1 - Create the network and subnet

For this step, you will use the hcloud CLI tool on your local workstation to create the network and subnet in your Hetzner Cloud project. To use the hcloud CLI tool, we need to first install it and initialize a context. For information on how to do that, consult the How-to: Hetzner Cloud CLI tutorial.

In the example below, I'm defining the network to have the IP range 10.44.0.0/16. The subnet is in the network zone eu-central with a definition of 10.44.0.0/24.

local$ hcloud network create --name <network_id> --ip-range 10.44.0.0/16
local$ hcloud network add-subnet <network_id> --network-zone eu-central --type server --ip-range 10.44.0.0/24

Step 2 - Create servers

Next, we create the servers using hcloud CLI. This should only take a few minutes.

local$ hcloud server create --type cx11 --name master-1 --image ubuntu-18.04 --ssh-key <ssh_key_id> --network <network_id>
local$ hcloud server create --type cx11 --name node-1 --image ubuntu-18.04 --ssh-key <ssh_key_id> --network <network_id>
local$ hcloud server create --type cx11 --name node-2 --image ubuntu-18.04 --ssh-key <ssh_key_id> --network <network_id>

Be sure to note the IP address for each server that is created as you will need them to login via ssh in later steps.

Step 3 - Install MicroK8s

After we have the servers, we will log into each one, apply updates and install microK8s. From your workstation, ssh to the IP address of the server as root (e.g. local$ ssh root@<master-1_ip> -i ~/.ssh/id_rsa) and run the following:

root@master-1:~# apt update && apt -y upgrade
root@master-1:~# snap install microk8s --classic
root@master-1:~# microk8s.enable dns storage ingress

This will take a few minutes to complete. Do the same steps on node-1 and node-2 so that MicroK8s is installed on all of the servers.

Step 4 - Create cluster

Now that we have installed MicroK8s, we create a cluster for our three machines using the microk8s.add-node and microk8s.join commands. SSH to the master-1 server and run the following:

root@master-1:~# microk8s.add-node
Join node with: microk8s.join 95.217.153.160:25000/ruNhDtPezpdTCfyAyhOCJWpPnGhSggvZ

If the node you are adding is not reachable through the default interface you can use one of the following:
 microk8s.join 95.217.153.160:25000/ruNhDtPezpdTCfyAyhOCJWpPnGhSggvZ
 microk8s.join 10.44.0.2:25000/ruNhDtPezpdTCfyAyhOCJWpPnGhSggvZ
 microk8s.join 10.1.77.0:25000/ruNhDtPezpdTCfyAyhOCJWpPnGhSggvZ

Copy the microk8s.join command that uses the network you defined above (e.g. microk8s.join 10.44.0.2:25000/ruNhDtPezpdTCfyAyhOCJWpPnGhSggvZ). Next, ssh to the node-1 server and run the microk8s.join command:

root@node-1:~# microk8s.join 10.44.0.2:25000/ruNhDtPezpdTCfyAyhOCJWpPnGhSggvZ

This command will take a few seconds and return.

Go back to the master-1 server and run microk8s.add-node again to generate a new join key. Copy the microk8s.join command as you did before and run it on node-2.

root@node-2:~# microk8s.join 10.44.0.2:25000/cwWfPWPUmrmFGKUYaVovhhWVUrLpsEyP

After the microk8s.join command returns, log into master-1 again and check that the nodes are joined using the following command:

root@master-1:~# microk8s.kubectl get nodes

You should get a response that lists the three nodes that are in the network:

NAME        STATUS   ROLES    AGE    VERSION
10.44.0.3   Ready    <none>   5m     v1.17.2
10.44.0.4   Ready    <none>   119s   v1.17.2
master-1    Ready    <none>   44m    v1.17.2

Step 5 - Run an application

Now that we have a MicroK8s cluster, let's run a simple application in the cluster. To do that, go to master-1 and run the following command to install a hello-app:

root@master-1:~# microk8s.kubectl run web --image=gcr.io/google-samples/hello-app:1.0 --port=8080

You will receive a message that kubectl run is deprecated which is OK.

Next, we need to expose that application we installed with a service:

root@master-1:~# microk8s.kubectl expose deployment web --target-port=8080 --type=NodePort
service/web exposed

Now, using whichever text editor you prefer (e.g. vim, nano, etc.) create a text file called simple-ingress.yaml on master-1 with the following contents:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: sample-ingress
spec:
  backend:
    serviceName: web
    servicePort: 8080

This will create an ingress controller for the hello-web application which allows it to be accessed from outside the Kubernetes cluster. We create this ingress with the following command:

root@master-1:~# microk8s.kubectl apply -f simple-ingress.yaml

To test that this works, go to your local workstation and run curl:\\<master-1_ip>. You will get some result like:

Hello, world!
Version: 1.0.0
Hostname: web-9bbd7b488-68fbd

We now have a working application running on our Kubernetes cluster.

Conclusion

If you have worked all the way through this tutorial, you now have a working MicroK8s cluster running on Hetzner Cloud that you can use for development or prototyping. There are many more things we can with this simple cluster. A nice feature of running MicroK8s on Hetzner Cloud is that you can upgrade your server to a larger instance to get more capacity when you need it.

License: MIT
Want to contribute?

Get Rewarded: Get up to €50 in credit! Be a part of the community and contribute. Do it for the money. Do it for the bragging rights. And do it to teach others!

Report Issue
Try Hetzner Cloud

Get 20€ free credit!

Valid until: 31 December 2024 Valid for: 3 months and only for new customers
Get started
Want to contribute?

Get Rewarded: Get up to €50 credit on your account for every tutorial you write and we publish!

Find out more