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:
- Install direnv (
sudo apt install -y direnv
) - Add direnv to shell
# ~/.bashrc eval "$(direnv hook bash)" echo "direnv setup"
- Create a directory for K8S files to live in, eg
~/k8s
- Create a subdirectory for each K8S cluster to manage, eg
~/k8s/k3s-lab
- Copy K3S YAML credentials from
/etc/rancher/k3s/k3s.yaml
on a K3S server under~/.kube.d/
, eg~/.kube.d/k3s-lab.yaml
- In the K8S YAML file:
- find/replace
default
-> cluster name - replace
127.0.0.1
with reachable hostname
- find/replace
- allow direnv script to run:
cd ~/k8s/k3s-lab ; direnv allow
- 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.