Skip to content

An introduction to Microk8s cluster with Raspberry Pi

Notifications You must be signed in to change notification settings

hao-sdet/raspi-k8s

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Getting Started

Sources: https://microk8s.io/docs

Requirements

  • 2 or more Raspberry Pi (the Raspberry Pi 3B is recommended)
  • 2 or more SD Cards
  • 5V and 2.5A Power Supply for each Pi

Setup

Installing Ubuntu Server 20.04 LTS (64-bit server OS)

  1. Insert the SD card then run
$ diskutil list
  1. Unmount the target disk
$ diskutil unmountDisk /dev/diskN (where N is the target disk)
  1. Copy the image
$ sudo dd bs=1m if=path/to/your/image.img of=/dev/rdiskN; sync (where N is the target disk)
  1. Eject the SD card
$ sudo diskutil eject /dev/rdiskN (where N is the target disk)

Configuring your Raspberry Pi

Goal:

k8s-master
k8s-worker-01
k8s-worker-02
k8s-worker-03
  1. Enable SSH
$ sudo apt update
$ sudo apt install openssh-server
$ sudo systemctl status ssh
  1. Change the hostname
$ nano /etc/hostname
  1. Edit the hosts file:
$ nano /etc/hosts

Replace the "127.0.0.1 localhost" line with the new hostname so: "172.0.0.1 k8s-master localhost" for example. 4. Update

$ apt update && apt upgrade
  1. Reboot

Installing MicroK8s

  1. SSH into your Pi
  1. Enable c-groups (to make kubelet work out of the box)
$ sudo nano /boot/firmware/cmdline.txt
  1. Add the following options
cgroup_enable=memory cgroup_memory=1

Expected:

cgroup_enable=memory cgroup_memory=1 net.ifnames=0 dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
  1. Reboot
  2. Install MicroK8s snap
sudo snap install microk8s --classic
  1. Repeat step 0-4 for the remaining Pis

Usage

MicroK8s Add-Ons

See: https://microk8s.io/docs/addons

$ microk8s enable dns 
$ microk8s enable storage 
$ microk8s enable metallb (You’ll be prompted for a range of IP addresses here, pick one on the same subnet as the Kubenetes nodes)
$ microk8s enable ingress 
$ microk8s enable dashboard
$ microk8s enable registry

Adding/Removing a node

!You may need to enable your firewall to allow communication between nodes

sudo ufw allow in on cni0 && sudo ufw allow out on cni0
sudo ufw default allow routed
  1. Generate a connection string for master node
$ sudo microk8s.add-node
  1. Join master from other nodes
$ microk8s.join <master_ip>:<port>/<token>
Ex: $ microk8s.join 10.55.60.14:25000/JHpbBYMIevZSAMnmjMHmFwanrOYCWZLu
  1. Verify if new node has been joined/added ! To this on master node !
$ microk8s.kubectl get node
  1. Repeat step 2 for the remaining nodes you want to add to the cluster
  2. Remove a node from master
$ sudo microk8s remove-node <node name>
  1. Alternatively, to leave the cluster from any node
sudo microk8s.leave

Deploying the Application into Kubernetes

! Master node ONLY!

  1. Install Docker
$ apt-get install docker.io
$ sudo systemctl enable docker
$ sudo systemctl start docker
  1. Create the Docker File for your application ! The Raspberry Pi is an ARM based processor architecture, therefore you can only run images that are compiled for ARM, you can't run x86/x64 architecture images.
$ touch DockerFile (with your application)
  1. Create an Docker image from DockerFile
$ docker build -f Dockerfile -t my-application:local .
  1. Export Docker image
$ docker save my-application > my-application.tar
  1. Import the Docker image into Kuerbetes Repository
$ microk8s ctr image import my-application.tar
  1. Verify that the Docker image has successfully imported
$ microk8s ctr images ls
  1. Create a deployment.yaml and service.yaml files
$ touch deployment.yaml
$ touch service.yaml 
  1. Deploy your application
$ kubectl apply -f deployment.yaml
$ kubectl apply -f service.yaml
  1. Check pods status
$ microk8s kubectl get pods
  1. Show services
$ microk8s kubectl get services
  1. Remove the Application (Deployment and Service)
$ microk8s kubectl delete -f deployment.yaml
$ microk8s kubectl delete -f service.yaml
  1. Updating an Application or Service on the Fly While the application is running you can modify the content inside the service.yaml file then run the following command to update the changes
$ microk8s kubectl apply -f service.yaml

Working with Kubernetes Image Repository

!You may need to allow Insecure Registry for private secure registry See: https://microk8s.io/docs/registry-private

  1. Enable Kubernetes Image Repository !Once enabled, the registry should be lived on localhost:32000
$ microk8s enable registry
  1. Build the Docker image with tag
$ docker build . -t localhost:32000/my-application:registry
  1. Push image to Kubernetes Image Repository
$ docker push localhost:32000/my-application
  1. Deploy your container using Registry Image Update deployment.yaml file
image: 192.168.1.164:32000/my-application:registry
  1. Save and deploy
$ kubectl apply -f deployment.yaml

About

An introduction to Microk8s cluster with Raspberry Pi

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published