If you are looking for an easy and reliable way to deploy Kubernetes on Ubuntu, then MicroK8s is the perfect solution for you. MicroK8s is a lightweight, secure, and reliable Kubernetes distribution that can be installed in minutes, and it provides out-of-the-box support for many popular cloud-native applications.
This article will show how to install MicorK8s on an Ubuntu server and access the Kubernetes dashboard in a few simple steps.
Table of Contents
Steps to Install MicroK8s on Ubuntu
1. If you are using Ubuntu, you can directly install MicroK8s using snap. Use the “snap install microk8s” command to install microk8s quickly.
sudo snap install microk8s --classic
[sudo] password for ubuntu:
microk8s (1.26/stable) v1.26.1 from Canonical✓ installed
2. Now add your ubuntu user to the microk8s group. Our user is ubuntu, so if you have any other user, use that instead of ubuntu.
ubuntu@ub21:~$ sudo usermod -a -G microk8s ubuntu
ubuntu@ub21:~$ id ubuntu
uid=1000(ubuntu) gid=1000(ubuntu) groups=1000(ubuntu),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),122(lpadmin),133(lxd),134(sambashare),1001(microk8s)
3. Update the firewall using the “ufw allow” command
ubuntu@ub21:~$ sudo ufw allow in on cni0 && sudo ufw allow out on cni0
sudo ufw default allow routed
Rules updated
Rules updated (v6)
Rules updated
Rules updated (v6)
Default routed policy changed to 'allow'
(be sure to update your rules accordingly)
4. At this point you should be able to run Kubernetes commands like “kubectl get” or “kubectl run“.
ubuntu@ub21:~$ microk8s kubectl get all --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system pod/calico-node-mdnb9 1/1 Running 0 5m36s
kube-system pod/calico-kube-controllers-846484bf89-knvvb 1/1 Running 0 5m36s
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default service/kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 5m46s
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 5m43s
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
kube-system deployment.apps/calico-kube-controllers 1/1 1 1 5m43s
NAMESPACE NAME DESIRED CURRENT READY AGE
kube-system replicaset.apps/calico-kube-controllers-79568db7f8 0 0 0 5m42s
kube-system replicaset.apps/calico-kube-controllers-846484bf89 1 1 1 5m36s
You can also create your first pod after installing MicroK8s on Ubuntu.
buntu@ub21:~$ microk8s kubectl run nginx --image=nginx
pod/nginx created
ubuntu@ub21:~$
ubuntu@ub21:~$ microk8s kubectl get po
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 63s
Enable MicroK8s Dashboard, Core-dns & Local Storage
This installation comes with minimum components, so to enable other features such as DNS, dashboard, and local storage you will have to use the “microk8s enable” command
buntu@ub21:~$ microk8s enable dns dashboard storage
Infer repository core for addon dns
Infer repository core for addon dashboard
Infer repository core for addon storage
Enabling DNS
Using host configuration from /run/systemd/resolve/resolv.conf
Applying manifest
serviceaccount/coredns created
configmap/coredns created
deployment.apps/coredns created
service/kube-dns created
........................
........................
Infer repository core for addon hostpath-storage
Enabling default storage class.
WARNING: Hostpath storage is not suitable for production environments.
deployment.apps/hostpath-provisioner created
storageclass.storage.k8s.io/microk8s-hostpath created
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.
Wait for a few seconds and all the desired services will be up and running.
Get MicroK8s Dashboard IP Address
Now, once all the services have started, you can get the IP of MicroK8s Kubernetes dashboard by running the kubectl command along with the grep command like the below command:
buntu@ub21:~$ microk8s kubectl get all -A |grep -i "service/kubernetes-dashboard"
kube-system service/kubernetes-dashboard ClusterIP 10.152.183.92 <none> 443/TCP 15m
ubuntu@ub21:~$
Access MicroK8s Kubernetes Dashboard
As we can see the ClusterIP is 10.152.183.92 and the port is 443, so now we can try to open that in our browser i.e `https://10.152.183.92:443`
When you try to access the Kubernetes dashboard for the first time, it will ask for the token like the below screenshot.
For the token, you can run the below command and paste it into the token section to access the MicroK8s dashboard.
token=$(microk8s kubectl -n kube-system get secret | grep default-token | cut -d " " -f1)
microk8s kubectl -n kube-system describe secret $token
This will look like below code:
ubuntu@ub21:~$ token=$(microk8s kubectl -n kube-system get secret | grep default-token | cut -d " " -f1)
microk8s kubectl -n kube-system describe secret $token
Name: kubernetes-dashboard-certs
Namespace: kube-system
Labels: k8s-app=kubernetes-dashboard
Annotations: <none>
Type: Opaque
Data
====
Name: microk8s-dashboard-token
Namespace: kube-system
Labels: <none>
Annotations: kubernetes.io/service-account.name: default
kubernetes.io/service-account.uid: 31d1a1a4-0c31-48db-81e3-ddba6bf591f6
Type: kubernetes.io/service-account-token
Data
====
ca.crt: 1123 bytes
namespace: 11 bytes
token: eyJhbGciOiJSUzI1NiIsImtpZCI6ImllS09HeTBPMl9JeG1oZWZQVTlxazVIaFNVSFdvdV82UGw4ajcxVXdUTUkifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJtaWNyb2s4cy1kYXNoYm9hcmQtdG9rZW4iLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGVmYXVsdCIsImt1YmVybmV0ZXMuaWvBx51tJ1ipzmFVZV3ZLkGnt92OfYv6p7Ig2CJjOyqw1QKbjvqa6LHionuWfEXjBHusLq5TwoelHcea_YHByR0zWS9CGpIlKTV-w3Dl_Vrc-Uv8LIeVfGW4t6yK6W22OtvDH8_W7fek0OXkQUf7s-39w
Name: kubernetes-dashboard-csrf
Namespace: kube-system
Labels: k8s-app=kubernetes-dashboard
Annotations: <none>
That’s it!
Once you are logged in, you will be able to see the running pods and other services.
Conclusion
Installing MicroK8s on Ubuntu is a great way to quickly deploy a reliable Kubernetes cluster. We hope this tutorial has helped you in running MicroK8s on Ubuntu without any issues, but if you have any questions or concerns, please let us know through your comments.