Thursday, November 21, 2024
HomeClusteringHow to Setup Kubernetes Cluster with Microk8s

How to Setup Kubernetes Cluster with Microk8s

How to Setup Kubernetes Cluster with Microk8s: Microk8s is a small, lightweight, and fully compatible Kubernetes distribution from Canonical. It’s a minimalistic distribution focused on simplicity and performance. Given its footprint, Microk8s can be easily deployed in IoT and edge devices.

Canonical has packaged Microk8s as a snap, the company’s package manager for Linux. A snap is a bundle of an app and its dependencies that works without modification across many different Linux distributions. Snaps are independent, self-contained applications that run in a sandbox with mediated access to the host system. Snaps have emerged as an alternative to the standard .deb packages typically used in Debian-based distributions. Applications packaged as a snap can be easily installed and uninstalled.

Along with recent versions of Ubuntu, snaps can be deployed on a variety of platforms, including Linux Mint, Raspberry Pi OS, and Arch Linux.

Installation

MicroK8s is available on Linux, Windows, and macOS. A graphical Windows installer is offered on the project’s website. macOS users should use Brew while Linux users are served by snaps.

Let’s check how to install the Microk8s using snap on Ubuntu VM, for this test, we are using Ubuntu 18.04 LTS version, note: all these commands ran using root user.

If you want to install latest version please use following commands,

# snap install microk8s --classic

If you want to install specific version, you should use –channel to call the release,

# snap install microk8s --classic --channel=1.18/stable

With this, version of MicroK8s that’s based on Kubernetes v1.18.

Setup Cluster

MicroK8s bundles its own version of the Kubectl command-line tool. Use microk8s kubectl to interact with your cluster, appending a regular kubectl command:

# microk8s kubectl get all --all-namespaces

This is as like kubectl in k8s. Just add microk8s front of kubectl command and run here, it should get as like same result.

Now, lets try to deploy a pod and see how it goes.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: nginx
      image: nginx:latest
      ports:
        - name: http
          containerPort: 80
          protocol: TCP

Apply the manifest to your cluster:

# microk8s kubectl apply -f ./nginx.yaml

MicroK8s will create a new Pod running the NGINX web server. It’ll show up when you re-run

# microk8s kubectl get po
# microk8s kubectl config view --raw > $HOME/.kube/config

This exports the MicroK8s connection information into your Kubernetes configuration file, allowing plain kubectl commands to reach your cluster.

Most MicroK8s commands require superuser access. For long-term use, add your user account to the microk8s group so you don’t need to use sudo each time:

# usermod -aG microk8s $USER

Docker Images

Please note, MicroK8s can’t access your local Docker images automatically from the system. If you wish to deploy a pod from local image, you should manually import it to the MicroK8s registry by exporting it as a tar archive.

Follow this command this compress the local docker image,

# docker save my-image:latest > my-image.tar

Use this command to export the image to microk8s

# microk8s ctr image import my-image.tar

Now you can deploy the pod with your image. You can list all known images in the MicroK8s registry with the microk8s ctr images ls command.

Beyond simple use on your local machine, you should set up a dedicated Docker image registry to hold your images. Push images there and grant MicroK8s pull access so it can retrieve them from your storage. The steps to follow depend on your registry’s configuration.

Enabling the Kubernetes Dashboard

The Kubernetes web dashboard is bundled with MicroK8s but is disabled by default. You can activate it by enabling its addon, then navigating to the IP address of the dashboard service.

# microk8s enable dashboard

If you are running microk8s versions 1.18 or earlier, you can access the dashboard directly via log in through the API:

  • Retrieve the Kubernetes admin credentials:
# grep -E "username|password" ~/.kube/config
  • Retrieve the Kubernetes token. Store the token name in a variable, then print token:
# token="$(microk8s.kubectl -n kube-system get secret | grep default-token | cut -d " " -f1)" && microk8s.kubectl -n kube-system describe secret "${token}"  | grep "token:"
  • Browse to the Kubernetes dashboard with the following URL (same Host IP_or_hostname ). Log in with the admin credentials.
https://<IP_or_hostname>:16443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#/login
  • Enter the admin username and password to log in when prompted. On the Kubernetes Dashboard screen, select Token and enter the token you saved earlier. Click Sign in.
# microk8s kubectl get services --namespace kube-system

and wait for the kubernetes-dashboard service to show up. Note down its CLUSTER-IP and visit its address in your browser. The Kubernetes dashboard should appear, letting you interact with your resources using a graphical interface.

More Addons

MicroK8s by default contains with several other optional addons for common use cases. The dns addon adds support for DNS deployment, storage provides a default storage class based on host directories, and ingress offers an ingress controller that lets you create Ingress routing resources.

The registry addon is a special service that deploys a ready-to-use Docker registry on localhost:32000. Using the registry provides a private storage space which you can push your images to, before using them in your Kubernetes Pods. Similarly, the istio addon launches an Istio mesh to provide service discovery functions.

Find more details about the add-ons on https://microk8s.io/docs/addons.

Manage Cluster

All cluster management functions are provided by the microk8s command. Running microk8s status shows an overview of your cluster, including whether it’s running and the list of active addons.

To shutdown your cluster, run microk8s stop. This will stop all services, taking everything offline. You restart the cluster with the microk8s start command. It might take a few moments for all your services to get running.

You can view your cluster connection details by running

# microk8s config

This emits a Kubectl-compatible YAML file, ready to use with other tools.

Running microk8s reset will reset your cluster to its initial post-installation state. This is a destructive time-consuming operation that will destroy all your resources. It facilitates experimentation by letting you teardown everything, without removing the MicroK8s snap. Kubernetes storage volumes will be retained by default; you can delete them too by adding the –destroy-storage flag.

Final Note

MicroK8s is a minimal Kubernetes distribution that’s easily self-hosted on your own hardware. It’s fully CNFC-compliant and comes with built-in addons for the official Kubernetes dashboard, Ingress routing, and the Istio service mesh. These help you quickly set up your own production-ready cluster capable of matching the managed Kubernetes offerings from public cloud providers.

MicroK8s supports a particularly broad range of hardware and deployment architectures. It works on developer workstations, in highly available production environments, and on the Raspberry Pi. This flexibility lets you use the same technology in development and production, even down to the Kubernetes implementation.

While MicroK8s began life as a developer-centric project, targeting local testing and prototyping, it’s now billed as a production-ready Kubernetes distribution with a proven record for closely following upstream releases. Clusters can be upgraded to new Kubernetes releases using the snap refresh microk8s command.

RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments