MicroK8s on Ubuntu: install and running apps

MicroK8s is a “Low-ops, minimal production Kubernetes, for devs, cloud, clusters, workstations, Edge and IoT”.

I recently installed it on my Ubuntu machine so I could mess around with some K8s SIGs projects. I wanted to document the steps and how to get a simple app up and running.

Install K8s

The documentation for this is really good. In summary, run the following for a basic setup:

1
2
3
4
sudo snap install microk8s --classic

# Enable DNS (coredns) and ingress (nginx)
microk8s enable dns ingress

You can then run commands using microk8s kubectl. For example:

1
2
# Get pods in all namespaces
microk8s kubectl get pods -A

If you get Insufficient permissions to access MicroK8s, you probably need to elevate your permissions using sudo.

Setup kubectl

You can use kubectl directly by copying the microk8s config file into your .kube directory.

1
2
cd ~/.kube
sudo microk8s config > config

Run a simple app (httpbin)

We can use the excellent httpbin as a simple app to demonstrate things work.

First, we create a deployment with a container from the image docker.io/kennethreitz/httpbin:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpbin
spec:
replicas: 1
selector:
matchLabels:
app: httpbin
version: v1
template:
metadata:
labels:
app: httpbin
version: v1
spec:
containers:
- image: docker.io/kennethreitz/httpbin
imagePullPolicy: IfNotPresent
name: httpbin
ports:
- containerPort: 80

Use kubectl apply -f <filename> to apply these manifest files to your cluster.

Run kubectl get pods -o wide and you will see the pod spinning up or running:

1
2
NAME                       READY   STATUS    RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
httpbin-84cddc85d4-6p6nn 1/1 Running 1 19h 10.1.71.70 hopper <none> <none>

As I am running MicroK8s on my local machine, I can navigate straight to the pod in the browser (as my machine and the single node in the cluster are the same thing). The container is running on port 80, so it’s as simple as entering the IP address into the address bar:

Screenshot of browser viewing http://10.1.71.70

Of course this IP address is likely to change if the pod is recreated, so we might create a service to make things a little more stable:

1
2
3
4
5
6
7
8
9
10
11
12
apiVersion: v1
kind: Service
metadata:
name: httpbin
spec:
selector:
app: httpbin
ports:
- protocol: TCP
port: 30459
targetPort: 80
name: http

Now run kubectl get svc and you’ll see we now have a ClusterIP for the service:

1
2
3
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)     AGE
kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 19h
httpbin ClusterIP 10.152.183.220 <none> 30459/TCP 77m

This also works in the browser - I can navigate to 10.152.183.220:30459.

We could also define the service type as NodePort, like so:

1
2
spec:
type: NodePort

This would allow us to access the pod at localhost with the relevant port, e.g. http://localhost:34802.

Configuring ingress

If you would like to configure an ingress rule for a custom domain, you can simply create an ingress resource. It’s important to set the kubernetes.io/ingress.class to public (this is the class ingress is configured to use by default on MicroK8s):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: httpbin
annotations:
kubernetes.io/ingress.class: public
ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: httpbin.k8s.io
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: httpbin
port:
number: 30459

You will see your new ingress resource, along with an address of 127.0.0.1 (potentially after a minute or so).

Although there is no external load balancer, this address appears because nginx is configured with the --publish-status-address=127.0.0.1. You can see this for yourself by looking at the DaemonSet definition with the following command:

1
kubectl describe ds nginx-ingress-microk8s-controller -n ingress

You will also need to configure your local hosts file to add the following line. You can use sudo nano /etc/hosts for this.

1
127.0.0.1    httpbin.k8s.io

The httpbin service can then be accessed via httpbin.k8s.io:

Screenshot of browser viewing http://httpbin.k8s.io