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 | sudo snap install microk8s --classic |
You can then run commands using microk8s kubectl
. For example:
1 | # Get pods in all namespaces |
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 | cd ~/.kube |
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 | apiVersion: apps/v1 |
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 | NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES |
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:
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 | apiVersion: v1 |
Now run kubectl get svc
and you’ll see we now have a ClusterIP for the service:
1 | NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
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 | spec: |
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 | apiVersion: networking.k8s.io/v1 |
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
: