Avatar

Every day Kubernetes

← Back to list
Posted on 13.07.2023
Image by AI on Midjourney
Refill!

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:

👉 📃  ~/.bash_profile
$
alias k="kubectl"
The code is licensed under the MIT license

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
The code is licensed under the MIT license

To switch to a specific context:

$
k config use-context <CONTEXT_NAME>
The code is licensed under the MIT license

To delete a context no longer needed:

$
k config delete-context <CONTEXT_NAME>
The code is licensed under the MIT license

If find it useful to add some aliasing for the first two commands:

👉 📃  ~/.bash_profile
$
alias kctx="k config get-contexts"
alias kuctx="k config use-context"
The code is licensed under the MIT license

Namespaces

To see the list of all namespaces on a cluster:

$
k get namespace
The code is licensed under the MIT license

As most of the time I work with just one application, it makes sense narrow the scope to just one particular namespace:

👉 📃  ~/.bash_profile
$
alias kn="k -n <NAMESPACE_OF_MY_APP>"
The code is licensed under the MIT license

Seeing a list of pods

To see the list of pods, there is a command:

$
kn get pod
The code is licensed under the MIT license

To see the list of pods and containers running inside each of them, here is a snippet:

$
kn get pods -o jsonpath='{range .items[]}{"\n"}{.metadata.name}{"\t"}{range .spec.containers[]}{.name}{"=>"}{.image}{"\t"}{end}{end}'|sort|column -t
The code is licensed under the MIT license

To get a list of containers and images they are spinning, here is another snippet:

$
kn get pods -o json | jq '.items[] | { "name": .metadata.labels.name, "country": .metadata.labels.country, "image": .status.containerStatuses[].imageID }'
The code is licensed under the MIT license

Note, that I select imageID, not image, because the sha digest is much more valuable than just an image name + a tag.

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
The code is licensed under the MIT license

Getting a list of containers in a pod

$
kn get pods <POD_NAME> -o jsonpath='{.spec.containers[*].name}'
The code is licensed under the MIT license

Reading container's logs

To see the logs there is a command:

$
kn logs -f <POD_NAME> -c <CONTAINER_NAME>
The code is licensed under the MIT license

If you don't specify the -c parameter, it will print logs of the first container in the pod (this is probably not what you want, so get the list of containers on the pod first).

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
The code is licensed under the MIT license

Most of the time I use it when I need to check the env vars with printenv.

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=0
kn scale deploy <POD_NAME> --replicas=<ORIGINAL_NUM_OF_REPLICAS>
The code is licensed under the MIT license

Getting K8s events

If a deployment is reported to be scaled up, but nothing shows up, it's a good case for reading the event log of the K8s itself:

$
kn get events
The code is licensed under the MIT license

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 code is licensed under the MIT license

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
The code is licensed under the MIT license

As usual, this is a work in progress article. I will add more commands later as soon as I have them!


Avatar

Sergei Gannochenko

Business-oriented fullstack engineer, in ❤️ with Tech.
Golang, React, TypeScript, Docker, AWS, Jamstack.
15+ years in dev.