Because kubernetes is becoming so pervasive, one of the first obstacles to working with kubernetes is learning how to jump between different clusters. There are many, many ways to shave this yak. Here's the one I am settling into.
Just after writing this page, I learned more idomatic use of kubectl config commands. People developing kubernetes have already devised solutions for managing access to many clusters.
My current practice is from here to the pagefold.
There are benefits to adhering to idioms. I use a zsh module for kubernetes which turns out to also understand the idomatic kubectl config. It includes the name of the current cluster and namespace in the command prompt. oh-my-zsh k8s plugin ![]()
kubectl config --help Modify kubeconfig files using subcommands like "kubectl config set current-context my-context"
A kubeconfig context groups a user, a cluster, and a namespace.
In day-to-day use, I find myself mostly switching between contexts. Here's how I remember my current configured contexts:
kubectl config get-contexts
Here is an example switching to my local wiki:
kubectl config use-context wiki
A few times I have had to raze my local cluster. Here's an example:
k3d cluster delete wiki rm -rf ~/.config/k3d mkdir -p /tmp/$USER/.kube mv ~/.kube/* /tmp/$USER/.kube k3d cluster create wiki \ --server-arg --tls-san="127.0.0.1" \ --port 80:80@loadbalancer \ --volume "$HOME/.wiki-k8s:/macos/.wiki-k8s" \ --volume "$HOME/workspace/fedwiki:/macos/fedwiki" \ --wait
.
My previous practice is here, below the pagefold. I hang onto these notes for use when experimenting to avoid cluttering my ~/.kube/config.
First, I leave KUBECONFIG empty by default to protect myself from certain types of accidents—the ones where I mistakenly destroy resources I cared about in the wrong cluster.
This also means I find myself regularly running this set of commands to ensure I'm in a clean state:
mv ~/.kube/config ~/.kube/config-$(date +%Y-%m-%d) unset KUBECONFIG kubectl cluster-info &> /dev/null \ || echo ALL CLEAR
Second, I define shell aliases for the clusters I'm currently working on. In this example there are aliases for two local KinD clusters, one local K3D cluster, and a production cluster.
alias kwiki="$HOME/.kube/k3d-wiki.yaml" alias kdojo="$HOME/.kube/kind-dojo.yaml" alias kargo="$HOME/.kube/kind-argo.yaml" alias KProd="$HOME/.kube/production.yaml"
The combination of empty default config and aliases means I must be intentional about which cluster I intend to use with every kubectl command I type.
The use of capital letters in the alias for production clusters makes it physically harder to change production 'cos of needing to hold down the shift key.
Shell aliases also integrate well with a zsh module I also have configured which offers many shortcuts for common commands and makes excellent use of tab completion.
Refresh my memory about what I have set up for my local wiki?
kwiki kga -A
which expands to
KUBECONFIG=$HOME/.kube/k3d-wiki.yaml kubectl get all -A
Create new clusters to experiment with shiny new kubernetes tools. Create the alias first and use the alias with the kind create command to ensure the kube config file lands in ~/.kube directory following any preferred naming convention.
alias kfoo="KUBECONFIG=$HOME/.kube/kind-foo.yaml" kfoo kind create cluster --name=foo kfoo kaf ./foo.yaml # kubectl apply -f ./foo.yaml