Everything Useful I Know About kubectl
Ok, mostly everything.
Also check out the official kubectl cheatsheet. This community cheat sheet is also very thorough.
Iâm going to put a â next to things Iâve found that are less common. A lot of this is already well documented.
Quick Note on Aliasing k
Before we start, I recommend adding the following snippet to your .bashrc
or .zshrc
. Writing k
instead of kubectl
is nice, and you want autocompletion.
Get Help
kubectl
is self-documenting, which is another reason why itâs 100x better than a UI. If youâre confused about some command, just do k <command> --help
. e.g. k label --help
. There will be lots of examples in the help docs.
Minimize the use of imperative commands
There are many commands, like create
, expose
, scale
, set image
, rollout
, etc. that I donât use that much.
Thatâs because itâs better to deal with your Kubernetes configuration declaratively, and have a CI/CD system run the equivalent of kubectl apply -f .
.
You should learn how to use these commands, but they shouldnât be a regular part of your prod workflows. That will lead to a flaky system.
Basic Getting of Details
You can k get
any Kubernetes API Resource.
Describing the pod can help you figure out why it crashed. Perhaps youâre trying to inject a config map that doesnât exist into its environment.
Furthermore, they often have abbreviations. To see what resources you can get and their abbreviations, use the api-resources
command. This is helpful as well with custom resources.
If youâre unsure what some resource is, use k explain
.
Troubleshooting
Getting Logs
â Debugging DNS and Services
This first tip is in the Debugging DNS Resolution article in the docs, but here is a quick imperative command to get the same result.
If youâre wondering if you created a service correctly, you should also look into k get endpoints
, and also double check the label selector. e.g.
What image is the pod running?
Decode and Copy a Secret
Metrics / Monitoring
For more you can do with metrics, see here.
Executing commands on pods or nodes
Once youâve started a shell session on a pod or node, curl
, nslookup
, ping
, and env
are useful.
What is the podâs IP?
Validate Kubernetes Resources
Iâve seen a lot of time wasted from people struggling to write YAML.
Making Changes
Recall my note on not abusing the imperative commands. Iâm serious about that.
Scale a Deployment
or StatefulSet
Restarting / Deleting Workflows
If a pod is not owned by a Deployment
(really, a ReplicaSet
), it will not come back after you delete it.
Quickly Generate YAML
You can quickly generate a starting point for resource files with an imperative command + --dry-run=client -o yaml
.
This helped a lot on the CKA exam, because you basically just have kubectl
and vim
, as well as the Kubernetes docs. And there is definitely time pressure.
â Run an echo server
Plugins
Note - install any of these with krew
.
Kubectl has some awesome plugins. For example, I hate typing
so I use the kubectx plugin. And I do
Note - Always unset the context after youâre done using kubectl. Itâs easier than you think to accidentally run commands against prod. Tools like Pulumi and Terraform can use your local kubectl context without you thinking about it.
I also like
- kubens - Same as kubectx, but for namespaces.
- â ksniff - Use tcpdump and Wireshark to start a remote capture on any pod. This is gold for debugging socket hangups.
- kubectl-node-shell - Run a shell on a Kubernetes node. Note that there are a lot of privileged things you wonât be able to do. For that you have to use the plugin to run a privileged container.
- kubecolor - I like having colorized output. I just make the
alias k=kubecolor
in my.zshrc
to use this instead.
Additional Thoughts
- To check user privileges, look into
k auth can-i
. - You can see all CRDs with
k get crds
. - Set the default namespace with
k config set-context --current --namespace=foo
- Sometimes when going between
gcloud
users and different Kubernetes contexts, kubectl will cache the wrong token. It can be helpful to automatically expire all of your users and force kubectl to re-authenticate. You do that like this. (You need yq installed.)
Final Note on kubectl Training
This past spring I passed the Certified Kubernetes Administrator exam.
While we can debate the merits of the certification industry, the CKA can be looked at as a kubectl video game. Itâs a lot of fun. If you donât love exams but want the video game experience, Killer Shell is a great challenge.
Wow! You read the whole thing. People who make it this far sometimes
want to receive emails when I post something new.
I also have an RSS feed.