Wednesday, July 3, 2024
HomeServerContainersDeploy Lightweight Kubernetes with MicroK8s and Snap

Deploy Lightweight Kubernetes with MicroK8s and Snap

Kubernetes is the most trending word in the sphere of Containerization and Microservices. But deploying Kubernetes can be expensive, mostly when not being done to power production applications. In this tutorial, I’ll introduce you to MicroK8s tool which enables you to run the latest stable upstream Kubernetes release in Snap.

Production deployment: Deploy Production Kubernetes Cluster with Ansible & Kubespray

For Docker users: How To run Local Kubernetes clusters in Docker

deploy kubernetes microk8s

MicroK8s is just a single package of k8s that installs on most Linux flavors and any other system which can run Snap. This is fit for running on IoT devices such as Raspberry Pi, personal computers and Desktops.

This deployment is not recommended for Production setups but only for offline development, prototyping, testing, or use it on a VM as a small, cheap, reliable k8s for CI/CD.

Features of MicroK8s – (https://microk8s.io/)

  • Fast install – Get a full Kubernetes system running in under 60 seconds.
  • Small – Use MicroK8s in your CI/CD pipelines and get on with your day without headaches.
  • Secure – Runs safely on your laptop with state of the art isolation.
  • Complete – Includes a docker registry so you can make containers, push them, and deploy them all on your laptop.
  • Upgrades – When a new major version comes out, upgrade with a single command (or automatically).
  • Featureful – Cool things you probably want to try on a small, standard K8s are all built-in. Just enable them and go.
  • GPU Passthrough – Give MicroK8s a GPGPU and your Docker containers can get all nice and CUDA.
  • Upstream – CNCF binaries delivered to your laptop, with updates and upgrades.

Setup Requirements

  • Linux Operating system – Debian, Ubuntu, CentOS, RHEL, Fedora, Arch e.t.c
  • Snap installed
  • User with sudo

Install Microk8s using Snap

We have guides on installation of Snapd:

Install Snap on Ubuntu/Debian, CentOS 7, Fedora, Arch Linux/Manjaro.

After installing Snap, you can start MicroK8s installation using Snap. Snaps are frequently updated to match each release of Kubernetes.

$ sudo snap install microk8s --classic
2023-08-21T13:03:23Z INFO Waiting for automatic snapd restart...
microk8s (1.27/stable) v1.27.4 from Canonical✓ installed

Installation should look like this:

install MicroK8s with Snap

All Published version can be checked with:

$ snap info microk8s
name:      microk8s
summary:   Kubernetes for workstations and appliances
publisher: Canonical✓
store-url: https://snapcraft.io/microk8s
contact:   https://github.com/canonical/microk8s
license:   Apache-2.0

Once MicroK8s is installed, it will start creating a one node Kubernetes cluster. Status for this deployment can be checked using.

# microk8s.status 
microk8s is running
addons:
 jaeger: disabled
 fluentd: disabled
 gpu: disabled
 storage: disabled
 registry: disabled
 ingress: disabled
 dns: disabled
 metrics-server: disabled
 prometheus: disabled
 istio: disabled
 dashboard: disabled

From the output, we can see is MicroK8s is running and the state of addons available.

To check cluster info, use:

# microk8s.kubectl cluster-info
Kubernetes master is running at https://127.0.0.1:16443

Also get status of k8s node.

# microk8s.kubectl get nodes
NAME    STATUS   ROLES    AGE   VERSION
jammy   Ready    <none>   40s   v1.27.4

If you’re not comfortable with microk8s.kubectl command, you can create an alias for it.

echo "alias kubectl='microk8s.kubectl'" >>~/.bashrc
source ~/.bashrc

Then you can use kubectl command:

# kubectl  get nodes -o wide
NAME    STATUS   ROLES    AGE   VERSION   INTERNAL-IP      EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
jammy   Ready    <none>   62s   v1.27.4   128.140.96.144   <none>        Ubuntu 22.04.2 LTS   5.15.0-78-generic   containerd://1.6.15

Enable/Disable Kubernetes Addons on MicroK8s

Addons available for MicroK8s are:

  • dashboard: Deploy kubernetes dashboard as well as grafana and influxdb.
  • dns: Deploy kube dns
  • storage: Create a default storage class. This storage class makes use of the hostpath-provisioner pointing to a directory on the host. Persistent volumes are created under ${SNAP_COMMON}/default-storage.
  • ingress: Create an ingress controller.
  • istio: Deploy the core Istio services. Use the microk8s.istioctl command to manage your deployments.
  • registry:  Deploy an image private registry and expose it on localhost:32000
  • metrics-server: Deploy the Metrics Server.
  • prometheus: Deploy the Prometheus Operator v0.25.
  • fluentd: Deploy Elasticsearch-Kibana-Fluentd logging and monitoring solution.
  • jaeger: Deploy the Jaeger Operator v1.8.2 in the “simplest” configuration.

You can enable and disable addons available for Kubernetes.

Enable Addons

Use the microk8s.enable command to enable addons.

# microk8s.enable dashboard dns
nabling Kubernetes Dashboard
Infer repository core for addon metrics-server
Enabling Metrics-Server
serviceaccount/metrics-server created
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
clusterrole.rbac.authorization.k8s.io/system:metrics-server created
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
service/metrics-server created
deployment.apps/metrics-server created
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
clusterrolebinding.rbac.authorization.k8s.io/microk8s-admin created
Metrics-Server is enabled
Applying manifest
serviceaccount/kubernetes-dashboard created
service/kubernetes-dashboard created
secret/kubernetes-dashboard-certs created
secret/kubernetes-dashboard-csrf created
secret/kubernetes-dashboard-key-holder created
configmap/kubernetes-dashboard-settings created
role.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrole.rbac.authorization.k8s.io/kubernetes-dashboard created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
deployment.apps/kubernetes-dashboard created
service/dashboard-metrics-scraper created
deployment.apps/dashboard-metrics-scraper created
secret/microk8s-dashboard-token created

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

microk8s kubectl describe secret -n kube-system microk8s-dashboard-token

Use this token in the https login UI of the kubernetes-dashboard service.

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

Addon core/dns is already enabled

Enable Storage addon:

# microk8s.enable storage
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.

To enable Storage and Istio addons:

microk8s.enable istio

The same format apply for other plugins. Confirm enabled addons with:

# microk8s.status
microk8s is running
addons:
 jaeger: disabled
 fluentd: disabled
 gpu: disabled
 storage: enabled
 registry: enabled
 ingress: disabled
 dns: disabled
 metrics-server: disabled
 prometheus: disabled
 istio: disabled
 dashboard: enabled

For more information like URL, use:

microk8s.kubectl cluster-info

Output:

MicroK8s Cluster details

Disable Addons

Use microk8s.disable command to disable addon.

# microk8s.disable istio
Disabling Istio
namespace "istio-system" deleted
Istio is terminating

Deploying Pods and Containers on MicroK8s

Deployments are done in standard Kubernetes way. See the example below which will create Nginx deployment with two containers.

# microk8s.kubectl run nginx --replicas 2 --image nginx
deployment.apps/nginx created

# microk8s.kubectl get deployments
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   2/2     2            2           39s

# microk8s.kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7db9fccd9b-7662b   1/1     Running   0          61s
nginx-7db9fccd9b-87z6d   1/1     Running   0          61s

Expose service:

# microk8s.kubectl expose deployment nginx --port 80 --target-port 80 --type ClusterIP --selector=run=nginx --name nginx
service/nginx exposed

# microk8s.kubectl get services
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.152.183.1            443/TCP   27m
nginx        ClusterIP   10.152.183.54           80/TCP    104s

Delete Deployment

# microk8s.kubectl delete deployment nginx
deployment.extensions "nginx" deleted

# microk8s.kubectl delete service nginx
service "nginx" deleted

Stopping and Restarting MicroK8s

You can easily shutdown MicroK8s when not in use without un-installing it.

# microk8s.stop
Stopped.

Start MicroK8s using:

# microk8s.start
Started.

Removing MicroK8s

If you wish to completely remove MicroK8s, first stop all running pods.

microk8s.reset

Then remove MicroK8s snap.

snap remove microk8s

Conclusion

MicroK8s is the quickest and most lightweight Kubernetes deployment tool available. It is ideal for playing with Kubernetes without mastery of Linux and Containers concepts before you can get started. For more reading, visit MicroK8s Documentation page.

Similar Articles:

Thapelo Manthata
I’m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

Most Popular

Recent Comments