a .zshrc that has been near a cluster for a while ends up with a block like kgp, kgpw, kdp, klf, each one kubectl plus a subcommand plus the flags typed most often. they work in the shell that defined them and nowhere else: not inside watch, not in a script, not anywhere zsh did not load .zshrc first. each one also needs its own compdef line before tab completion knows what it is.
since 1.34 kubectl reads ~/.kube/kuberc, a preferences file with two jobs: aliases that behave like kubectl subcommands, and default values for flags on built-in commands. it lives next to the kubeconfig and never inside it, so a kubeconfig handed out by a cloud provider or regenerated by a login tool cannot overwrite it.
the short version
apiVersion: kubectl.config.k8s.io/v1beta1
kind: Preference
aliases:
- name: getw
command: get
options:
- name: output
default: wide
defaults:
- command: delete
options:
- name: interactive
default: "true"kubectl getw pods now runs kubectl get pods --output=wide, and every kubectl delete asks before it deletes. a flag typed on the command line always beats the value in the file, so kubectl getw pods -o name prints names and kubectl delete pod x --interactive=false skips the prompt.
the feature was alpha in 1.33 behind KUBECTL_KUBERC=true and is beta, on by default, from 1.34, with stable targeted for 1.38. the credential plugin policy in the same file went beta in 1.35.
what kubectl does with it
kuberc rewrites the command line before kubectl parses it. the alias is swapped for its command, and the defaults are added for every flag the command line does not already set:
kubectl getw pods -n api
│
▼
alias getw ─▶ command: get, output=wide
│
├─ --output not on the command line ─▶ kubectl get --output=wide pods -n api
│
└─ -o name on the command line ──────▶ kubectl get pods -o name -n apithe rewrite is visible at -v=6, which is the fastest way to check what an alias turns into:
kubectl getw pods -v=6 2>&1 | grep 'original command'I1104 13:16:30.597109 909 cmd.go:371] original command: "kubectl get --output=wide pods -v=6"an alias can also carry arguments. prependArgs go straight after the command, which is how an alias pins a resource type, and appendArgs go at the end, which suits anything after a --:
aliases:
# kubectl getn -> kubectl get nodes --output wide
- name: getn
command: get
prependArgs:
- nodes
options:
- name: output
default: wide
# kubectl logf deploy/api -> kubectl logs deploy/api --follow --timestamps
- name: logf
command: logs
options:
- name: follow
default: "true"
- name: timestamps
default: "true"command takes subcommands too, so an alias or a default can target create role or set env.
what stays in the shell
kuberc aliases are subcommands of kubectl, so they cannot rename kubectl itself. alias k=kubectl stays in .zshrc, with compdef k=kubectl beside it. what moves is everything that came after the k: kgpw becomes k getw pods, which is longer to type but completes its arguments.
arguments to an alias complete as the underlying command’s do, so kubectl getw po<tab> offers pods and podtemplates. the alias names are not offered: kubectl ge<tab> still completes only to get, so nothing reminds you an alias exists.
keeping it in dotfiles
kuberc is one file under ~/.kube, a directory that also holds the kubeconfig and kubectl’s discovery cache. with stow it is a package of its own:
~/.dotfiles/
├── kube/
│ └── .kube/
│ └── kuberc
└── zsh/
└── .zshrcstow it with --no-folding. if ~/.kube does not exist yet, stow’s default is to replace the whole directory with a single symlink into the repository, and the next kubectl config use-context or cache refresh writes the kubeconfig and cache files into the working tree. --no-folding creates a real ~/.kube with one symlink inside it.
what to watch out for
a broken kuberc breaks every kubectl command. a file that does not parse makes everything exit 1, kubectl version --client included:
error occurred while applying preferences kuberc error kuberc: no valid preferences found in /home/user/.kube/kuberc, use --v=5 to see detailsthe same happens when KUBERC names a file that does not exist. a missing file at the default path is fine. KUBERC=off skips the file entirely, which is the way out when the file itself is what broke.
the alias name is cut out of every argument. in kubectl 1.37 the rewrite removes the alias name wherever it appears on the command line, inside other words included:
# typed
kubectl logf deploy/logforwarder
# run
kubectl logs --follow=true --timestamps=true deploy/orwarder
# typed
kubectl logf deploy/api -l app=logf
# run
kubectl logs --follow=true --timestamps=true deploy/api -l app=the same cut empties a prependArgs entry that matches the alias name, so an alias called nodes that prepends nodes runs a bare kubectl get. pick alias names that cannot turn up inside a resource name, label or argument, and check anything surprising at -v=6.
an alias that collides with a built-in is ignored. an alias named get or logs never runs, and kubectl gives no warning, not even at -v=5. if an alias seems to do nothing, check its name against kubectl --help first.
a misspelled option fails only when the alias runs. name: outptu passes every other command and fails the moment the alias is used, with invalid alias flag outptu in alias kubectl. run each alias once after changing it.
defaults apply to scripts too. anything that runs kubectl as your user reads the same file, so a script calling kubectl delete gets --interactive=true added just like your terminal does. scripts and ci jobs that depend on stock behaviour should set KUBERC=off.
kubectl kuberc set rewrites the whole file. it writes through a stowed symlink without breaking it, but it re-serialises the yaml: every comment is dropped and the keys are sorted. if the file is kept in git with comments, edit it by hand and use kubectl kuberc view to check what kubectl parsed.
yaml document markers count. a comment header followed by --- makes the header its own empty document. kubectl skips it with a strict decoding error at -v=6 and uses the next one. it works, but the marker adds nothing to a one-document file, so leave it out.
references
[1] kubernetes documentation. “kubectl user preferences (kuberc).”
kubernetes.io/docs/reference/kubectl/kuberc
[2] kubernetes documentation. “kuberc configuration (v1beta1).”
kubernetes.io/docs/reference/config-api/kuberc.v1beta1
[3] kubernetes enhancements. “kep-3104: introduce kuberc.”
github.com/kubernetes/enhancements/tree/master/keps/sig-cli/3104-introduce-kuberc
[4] gnu stow documentation. “tree folding.”
gnu.org/software/stow/manual/html_node/Installing-Packages.html