Create a Simple Kubernetes Cluster with MicroK8s

Overview

Kubernetes is a container orchestration tool for managing containerized workloads and services. For creating a Kubernetes cluster, you should set up a container runtime and other configurations to initialize it. With MicroK8s, you can initialize a Kubernetes cluster in a simple way for learning Kubernetes.

Prerequisites

Two or more virtual machines for 1 master node and 1 or more worker node(s) with:

  • Ubuntu 20.04 or newer

  • 2 GB memory or more

  • 20GB storage or more

  • Internet access

Steps

  1. Installing MicroK8s on the master node and worker node(s)

    On the master node and worker node(s)

     sudo snap install microk8s --classic
     sudo usermod -aG microk8s $USER
     sudo chown -fR $USER ~/.kube
     su - $USER
    
  2. Joining worker node(s) to master node

    On master node

     microk8s add-node
     ---output omitted---
     Use the '--worker' flag to join a node as a worker not running the control
     plane, eg: microk8s join [master-node-ip]:25000/[some_value]/[some_value]--worker
     ---output omitted-
    

    On worker node(s)

     microk8s join [master-node-ip]:25000/[some_value]/[some_value] --worker
     ---output omitted---
     Currently, this worker node is configured with the following kubernetes API server endpoints:
     - [master-node-ip] and port 16443, this is the cluster node contacted during the join operation.
     ---output omitted---
    
  3. Enable add-on

    On master node

     microk8s.enable dns dashboard metrics-server rbac
    
  4. Setup kubectl

    On master node

     sudo apt install apt-transport-https --yes
     sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
     echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
     sudo apt update
     sudo apt install kubectl -y
     microk8s.config > ~/.kube/config
     echo 'source <(kubectl completion bash)' >>~/.bashrc
    
  5. Labeling and tainting node(s)

     kubectl label [master-node-hostname] node-role.kubernetes.io/master=""
     kubectl label [worker-node-hostname1] node-role.kubernetes.io/worker=""
     kubectl taint [master-node-hostname1] node-role.kubernetes.io/master:NoSchedule
     kubectl label [worker-node-hostname(n)] node-role.kubernetes.io/worker=""
     kubectl taint [master-node-hostname(n)] node-role.kubernetes.io/master:NoSchedule