
Every day Kubernetes
In this article I will post kubectl commands and Kubernetes tricks I use as part of my daily routine. I don't remember things by heart, but I am also sick and tired of googling each time. So here is the list of absolute must-know CLI commands that is used to operate a cluster on daily basis.
Aliases
The kubectl CLI command name is objectively challenging to spell. The letter combination is not handy and can't be easily typed in. So, I define an alias:
Contexts
The next thing I may want to do is to see the list of contexts. Each context represents a separate cluster. To see the list of contexts:
k config get-contexts
To switch to a specific context:
k config use-context <CONTEXT_NAME>
To delete a context no longer needed:
k config delete-context <CONTEXT_NAME>
If find it useful to add some aliasing for the first two commands:
alias kctx="k config get-contexts"alias kuctx="k config use-context"
Namespaces
To see the list of all namespaces on a cluster:
k get namespace
As most of the time I work with just one application, it makes sense narrow the scope to just one particular namespace:
alias kn="k -n <NAMESPACE_OF_MY_APP>"
Seeing a list of pods
To see the list of pods, there is a command:
kn get pod
Getting info about a specific pod
There is a way to see the detailed information about a pod: labels, a list of containers, etc.
kn get pods <POD_NAME> -o json
Restarting a pod
Sometimes I want a pod restarted, in case if it misbehaves. I scale it down to zero and then up again.
kn scale deploy <POD_NAME> --replicas=0kn scale deploy <POD_NAME> --replicas=<ORIGINAL_NUM_OF_REPLICAS>
Reading container's logs
To see the logs there is a command:
kn logs -f <POD_NAME> -c <CONTAINER_NAME>
Exec into a container
Exec into a container may be last resort. But, anyway, there is how it is done:
kn exec -it <POD_NAME> -c <CONTAINER_NAME> -- sh
Most of the time I use it when I need to check the env vars with printenv.
Getting info about jobs
If I have a regular job, or a cron job, I may want to see how it is doing. Assuming that I have the jq tool installed, I can then type:
kn get job -l <LABEL_NAME>=<LABEL_VALUE> -o json | jq -r '.items[] | select(.status.succeeded) | .status.completionTime' | sort -r | head -n 1
The -l command is a selector by label. It accepts a label name and label value to filter the list of jobs against.
Applying the aliases
Don't forget to apply those:
source ~/.bash_profile
As usual, this is a work in progress article. I will add more commands later as soon as I have them!

Sergei Gannochenko
React, Node, Go, Docker, AWS, Jamstack.
15+ years in dev.