K3S on Raspberry Pi 4 - kubectl outside cluster

December 19, 2023

Now that I have a cluster set up (see https://drumcoder.co.uk/blog/2023/dec/19/k3s-on-raspberry-pi-4-initial-setup/ for setup details) I wanted to be able to access it from my mac, without sshing into the cluster master node.

Installing kubectl

The kubectl command can be installed on a mac using:

$ brew install kubectl

Install can be confirmed by running

$ kubectl version --client                    
Client Version: v1.29.0
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3

At this point, we're not connected to the cluster, so this command will just sit there before failing with a timeout

$ kubectl cluster-info
E1219 11:59:06.947565    3078 memcache.go:265] couldn't get current server API group list: 
Get "https://192.168.1.10:6443/api?timeout=32s": dial tcp 192.168.1.10:6443: i/o timeout

Connecting to the cluster

So now we need to connect our local kubectl to the cluster.

On the master node, we need the contents of the file at /etc/rancher/k3s/k3s.yaml, and we need to copy this into a local file on the mac - ~/.kube/config, whilst changing the line the starts server so that is has the correct IP for our master node. I ended up with a file that looked like this:

apiVersion: v1 
kind: Config
preferences: {}
clusters:
- name: k3s-cluster
  cluster:
    certificate-authority-data: LS..0K
    server: https://192.168.1.20:6443
contexts:
- name: k3s
  context:
    cluster: k3s-cluster
    user: k3s-admin
current-context: k3s    
users:
- name: k3s-admin
  user:
    client-certificate-data: LS..0K
    client-key-data: LS..oa

So now we get check that we can access the cluster:

 :::text
$ kubectl cluster-info
Kubernetes control plane is running at https://192.168.1.20:6443
CoreDNS is running at https://192.168.1.20:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://192.168.1.20:6443/api/v1/namespaces/kube-system/services/https:metrics-server:https/proxy

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