This page looks best with JavaScript enabled

How to install k3s on Raspberry Pi CM4 cluster using Ansible

 ·  ☕ 2 min read  ·  🐧 sysadmin

Ansible+k3s on Raspberry Pi CM4 cluster

  1. Here is a video tutorial

Requirements:

For Raspberry Pi / CM4
1
cgroup_memory=1 cgroup_enable=memory

added to

1
/boot/cmdline.txt
For Ansible
  • ansible user added to each remote node
  • ansible user added to sudo/wheel/admins group
  • sudo/wheel/admins group set in /etc/sudoers to perform command with elevated privileges
  1. Create ansible inventory
1
sudo vim /etc/ansible/hosts
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
all:
  children:
    master:
      hosts:
        master-node:
          ansible_host: 10.10.0.112
          ansible_user: ansible
    workers:
      hosts:
        worker-node-1:
          ansible_host: 10.10.0.102
          ansible_user: ansible
        worker-node-2:
          ansible_host: 10.10.0.104
          ansible_user: ansible
  1. Create ansible playbook. See the video for the explanation.
1
vim k3s-raspberry-cluster.yml 
  1. Put the below content into this file.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
---
- name: Install K3s on Master Node
  hosts: master
  become: yes
  tasks:
    - name: Install K3s
      shell: curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--disable traefik,servicelb" K3S_KUBECONFIG_MODE="644" sh -

    - name: Install NGINX as ingress controller
      shell: kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/baremetal/deploy.yaml
    - name: Create a patch file for NGINX ingress controller 
      shell:
        cmd: |
          cat > ingress.yaml << EOF
            spec:
              template:
                spec:
                  hostNetwork: true
          EOF          
      args:
        executable: /bin/bash

    - name: patch NGINX ingress controller
      shell: kubectl patch deployment ingress-nginx-controller -n ingress-nginx --patch "$(cat ingress.yaml)"

    - name: Get K3s node token
      shell: cat /var/lib/rancher/k3s/server/node-token
      register: k3s_token
      delegate_to: "{{ inventory_hostname }}"



- name: Install K3s on Worker Nodes
  hosts: workers
  become: yes
  vars:
    k3s_url: "https://{{ hostvars['master-node']['ansible_host'] }}:6443"
    k3s_token: "{{ hostvars['master-node'].k3s_token.stdout }}"
  tasks:
    - name: Join worker nodes to the cluster
      shell: "curl -sfL https://get.k3s.io | K3S_URL={{ k3s_url }} K3S_TOKEN={{ k3s_token }} sh -"


- name: Label K3s workers on Master Node
  hosts: master
  become: yes
  tasks:
    - name: Label worker 1
      shell: kubectl label nodes worker1 kubernetes.io/role=worker
    - name: Label worker 2
      shell: kubectl label nodes worker2 kubernetes.io/role=worker
  1. Run the playbook like below:
1
ansible-playbook k3s-raspberry-cluster.yml
  1. Check on the remote k3s master node the configuration using below commands:
1
2
3
4
kubectl get nodes
kubectl get pods -A
kubectl get svc -A
kubectl get all -A
Scripts and configuration files are available here:
Share on

sysadmin
WRITTEN BY
sysadmin
QA & Linux Specialist