Tutorial

How to Setup a MicroK8s Kubernetes Cluster on Ubuntu 22.04

How to Setup a MicroK8s Kubernetes Cluster on Ubuntu 22.04

The author selected Open Source Initiative to receive a donation as part of the Write for DOnations program.

Introduction to MicroK8s

While many options are available for a Kubernetes cluster, not all follow a simple setup. MicroK8s, developed by Canonical, simplifies the Kubernetes cluster setup process through its single command binary installation and can be used to run Kubernetes cluster on local workstations, VMs, and edge IoT devices because of its low memory and CPU footprint. With added support of self-healing and high-availability features, it could also be a choice for hosting production-grade workloads.

In this tutorial, you will learn how to install MicroK8s on Ubuntu and get started with a Kubernetes cluster.

Prerequisites

To complete this tutorial, you will need:

Step 1: Install MicroK8s

In this step, you will install the latest version of MicroK8s on your Ubuntu machine.

Login to your server as your sudo-enabled user (in this tutorial, it will be Sammy) using the following command if using password-based login:

  1. ssh sammy@your_server_ip

Next, install the MicroK8s using the following command.

  1. sudo snap install microk8s --classic

You will be prompted to enter the user’s password.

The command uses snap to install and start the latest stable available version of the MicroK8s-based Kubernetes cluster.

You will receive an output similar to this:

microk8s (1.28/stable) v1.28.3 from Canonical✓ installed

Next, you can check the status of the MicroK8s cluster by running the following command.

  1. sudo microk8s status

This command will show the status of MicroK8s as running.


microk8s is running

high-availability: no

  datastore master nodes: 127.0.0.1:19001

  datastore standby nodes: none

addons:

  enabled:

    dns                  # (core) CoreDNS

    ha-cluster           # (core) Configure high availability on the current node

    helm                 # (core) Helm - the package manager for Kubernetes

    helm3                # (core) Helm 3 - the package manager for Kubernetes

  disabled:

    cert-manager         # (core) Cloud native certificate management

    cis-hardening        # (core) Apply CIS K8s hardening

    community            # (core) The community addons repository

    dashboard            # (core) The Kubernetes dashboard

Wait a few minutes and rerun the command if you receive an output like microk8s is not running.

You installed MicroK8s on Ubuntu in this step to create a single-node Kubernetes cluster. Next, you will look at the default deployed Kubernetes objects on the cluster.

Step 2: Check Default Kubernetes Objects

In this step, you will check the default Kubernetes objects deployed after installing MicroK8s.

MicroK8s come pre-bundled with its version kubectl and can execute the native Kubernetes commands to inspect and work with the cluster.

Execute the following command to see all Kubernetes objects deployed in the cluster in the kube-system namespace.

  1. sudo microk8s kubectl get all -n kube-system

You will receive an output similar to this:


NAME                                         READY   STATUS    RESTARTS   AGE

pod/coredns-864597b5fd-kwljt                 1/1     Running   0          93s

pod/calico-node-t5dmh                        1/1     Running   0          93s

pod/calico-kube-controllers-77bd7c5b-s4dwt   1/1     Running   0          93s

  
NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE

service/kube-dns   ClusterIP   10.152.183.10   <none>        53/UDP,53/TCP,9153/TCP   97s

  NAME                         DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR                AGE

daemonset.apps/calico-node       1         1         1       1            1           kubernetes.io/os=linux     98s

  
  
 NAME                                      READY   UP-TO-DATE   AVAILABLE   AGE

  
deployment.apps/coredns                   1/1     1            1           97s

deployment.apps/calico-kube-controllers   1/1     1            1           98s

   
NAME                                               DESIRED   CURRENT   READY   AGE

  
replicaset.apps/coredns-864597b5fd                 1         1         1       94s

replicaset.apps/calico-kube-controllers-77bd7c5b   1         1         1       94s

The above output shows different objects deployed within the Kubernetes cluster.

You may get the following error if you run the command without using sudo.

Insufficient permissions to access MicroK8s.
...

To avoid using microk8s as a prefix while running kubectl commands, you can add an alias if you don’t have an existing installation of  kubectl using the following command.

  1. alias kubectl='sudo microk8s kubectl'

Now, you can execute kubectl commands directly without the prefix.

  1. kubectl get nodes

In case you want to use native kubectl for executing the commands, copy the MicroK8s generated kubeconfig to the ~/.kube/config file by using the following command.

  1. mkdir ~/.kube
  2. sudo microk8s kubectl config view --raw > ~/.kube/config

Now, you can use the native kubectl as well to run the commands.

  1. kubectl get pods -A

In this step, you have verified the status of Kubernetes objects deployed in the MicroK8s cluster by default. Next, you will understand the different options and addons available in MicroK8s.

Step 3: Understand Addons in MicroK8s

The default installation of MicroK8s comes with the essential Kubernetes components to get the cluster up and running. Installing additional components using the addons functionality provided by MicroK8s is possible.

For example, you can use the following command to enable the CoreDNS component in your cluster.

  1. sudo microk8s enable dns

To enable the ingress controller, you can use the following command.

  1. sudo microk8s enable ingress

You will receive the following output upon enabling the ingress.


Infer repository core for addon ingress

Enabling Ingress

ingressclass.networking.k8s.io/public created

ingressclass.networking.k8s.io/nginx created

namespace/ingress created

serviceaccount/nginx-ingress-microk8s-serviceaccount created

clusterrole.rbac.authorization.k8s.io/nginx-ingress-microk8s-clusterrole created

role.rbac.authorization.k8s.io/nginx-ingress-microk8s-role created

clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-microk8s created

rolebinding.rbac.authorization.k8s.io/nginx-ingress-microk8s created

configmap/nginx-load-balancer-microk8s-conf created

configmap/nginx-ingress-tcp-microk8s-conf created

configmap/nginx-ingress-udp-microk8s-conf created

daemonset.apps/nginx-ingress-microk8s-controller created

Ingress is enabled

To get the complete list of available addons, execute the sudo microk8s status command, and the output shows the list.


  disabled:

    cert-manager         # (core) Cloud native certificate management

    cis-hardening        # (core) Apply CIS K8s hardening

    community            # (core) The community addons repository

    dashboard            # (core) The Kubernetes dashboard

    gpu                  # (core) Automatic enablement of Nvidia CUDA

    host-access          # (core) Allow Pods connecting to Host services smoothly

    hostpath-storage     # (core) Storage class; allocates storage from host directory

    ingress              # (core) Ingress controller for external access

    kube-ovn             # (core) An advanced network fabric for Kubernetes

    mayastor             # (core) OpenEBS MayaStor

    metallb              # (core) Loadbalancer for your Kubernetes cluster

    metrics-server       # (core) K8s Metrics Server for API access to service metrics

    minio                # (core) MinIO object storage

    observability        # (core) A lightweight observability stack for logs, traces and metrics

    prometheus           # (core) Prometheus operator for monitoring and logging

    rbac                 # (core) Role-Based Access Control for authorisation

    registry             # (core) Private image registry exposed on localhost:32000

    rook-ceph            # (core) Distributed Ceph storage using Rook

    storage              # (core) Alias to hostpath-storage add-on, deprecated

You can use the microk8s disable command to turn off any addon.

For example, execute the following command to disable the ingress controller and delete all related resources.

  1. microk8s disable ingress

You will receive the following output upon disabling the ingress.


Infer repository core for addon ingress

Disabling Ingress

ingressclass.networking.k8s.io "public" deleted

ingressclass.networking.k8s.io "nginx" deleted

namespace "ingress" deleted

serviceaccount "nginx-ingress-microk8s-serviceaccount" deleted

clusterrole.rbac.authorization.k8s.io "nginx-ingress-microk8s-clusterrole" deleted

role.rbac.authorization.k8s.io "nginx-ingress-microk8s-role" deleted

clusterrolebinding.rbac.authorization.k8s.io "nginx-ingress-microk8s" deleted

rolebinding.rbac.authorization.k8s.io "nginx-ingress-microk8s" deleted

configmap "nginx-load-balancer-microk8s-conf" deleted

configmap "nginx-ingress-tcp-microk8s-conf" deleted

configmap "nginx-ingress-udp-microk8s-conf" deleted

daemonset.apps "nginx-ingress-microk8s-controller" deleted

Ingress is disabled

In this step, you learned how to work with addons in MicroK8s. Next, you will stop the MicroK8s cluster and uninstall it.

Step 4: Stop and Uninstall MicroK8s

To stop the running MicroK8s cluster, execute the following command. This will stop all the running Kubernetes components in the virtual machine.

  1. sudo microk8s stop

Next, you can uninstall the MicroK8s using the following command. This removes the installed microk8s snap package from the VM.

  1. sudo snap remove microk8s

Conclusion

In this article, you installed a MicroK8s cluster on Ubuntu and understood the steps to work with a MicroK8s-based Kubernetes cluster. Now that you have explored MicroK8s, you should also see the workings of K3S and how it can also be utilized to create a Kubernetes cluster.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors

Default avatar

Manager, Developer Relations


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel