Skip to content
Blogster on GitHub Paritosh on Twitter

Playing with MicroK8s

Introudction

In the ever-evolving landscape of container orchestration, Kubernetes stands tall as the go-to platform for managing containerized applications. However, setting up a full-scale Kubernetes cluster can be a daunting task, especially for those looking to experiment and learn without committing to a complex deployment. This is where MicroK8s comes into play, offering a lightweight, easy-to-use alternative for local development and testing.

MicroK8s is a low-ops, minimal production Kubernetes.

Why not minikube

Minikube is a great tool for learning k8s however it has lots of default settings that might not be available at the production level. If you try to apply the same practices in the production level k8 cluster, you might face some problems.

I wanted to experiment with something lightweight still very close to production level k8s. MicroK8s can provide these features. It has inbuilt features as follows:

  • Built-in DNS
  • Addon Management
  • Kubernetes Dashboard
  • Single-node Kubernetes Cluster can be extended to a multi-node cluster
  • Local image repository
  • Certificates setup

And many more .....

Installation

microk8s installation is very simple, run the following commands in order:

First, install microk8s

sudo snap install microk8s --classic

Once installed, run the following command:

microk8s start

Please wait until the node is ready. You can check the status of nodes by running the following commands:

microk8s status --wait-ready

You can query the status of

microk8s kubectl get nodes

Addons

microk8s enable dashboard
microk8s enable storage
microk8s enable istio

For further troubleshooting:

Microk8s has great documentation for various platforms, please refer to it here for more options and troubleshooting.

https://microk8s.io/docs/how-to

Dashboard

The K8 dashboard provides a simple way to see and troubleshoot problems in the k8 cluster. Run the following command in command prompts to get access of the dashboard:

microk8s dashboard-proxy

This will emit the dashboard url and a token to access the dashboard. Navigate to the url and enter token generated by above command. Now you can visit the dashboard successfully.

Configuring kubectl

Although microk8s provides a way to access the k8-related commands, e.g., if you want to access the nodes of k8, you can run the following commands:

microk8s kubectl get nodes

or to get pods, you can run the following commands:

microk8s kubectl get pods

However, as you can see, typing "microk8s" all over the place is annoying and less productive. You can make an alias to get around this issue, but I'd prefer to utilise the current Kubectl utility to get the k8 releated information.

Check the Kubectl installation instructions here: https://kubernetes.io/docs/tasks/tools/#kubectl.

And copy the microk8s configuration to the Kubectl location using the following command:

cd $HOME
mkdir .kube
cd .kube
microk8s config > config

You can also check the microk8s config before copying by using the following command:

microk8s config

Get to the business

The reason I wanted to spin my cluster was to run the curl command to create a pod. Let us see how we can achieve it.

First, how to get the K8 API URL and store it in the KUBE_API variable:

KUBE_API=$(kubectl config view -o jsonpath='{.clusters[0].cluster.server}')

You cannot access the API without a token, so how will we create one? Here is the command to help create a token:

JWT_TOKEN_DEFAULT_DEFAULT=$(kubectl create token default)

Now I have a token. Where can I send the request?

curl --insecure $KUBE_API/version --header "Authorization: Bearer $JWT_TOKEN_DEFAULT_DEFAULT"

Congrates!! Now you have a working curl to send a request.

Note: I am not validating the certs from the API server, which is not good in the real life. Do not do this. Always validate server certificates. But for experimental purposes, this is fine.

Let us create a pod now. First, define the pod specification and save it to pod-spec.yaml.

cat > pod-spec.yaml <<EOF
apiVersion: v1
kind: Pod
metadata:
name: hello-world
spec:
containers:

- name: hello-world
  image: hello-world
  restartPolicy: Never

Now run the following command:

curl --insecure -k -X POST -H 'Content-Type: application/yaml' -H "Authorization: Bearer $JWT_TOKEN_DEFAULT_DEFAULT"\
 --data "$(cat pod-spec.yaml)" $KUBE_API/api/v1/namespaces/default/pods

Oh, this command does not work. The reason is that we need admin access to create pods. So let us create one:

JWT_TOKEN_KUBESYSTEM_DEFAULT=$(kubectl -n kube-system create token default)

Now run the following commands using the above token:

curl --insecure -k -X POST -H 'Content-Type: application/yaml' -H "Authorization: Bearer $JWT_TOKEN_KUBESYSTEM_DEFAULT"\
 --data "$(cat pod-spec.yaml)" $KUBE_API/api/v1/namespaces/default/pods

Wow, it succeeded. YAY

Congrates, now you are ready to play around with k8 cluster.