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 "": dial tcp 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: {}
- name: k3s-cluster
    certificate-authority-data: LS..0K
- name: k3s
    cluster: k3s-cluster
    user: k3s-admin
current-context: k3s    
- name: k3s-admin
    client-certificate-data: LS..0K
    client-key-data: LS..oa

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

$ kubectl cluster-info
Kubernetes control plane is running at
CoreDNS is running at
Metrics-server is running at

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