MicroK8s is a powerful, lightweight and a reliable production-ready Kubernetes distribution. It is the smallest and fastest multi-node kubernetes and works on Linux, Windows and MacOS. MickroK8s can easily be installed with a single command and takes away the complexity associated with setting up Kubernetes. MicroK8s is great for offline development, prototyping, and testing.

Install MicroK8s on Linux Mint 20

We are going to use snap to install MicroK8s on Linux Mint 20. We should, however, remember that snap is disabled in Linux 20. To enable snap support on Linux Mint, follow the guide here:

How to install and use snap on Linux

Installing MicroK8s

Once snap is enable on Linux mint 20, run the below command to install MicroK8s

$ sudo snap install microk8s --classic

Add user to microk8s group

Your local user should be part of microk8s group for it to run commands without sudo.

sudo usermod -a -G microk8s $USER 
sudo chown -f -R $USER ~/.kube
sudo reboot

Managing Microk8s

The following steps show how to manage Microk8s service.

Start MicroK8s

To start MicroK8s, we run the below command

$ microk8s start
Started.

Check MicroK8s status

To check whether MicroK8s is running, we use the below command.

$ microk8s status
microk8s is running 
high-availability: no 
datastore master nodes: 127.0.0.1:19001
Enable MicroK8s

Enable/ disable MicroK8s

To enable or disable MicroK8s, we use snap as shown:

$ sudo snap enable microk8s
microk8s enabled
$ sudo snap disable microk8s
microk8s disabled

MicroK8s List all nodes

To list nodes in MicroK8s, we use the following command:

$ microk8s kubectl get nodes
NAME        STATUS   ROLES    AGE     VERSION 
linux   Ready    <none>   3h32m   v1.19.0-34+1a52fbf0753680 

MicroK8s check services

Use the below commands to list MicroK8s services

$ microk8s kubectl get services
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.152.183.1   <none>        443/TCP   23h

$ microk8s kubectl get all
NAME        READY   STATUS    RESTARTS   AGE
pod/nginx   1/1     Running   0          70m
NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.152.183.1   <none>        443/TCP   23h

Adding a Node to Microk8s cluster

To create a cluster out of two or more already-running MicroK8s instances, use the microk8s add-node command. The MicroK8s instance on which this command is run will be the master of the cluster and will host the Kubernetes control plane.

$ microk8s add-node
From the node you wish to join to this cluster, run the following: 
microk8s join <server-ip>:25000/4788df710f77d3fc8d8c3a1e2386132a 

If the node you are adding is not reachable through the default interface you can use one of the following: 
microk8s join <server-ip>:25000/4788df710f77d3fc8d8c3a1e2386132a 
microk8s join 172.17.0.1:25000/4788df710f77d3fc8d8c3a1e2386132a 
microk8s join 10.1.39.64:25000/4788df710f77d3fc8d8c3a1e2386132a

Run the above instructions on the node to be added to the cluster. After that confirm if the host is added. The below output shows two nodes, the node was successfully added.

$ microk8s kubectl get no 
NAME        STATUS   ROLES    AGE     VERSION 
linux       Ready    <none>   3h32m   v1.19.0-34+1a52fbf0753680 
app2        Ready    <none>   86s     v1.19.0-34+1a52fbf0753680

Remove Node from Mikrok8s Cluster

To remove a node from the cluster, first run the leave command from the node to be removed

$ microk8s leave

On the remaining nodes run the command:

$ microk8s remove-node <node-ip/node.name>

Confirm it has been removed. From my output you can see that only one node is present.

$ microk8s kubectl get nodes
NAME        STATUS   ROLES    AGE     VERSION 
linux       Ready    <none>   3h32m   v1.19.0-34+1a52fbf0753680

Deploy Pods in Mikrok8s Cluster

Pods are the basic building blocks of K8s and can have of one or more containers in them. When you instantiate a Pod, you’re prompted to specify the number of replica Pods it should have. We are going to deploy nginx as an example.

$ microk8s.kubectl run nginx --replicas 2 --image nginx

To check for any existing pod in your MicroK8s environment, run the command as shown below:

$ microk8s kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE 
nginx   1/1     Running   0          4m57s

Enable MicroK8s Addons

The next steps show how to enable MicroK8s addons

Enable dashboard and DNS

Deploy dashboards and dns with the command below:

$ microk8s.enable dashboard dns
..........
service/dashboard-metrics-scraper created 
deployment.apps/dashboard-metrics-scraper created 

If RBAC is not enabled access the dashboard using the default token retrieved with: 

token=$(microk8s kubectl -n kube-system get secret | grep default-token | cut -d " " -f1) 
microk8s kubectl -n kube-system describe secret $token 

In an RBAC enabled setup (microk8s enable RBAC) you need to create a user with restricted 
permissions as shown in: 
https://github.com/kubernetes/dashboard/blob/master/docs/user/access-control/creating-sample-user.md Enabling DNS 
Applying manifest 
serviceaccount/coredns created 
configmap/coredns created 
deployment.apps/coredns created 
service/kube-dns created 
clusterrole.rbac.authorization.k8s.io/coredns created 
clusterrolebinding.rbac.authorization.k8s.io/coredns created 
Restarting kubelet 
DNS is enabled

Retrieve the token that will be used in accessing dashboard by executing the ‘token =’ and microk8s lines. Ensure to keep the token in the above output.

$ token=$(microk8s kubectl -n kube-system get secret | grep default-token | cut -d " " -f1)
$ microk8s kubectl -n kube-system describe secret $token
Name:         default-token-s8xpj 
Namespace:    kube-system 
Labels:       <none> 
Annotations:  kubernetes.io/service-account.name: default 
             kubernetes.io/service-account.uid: 2bc1562d-4287-4d4d-946c-48213b2bb8ba 

Type:  kubernetes.io/service-account-token 

Data 
==== 
ca.crt:     1103 bytes 
namespace:  11 bytes 
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6ImU1b1FkT1k5U0hZeTlTdGxSY1ZWd1hGeHlrZm55SjNtOE9NZ3o5Ni1VTU0ifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0
Lm5hbWUiOiJkZWZhdWx0LXRva2VuLXM4eHBqIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImRlZmF1bHQiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiIyYmMxNTYyZC00Mjg3LTRkNGQtOTQ2Yy00ODIxM2IyYmI4YmEiLCJzdWIiOiJzeXN0ZW06c2VydmljZWF
jY291bnQ6a3ViZS1zeXN0ZW06ZGVmYXVsdCJ9.k0x6i2andIBymc1TZM83C9uD6r-P3QuNOFxRvnAHC0WKLqqw33Vk2AEqwDsfIfqoHUQdphi71qi5d1cit3s9fLXqNxn_smIDkgmRn3eKzJHE-cmyQg7naLUq3vjCgGFGh3HTc88WFtsFqnN83QIlbnKX6WbX7oUMfoi6LkgHLpFsNf2ifGa-EU_1C7LHzn7zdfLKVS8akide7rDtEZM3_9DwbdT3wkiso0SqbDIpj
NaYRhMyAUa6QiMo2Za6Wd887VC8ZaBkDRHEfYm-2qSGUifHEJXx10c4Vzggd8ZU6aTYXx162RoY013wno42FuSkCC9F8RUTj7dp-DLDECR8Ng

Enable MicroK8s storage

Use the command:

$ microk8s.enable storage
serviceaccount/microk8s-hostpath created 
clusterrole.rbac.authorization.k8s.io/microk8s-hostpath created 
clusterrolebinding.rbac.authorization.k8s.io/microk8s-hostpath created 
Storage will be available soon

Enable MicroK8s Prometheus

Use the command as shown to enable MicroK8s Prometheus

$ microk8s.enable prometheus
service/prometheus-k8s created 
serviceaccount/prometheus-k8s created 
servicemonitor.monitoring.coreos.com/prometheus created 
servicemonitor.monitoring.coreos.com/kube-apiserver created 
servicemonitor.monitoring.coreos.com/coredns created 
servicemonitor.monitoring.coreos.com/kube-controller-manager created 
servicemonitor.monitoring.coreos.com/kube-scheduler created 
servicemonitor.monitoring.coreos.com/kubelet created 
The Prometheus operator is enabled (user/pass: admin/admin)

Set MicroK8s Prometheus and Grafana port forwarding to enable external access

$ microk8s kubectl port-forward -n monitoring service/prometheus-k8s --address 0.0.0.0 9090:9090
$ microk8s kubectl port-forward -n monitoring service/grafana --address 0.0.0.0 3000:3000

Ensure to allow the ports through the firewall if firewall is enabled

$ sudo firewall-cmd --add-port={9090/tcp,3000/tcp} --permanent
$ sudo firewall-cmd --reload

On your browser, open http://<your-server-ip>:9090 to access prometheus dashboard. You should see an output as below:

For Grafana, http://<your-server-ip>:3000. Output as shown

Enable MicroK8s registry

The command is as below:

$ microk8s.enable registry
service/registry created 
configmap/local-registry-hosting configured 
The registry is enabled

Check what is deployed for MickoK8s

To see everything deployed we use the command below:

$ microk8s.kubectl get all --all-namespaces
NAMESPACE     NAME                                             READY   STATUS    RESTARTS   AGE 
kube-system   pod/dashboard-metrics-scraper-6c4568dc68-llfgj   1/1     Running   2          22h 
kube-system   pod/metrics-server-8bbfb4bdb-7w9x2               1/1     Running   2          22h 
kube-system   pod/kubernetes-dashboard-7ffd448895-b52jk        1/1     Running   2          22h 
default       pod/nginx                                        1/1     Running   0          28m 
kube-system   pod/hostpath-provisioner-5c65fbdb4f-2knzl        1/1     Running   0          14m 
kube-system   pod/calico-node-znfw4                            1/1     Running   0          19h 
kube-system   pod/coredns-86f78bb79c-brg7h                     1/1     Running   2          22h 
kube-system   pod/calico-kube-controllers-847c8c99d-95jzq      1/1     Running   2          22h 
monitoring    pod/prometheus-adapter-557648f58c-zkbmj          1/1     Running   0          5m15s 
monitoring    pod/prometheus-operator-5b7946f4d6-87x4n         2/2     Running   0          5m57s 
monitoring    pod/grafana-7c9bc466d8-2nbn6                     1/1     Running   0          5m33s 
monitoring    pod/node-exporter-z42lk                          2/2     Running   0          5m26s 
monitoring    pod/kube-state-metrics-66b65b78bc-8gzxs          3/3     Running   0          5m30s 
monitoring    pod/alertmanager-main-0                          2/2     Running   0          4m57s 
monitoring    pod/prometheus-k8s-0                             3/3     Running   1          4m55s 

NAMESPACE     NAME                                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                        AGE 
default       service/kubernetes                  ClusterIP   10.152.183.1     <none>        443/TCP                        22h 
kube-system   service/metrics-server              ClusterIP   10.152.183.209   <none>        443/TCP                        22h 
kube-system   service/kubernetes-dashboard        ClusterIP   10.152.183.150   <none>        443/TCP                        22h 
kube-system   service/dashboard-metrics-scraper   ClusterIP   10.152.183.206   <none>        8000/TCP                       22h 
kube-system   service/kube-dns                    ClusterIP   10.152.183.10    <none>        53/UDP,53/TCP,9153/TCP         22h 
monitoring    service/prometheus-operator         ClusterIP   None             <none>        8443/TCP                       6m2s 
monitoring    service/alertmanager-main           ClusterIP   10.152.183.151   <none>        9093/TCP                       5m45s 
monitoring    service/grafana                     ClusterIP   10.152.183.245   <none>        3000/TCP                       5m35s 
monitoring    service/kube-state-metrics          ClusterIP   None             <none>        8443/TCP,9443/TCP              5m32s 
monitoring    service/node-exporter               ClusterIP   None             <none>        9100/TCP                       5m31s 
monitoring    service/prometheus-adapter          ClusterIP   10.152.183.63    <none>        443/TCP                        5m18s 
monitoring    service/prometheus-k8s              ClusterIP   10.152.183.45    <none>        9090/TCP                       5m10s 
monitoring    service/alertmanager-operated       ClusterIP   None             <none>        9093/TCP,9094/TCP,9094/UDP     5m 
kube-system   service/kubelet                     ClusterIP   None             <none>        10250/TCP,10255/TCP,4194/TCP   5m 
monitoring    service/prometheus-operated         ClusterIP   None             <none>        9090/TCP                       4m56s 

NAMESPACE     NAME                           DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE 
kube-system   daemonset.apps/calico-node     1         1         1       1            1           kubernetes.io/os=linux   22h 
monitoring    daemonset.apps/node-exporter   1         1         1       1            1           kubernetes.io/os=linux   5m31s 

NAMESPACE     NAME                                        READY   UP-TO-DATE   AVAILABLE   AGE 
kube-system   deployment.apps/dashboard-metrics-scraper   1/1     1            1           22h 
kube-system   deployment.apps/metrics-server              1/1     1            1           22h 
kube-system   deployment.apps/kubernetes-dashboard        1/1     1            1           22h 
kube-system   deployment.apps/hostpath-provisioner        1/1     1            1           14m 
kube-system   deployment.apps/coredns                     1/1     1            1           22h 
kube-system   deployment.apps/calico-kube-controllers     1/1     1            1           22h 
monitoring    deployment.apps/prometheus-adapter          1/1     1            1           5m19s 
monitoring    deployment.apps/prometheus-operator         1/1     1            1           6m4s 
monitoring    deployment.apps/grafana                     1/1     1            1           5m36s 
monitoring    deployment.apps/kube-state-metrics          1/1     1            1           5m33s 

NAMESPACE     NAME                                                   DESIRED   CURRENT   READY   AGE 
kube-system   replicaset.apps/dashboard-metrics-scraper-6c4568dc68   1         1         1       22h 
kube-system   replicaset.apps/metrics-server-8bbfb4bdb               1         1         1       22h 
kube-system   replicaset.apps/kubernetes-dashboard-7ffd448895        1         1         1       22h 
kube-system   replicaset.apps/hostpath-provisioner-5c65fbdb4f        1         1         1       14m 
kube-system   replicaset.apps/coredns-86f78bb79c                     1         1         1       22h 
kube-system   replicaset.apps/calico-kube-controllers-847c8c99d      1         1         1       22h 
monitoring    replicaset.apps/prometheus-adapter-557648f58c          1         1         1       5m20s 
monitoring    replicaset.apps/prometheus-operator-5b7946f4d6         1         1         1       6m4s 
monitoring    replicaset.apps/grafana-7c9bc466d8                     1         1         1       5m37s 
monitoring    replicaset.apps/kube-state-metrics-66b65b78bc          1         1         1       5m33s 

NAMESPACE    NAME                                 READY   AGE 
monitoring   statefulset.apps/alertmanager-main   1/1     5m1s 
monitoring   statefulset.apps/prometheus-k8s      1/1     4m57s

From the above output you can the different IPs for accessing various services dashboards, including kubernetes dashboard and metrics dashboard. Alternatively, run the below command to get various dashboards urls

$ microk8s.kubectl cluster-info
Kubernetes master is running at https://127.0.0.1:16443 
Metrics-server is running at https://127.0.0.1:16443/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy 
CoreDNS is running at https://127.0.0.1:16443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

Or this other command

$ kubectl get services -n kube-system      
NAME                        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                        AGE 
metrics-server              ClusterIP   10.152.183.209   <none>        443/TCP                        22h 
kubernetes-dashboard        ClusterIP   10.152.183.150   <none>        443/TCP                        22h 
dashboard-metrics-scraper   ClusterIP   10.152.183.206   <none>        8000/TCP                       22h 
kube-dns                    ClusterIP   10.152.183.10    <none>        53/UDP,53/TCP,9153/TCP         22h 
kubelet                     ClusterIP   None             <none>        10250/TCP,10255/TCP,4194/TCP   23m

Show MicroK8s config

The below command displays MicroK8s configs

$ microk8s.config
apiVersion: v1 
clusters: 
- cluster: 
   certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ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pS2ljMTk5TTF2ZVJPMDBhNW9jV3FVM1VOWHFlb3REYmNnYUhaYnJFZEVJdzd0UlNBOWF5WDhUb01Bb04KM1I1VTk0dmpZTzRxNVprOG9MeDllV2dUVFV0aFFiL3A1bENrckxGOTVHZklORnc0S000MnpSSUp1SW5xTFV6dwpwbWRvL0FsTGZ0T25mQndPRy9ZbFRaNHg1ZlQ4UTV3V25qd0xVMXV6MjlMSzQ2UnlPQUtHWHJ
GSmtBUGNuQUlWCmUrNVBmeDJJT0xZeC9hdXk5bjAyRlBkeXk0QVhqTnFOb2dmNWFPOEZQTi9WL1BhNE5nb0JQaHJjdVI0KzhPU1QKQnNxeFAzVT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo= 
   server: https://<server-ip>:16443 
 name: microk8s-cluster 
contexts: 
- context: 
   cluster: microk8s-cluster 
   user: admin 
 name: microk8s 
current-context: microk8s 
kind: Config 
preferences: {} 
users: 
- name: admin 
 user: 
   token: aWdjSHJWbEs4U0VaZmN5dGY1VjBveE9rNnU5ejE2SEhzbStLV1VFZ3Faaz0K

Kubernetes Learning materials:

$15.69
$156.85
in stock
Udemy.com
$15.69
$156.85
in stock
Udemy.com
$15.69
$120.65
in stock
Udemy.com
$15.69
$48.25
in stock
Udemy.com

You have successfully set up MicroK8s on Linux Mint 20. Enjoy using it! Check below more captivating Linux guides

LEAVE A REPLY

Please enter your comment!
Please enter your name here

fifteen − fourteen =