Helm With K3s On Ubuntu

I’ve recently needed to use Helm with K3s on Ubuntu. helm is a separate application from Kubernetes that uses the standard K8s APIs in order to interface with whatever Kubernetes cluster it’s pointed at. On Ubuntu, it’s not available via apt and must be installed as a Snap:

sudo snap install helm --classic

Helm will typically source its connection URL for K8s via the KUBECONFIG environment variable. This is problematic depending on your setup (i.e. mine) because the context is lost when running commands via sudo, even if /root/.bashrc has the appropriate export:

export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

Note: The path to the K3s YAML comes from the Cluster Access documentation.

If Helm isn’t getting the correct URL for K8s, you’ll generally see an error where it tries to use its default local URL:

Error: Kubernetes cluster unreachable: Get "http://localhost:8080/version": dial tcp 127.0.0.1:8080: connect: connection refused

That’s not what K3s uses by default. If you crack open the k3s.yaml file you’ll see a directive like this:

server: https://127.0.0.1:6443

However, the KUBECONFIG environment variable isn’t the only option for instructing Helm; Helm can also accept a CLI parameter with the config location:

sudo helm list --kubeconfig /etc/rancher/k3s/k3s.yaml

This will work, but it’s annoying to specify --kubeconfig every… single… time. An easy solution to this is to create a wrapper script that lives in the PATH before the Snap, which is generally going to be last. You can check the available directories with:

sudo echo $PATH

You’ll probably see output like:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Anything before /snap/bin will work, though I recommend /usr/local/bin since that’s where the K8s scripts and symbolic links already live. Create a file called helm, mark it as executable, and then open it in your favorite text editor.

sudo touch /usr/local/bin/helm
sudo chmod +x /usr/local/bin/helm
sudo vim /usr/local/bin/helm

In the file, use the following contents:

#!/usr/bin/env bash

sudo /snap/bin/helm "$@" --kubeconfig /etc/rancher/k3s/k3s.yaml

Close the file, and now helm works as expected with something like:

sudo helm list