DEV Community

Cover image for Raspberry pi 4B kubernetes cluster
NULLX
NULLX

Posted on • Edited on

Raspberry pi 4B kubernetes cluster

I love decentralized computing. It’s beautiful. Just add a node to your cluster if you need more power! Just that. Restart the pods with no downtime, horizontal pod autoscaler...

To create a cluster, we can do it with services provided by google, amazon… or just manually installing it in each node.

I prefer to create my own mini cluster. My cluster is made of 6 raspberry pi 4B (4GB RAM each) and has it's own name: clusperry ⚡️

Cluster preview

I created a simple gui tool to generate the linux images to flash into the mini-SD cards with the initial cloudconfig. You can check it here: https://github.com/nullxx/clusperry-installer.

Nodes setup

Download the release from https://github.com/nullxx/clusperry-installer/releases/ (its currently only compiled for macOS)

1. Select the nodes operating system

SELECT OS

2. Configure each node with your configuration:

  • IP
  • WIFI (or ethernet)
  • hostname
  • SSH keys CONFIGURE NODES

3. Download OS images

Download

4. Write images

Write images

5. Open generated images

Open generated images

Install kubernetes with k3s

For this I will use ansible. It makes it easier to do all the install work.
I will use the following repo: https://github.com/k3s-io/k3s-ansible

Clone the repo

git clone https://github.com/k3s-io/k3s-ansible.git
Enter fullscreen mode Exit fullscreen mode

Get into the cloned repo

cd k3s-ansible
Enter fullscreen mode Exit fullscreen mode

Create our inventory from the sample

cp -R inventory/sample inventory/my-cluster
Enter fullscreen mode Exit fullscreen mode

Edit inventory/my-cluster/hosts.ini

nano inventory/my-cluster/hosts.ini
Enter fullscreen mode Exit fullscreen mode
[master]
192.168.1.100

[node]
192.168.1.101
192.168.1.102
192.168.1.103
192.168.1.104
192.168.1.105

[k3s_cluster:children]
master
node
Enter fullscreen mode Exit fullscreen mode

Edit inventory/my-cluster/group_vars/all.yml

In my case edit the ansible_user to ubuntu

---
k3s_version: v1.17.5+k3s1
ansible_user: ubuntu
systemd_dir: /etc/systemd/system
master_ip: "{{ hostvars[groups['master'][0]]['ansible_host'] | default(groups['master'][0]) }}"
extra_server_args: ""
extra_agent_args: ""
Enter fullscreen mode Exit fullscreen mode

Be ready for the power!

Execute the ansible-playbook to install k8s in the nodes.

ansible-playbook site.yml -i inventory/my-cluster/hosts.ini
Enter fullscreen mode Exit fullscreen mode

Install kubectl in your computer

In my case macOS

brew install kubectl
Enter fullscreen mode Exit fullscreen mode

Get the kubectl config from any of your master nodes

scp ubuntu@192.168.1.100:~/.kube/config ~/.kube/config
Enter fullscreen mode Exit fullscreen mode

Verify installation.

Check that all nodes are in STATUS 'Ready'

kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

Deploy test

Create a local DNS entry for the test

sudo echo "192.168.1.100 test.com" >> /etc/hosts
Enter fullscreen mode Exit fullscreen mode

I'm going to use traefik because is installed by default with k3s.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    name: nginx-service
spec:
  ports:
    - port: 80
      name: http
  selector:
    name: nginx-deployment
---

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - host: "test.com"
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80
Enter fullscreen mode Exit fullscreen mode

 Verify pods are up

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Go to http://test.com on your browser

curl http://test.com
Enter fullscreen mode Exit fullscreen mode

Its working!

Cleanup

Remember to remove the test.com line in /etc/hosts

Persistent storage

Part two available here

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.