geoffwilliams@home:~$

Kubernetes direnv

Kubernetes connections are normally defined in YAML files at ~/kube/config. This file normally intermingles and accumulates a bunch of server settings and keys, making it hard to add/remove kubernetes clusters.

In the past I resorted to building this whole file from fragments at login but recently I discovered direnv which eliminates the need to do this entirely.

My rough-and-ready process to enable kubernetes direnvs for the moment is:

  1. Install direnv (sudo apt install -y direnv)
  2. Add direnv to shell
    # ~/.bashrc
    eval "$(direnv hook bash)"
    echo "direnv setup"
    
  3. Create a directory for K8S files to live in, eg ~/k8s
  4. Create a subdirectory for each K8S cluster to manage, eg ~/k8s/k3s-lab
  5. Copy K3S YAML credentials from /etc/rancher/k3s/k3s.yaml on a K3S server under ~/.kube.d/, eg ~/.kube.d/k3s-lab.yaml
  6. In the K8S YAML file:
    1. find/replace default -> cluster name
    2. replace 127.0.0.1 with reachable hostname
  7. allow direnv script to run: cd ~/k8s/k3s-lab ; direnv allow
  8. Create a simple .envrc scriptto load the right K8S context file for the cluster - put one in each directory, eg ~/k8s/k3s-lab/.envrc
export K8S_CONTEXT=k3s-rook
echo $K8S_CONTEXT
export KUBECONFIG=~/.kube.d/${K8S_CONTEXT}.yaml

if [ ! -f $KUBECONFIG ] ; then
    echo "KUBECONFIG missing at $KUBECONFIG"
fi

Thats it! Test everything working and right cluster in use with kubectl get nodes:

(venv) geoff@laptop:~/homelab/k8s$ cd k3s-rook/
direnv: loading ~/homelab/k8s/k3s-rook/.envrc
k3s-rook
direnv: export +K8S_CONTEXT +KUBECONFIG
(venv) geoff@laptop:~/homelab/k8s/k3s-rook$ kubectl get nodes
NAME         STATUS   ROLES                       AGE   VERSION
kpa-rook-0   Ready    <none>                      9d    v1.31.5+k3s1
kpa-rook-1   Ready    <none>                      9d    v1.31.5+k3s1
kps-rook-0   Ready    control-plane,etcd,master   9d    v1.31.5+k3s1
kps-rook-1   Ready    control-plane,etcd,master   9d    v1.31.5+k3s1
kps-rook-2   Ready    control-plane,etcd,master   9d    v1.31.5+k3s1

Above is pretty clunky but works. I may tidy this up in the future.

Post comment

Markdown is allowed, HTML is not. All comments are moderated.