kubectl hackery
clux August 11, 2024 [software] #kubernetes #cliUnusual helpers for cluster level resource debugging.
Disclaimer
Most of these are very infrequently used, and may fail for certain edge cases.
At this point, a lot of people have better / different ways of interacting with Kubernetes, but despite the plethora of great TUIs I still find myself preferring having my own .k8s-helpers. Partly because there's a lot of really nice things you can do with just kubectl
(and maybe fzf
) that allows nice, immediate extensibility, and partly also because I don't necessarily trust what is being displayed in a TUI that has to make UI cconsiderations.
This article contains some stuff that I hardly ever use, and would normally bitrot in a file somewhere. They are presented here for my memory or as a potential pointer for others. If you know how to read these you should be able to modify them to your need.
Tools
For convenience here;
k
andkg
are aliases:alias k=kubectl
+alias kg=kubectl get
choose
is choose-rust (acut
/awk
replacement)yq
is whyq but the pythonyq
should also workrg
is ripgrepnumfmt
is coreutils/numfmt
then the regular gnu stuff like awk
, xargs
, sort
. Some might need mac alternative names like gawk
due to them being out of date by default on mac.
PVCs
# get all pvcs in the cluster in descending order of size
| |
# sum all the pvc size across the cluster
| | | | |
The Kubernetes mixin dashboard for PVCs is of course useful as well, but it kind of misses orphaned resources, and doesn't let you get a cluster level overview.
CPU / Memory
Usage (via builtin kubectl top
which works if you have metrics-server installed):
# total cpu usage in the cluster
| |
# total memory usage in the cluster
| | | |
Requests:
# sum cpu requests across the cluster
| |
# by name:
|
# top 20 by name
| | | |
# how much is overhead from daemonsets:
| | |
More approachable alternatives here are recommended, in particular the compute cluster dashboard from kubernetes mixin, or with the kubectl-view-allocations rust cli you can get a nice table:
) ) )
) )
) ) )
) )
PrometheusRules
# detect duplicates (accidentally deployed to other namespaces)
| |
# list all alerts with their severity (or other convention label)
|
You can write good rules for consistent labelling using pint, but that won't necessary help the stuff you have in the cluster already.
Custom / Arbitrary Resources
Stuff for orphan management:
# find crds without instances
| | \
# find ANYTHING in a namespace
|
I often just kill/recreate namespaces/crds to be sure things are truly gone in low-reliability environments, but nice to have a way to find out.
Pods
Phase specific stuff:
# find pods in bad states
# bounce evicted pods
| |
ideally you have better ways to handle this, but sometimes nice for quick overview in simpler clusters.
Fluff
Posted on mastodon. Feel free to comment / make an issue / suggest an edit.