How to Setup Lightweight Kubernetes with MicroK8s and Snap on Ubuntu 18.04

Kubernetes is the most trending container-orchestration system that can be used to automating application deployment, scaling and management. But you can not deploy Kubernetes easily it can be very expensive. MicroK8s is an upstream Kubernetes deployment that allows you to run Kubernetes in Snap. MicroK8s is a single package that runs on all major Linux-based operating systems. It comes with a docker registry so you can easily create, push and deploy containers on your system. Microk8s is a fast, lightweight and runs all Kubernetes services natively.

In this tutorial, we will learn how to install MicroK8s and get your Kuberenetes cluster up and running.

Requirements

  • A server running Ubuntu 18.04 with 4 GB of RAM.
  • A root password setup on your server.

Getting Started

First, update your server with the latest version with the following command:

# apt-get update -y
# apt-get upgrade -y

Once your server is updated, restart it to apply all the changes.

Install MicroK8s

First, you will need to install Snap in order to install MicroK8s. You can install Snap with the following command:

# apt-get install snapd -y

Once the Snap has been installed, you can install MicroK8s using snap command as shown below:

# snap install microk8s --classic

Once the MicroK8s has been installed, you can check the MicroK8s information with the following command:

# snap info microk8s

You should see the following output:

name: microk8s
summary: Kubernetes for workstations and appliances
publisher: Canonical✓
contact: https://github.com/ubuntu/microk8s
license: unset
description: |
MicroK8s is a small, fast, secure, single node Kubernetes that installs on just about any Linux
box. Use it for offline development, prototyping, testing, or use it on a VM as a small, cheap,
reliable k8s for CI/CD. It's also a great k8s for appliances - develop your IoT apps for k8s and
deploy them to MicroK8s on your boxes.

You can also check the status of MicroK8s deployment with the following command:

# microk8s.status

You should see MicroK8s status with all available addons in the following output:

microk8s is running
addons:
knative: disabled
jaeger: disabled
fluentd: disabled
gpu: disabled
storage: disabled
registry: disabled
rbac: disabled
ingress: disabled
dns: disabled
metrics-server: disabled
linkerd: disabled
prometheus: disabled
istio: disabled
dashboard: disabled

To check the cluster information, run the following command:

# microk8s.kubectl cluster-info

You should see the following output:

Kubernetes master is running at https://127.0.0.1:16443

To further debug and diagnose cluster problems, use ‘kubectl cluster-info dump’.

Working with MicroK8s

You can also list the MicroK8s nodes with the following command:

# microk8s.kubectl get nodes

You should see the following output:

NAME STATUS ROLES AGE VERSION
ubuntu1804 Ready 2m14s v1.15.0

Or run the following command:

# microk8s.kubectl get nodes -o wide

You should see the following output:

NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
ubuntu1804 Ready 3m5s v1.15.0 192.168.0.2 Ubuntu 18.04 LTS 4.15.0-20-generic containerd://1.2.5

MicroK8s comes with a lot of in-built addons. You can enable and disable them as per your requirements.

You can enable dashboard dns addons with the following command:

# microk8s.enable dashboard dns

You should see the following output:

Applying manifest
secret/kubernetes-dashboard-certs created
serviceaccount/kubernetes-dashboard created
role.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
deployment.apps/kubernetes-dashboard created
service/kubernetes-dashboard created
service/monitoring-grafana created
service/monitoring-influxdb created
service/heapster created
deployment.extensions/monitoring-influxdb-grafana-v4 created
serviceaccount/heapster created
clusterrolebinding.rbac.authorization.k8s.io/heapster created
configmap/heapster-config created
configmap/eventer-config created
deployment.extensions/heapster-v1.5.2 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/wiki/Creating-sample-user

Enabling DNS
Applying manifest
serviceaccount/coredns created
configmap/coredns created
deployment.apps/coredns created
service/kube-dns created
Restarting kubelet
DNS is enabled

To enable storage addons, run the following command:

# microk8s.enable storage

You should see the following output:

Enabling default storage class
deployment.extensions/hostpath-provisioner created
storageclass.storage.k8s.io/microk8s-hostpath created
Storage will be available soon

After enabling required addons, check the cluster information again with the following command:

# microk8s.kubectl cluster-info

You should see the following output:

Kubernetes master is running at https://127.0.0.1:16443
Heapster is running at https://127.0.0.1:16443/api/v1/namespaces/kube-system/services/heapster/proxy
CoreDNS is running at https://127.0.0.1:16443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Grafana is running at https://127.0.0.1:16443/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy
InfluxDB is running at https://127.0.0.1:16443/api/v1/namespaces/kube-system/services/monitoring-influxdb:http/proxy

How to Deploy Pods and Containers

You can create and deploy Pods and Containers with MicroK8s easily.

Let’s create an Nginx deployment with one container with the following command:

# microk8s.kubectl run nginx --replicas 1 --image nginx

You should see the following output:

kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created

You can now list the deployments with the following command:

# microk8s.kubectl get deployments

You should see the following output:

NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 2m33s

And list the Pods with the following command:

# microk8s.kubectl get pods

You should see the following output:

NAME READY STATUS RESTARTS AGE
nginx-7bb7cd8db5-n88qm 1/1 Running 0 2m47s

After deploying the Nginx container, expose the Nginx service with the following command:

# microk8s.kubectl get services

You should see the following output:

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.152.183.1 443/TCP 18m
nginx ClusterIP 10.152.183.127 80/TCP 45s

If you want to remove MicroK8s, first delete Nginx deployment and service with the following command:

# microk8s.kubectl delete deployment nginx
# microk8s.kubectl delete service nginx

Next, completely remove MicroK8s from your system with the following command:

# microk8s.reset
# snap remove microk8s

Congratulations! Now you have enough knowledge to deploy and manage containers with MicroK8s. Any questions? Don´t hesitate to contact us. We´ll be glad to help out at anything that you need.

To write a comment on this article, fill out the form below. Fields marked with an asterisk (*) are required.