Kubectl tips and tricks

How to cut the first column from a file:

cat <file-name> | cut -f 1 -d ‘:’ > <res-file>

1 Like

Add cURL to NGINX pod:

apt-get update && apt-get install -y curl && apt-get clean
1 Like

List all the pods showing name and namespace with a json path expression

kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'metadata.namespace']}"


I find jmespath syntax a lot easier than json path or even jq. Eliminates a lot of quotes and braces etc. It’s also used by aws cli --query.

Try jp (often expression doesn’t even need quoting, not at my terminal can’t test the following):

kubectl get pods -o=json | jp items[].[metadata.name, metadata.namespace]

You likely have to install jp. See https://jmespath.org/ for details.

1 Like

Here is some useful notes

Some Alias

alias k=‘kubectl’
alias pods=‘k get pods’
alias deployments=‘k get deployments’

Check the kubectl command to create things without using yaml file.
kubectl run nginx --image=nginx (deployment)

kubectl run nginx --image=nginx --restart=Never (Pod)

kubectl run nginx --image=nginx --restart=OnFailure (JOb)

kubectl run nginx --image=nginx --restart=Never --schedule="*/1 * * * *" (CronJob)

kubectl run nginx --image=nginx --restart=Never --port=80 --namespace=mynamespace --command --serviceaccount=mysql --env=HOSTNAME=local --labels=bu=finance,env=dev --requests=‘cpu=100m,memory=265Mi’ --limits=‘cpu=200m,memory=512Mi’ --dry-run -o yaml – /bin/sh -c ‘echo hello world’

kubectl run frontend --replicas=2 --labels=run=load-balancer --image=nginx --port=8080 --dry-run -o yaml

kubectl expose deployment frontend --type=NodePort --name=frontend-service --port=8080 --target-port=8080 --dry-run -o yaml (NodePort auto generate)

kubectl set serviceaccount deployment frontend myuser (make myuser the service account for deployment frontend)

kubectl create service clusterip my-sv --tcp=5678:8008 --dry-run -o yaml

k delete pod pod1 --force --grace-period 0

schedule: ‘*/1 * * * *’
successfulJobsHistoryLimit: 4

1 Like

When you need a pod starter yaml, create an alias like below.

After 1.18:
alias pod=‘kubectl run $name --image=$image --dry-run=client -o yaml > $name_$image_pod.yaml && vi $name_$image_pod.yaml’

Before 1.18:
alias pod=‘kubectl run $name --image=$image --generator=run-pod/v1 --dry-run -o yaml > $name_$image_pod.yaml && vi $name_$image_pod.yaml’

Then just set image and name then type “pod”

Save your time :slight_smile:

1 Like

Hi. I would also add line numbering. So, that would be:
set nu ts=2 sw=2 expandtab ruler

I found also useful to use:

export do="--dry-run=client -o yaml"
export d="--dry-run=client"
export oy="-o yaml"
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
alias k=kubectl
complete -F __start_kubectl k

Additionally, in vim you can indent/unident lines:
5>> to indent five lines
5<< to unindent five lines
3>>. to indent three lines, indent once more
Shift+V then up or down or jj then > to indent marked block

When there are tabs instead of space characters:
:set list - identify tabs,
:retab! - fix tabs

When every pasted line indents:
:set paste - before pasting
:set nopaste - after pasting
Or add use:
:set pastetoggle=<F2> - press i and then F2, paste and after that press F2

On my Windows laptop I have no Insert button, so:
Right Ctrl+fn+E - copy
Right Shift+fn+E - paste

Check connectivity:
wget -O-
curl 80
nc -vz 80


kubectl explain Pods.spec.containers --recursive

kubectl run # without flag creates a deployment
kubectl run — restart=Never #Creates a Pod
kubectl run — restart=OnFailure #Creates a job
kubectl run — restart=OnFailure — schedule=”* * * * *” # Creates a cronjob

Some useful aliases & stuffs

alias k=‘kubectl’
alias kgp=‘kubectl get pods’
alias kgd=‘kubectl get deployment’
alias kgs=‘kubectl get service’

alias kga=‘kubectl get all’
alias kgaa=‘kubectl get all -A’

alias kdp=‘kubectl delete pod --grace-period=0 --force’

alias kcf=‘kubectl create -f’
alias kaf=‘kubectl apply -f’

export drc=’–dry-run=client -o yaml’

Identify your shell

echo $SHELL

source <(kubectl completion bash)
alias k=kubectl
complete -F __start_kubectl k
export do="–dry-run=client -o yaml"
export fg="–force --grace-period=0"
export kc=’‘kubectl config set-context $(kubectl config current-context) --namespace’’
vi ~/.vimrc
set ts=2 sw=2 expandtab ruler
set backspace=indent,eol,start

Here is how we could get only that pod that uses most CPU:
k top pod -n kube-system --sort-by=cpu | head -2 | tail -n 1


Posting in this thread directly because we’ve had to remove posts / ban users after posting in here several times now.

This is not a place for exam notes. You cannot use this on the CKA/CKAD. Posting such content will result in an immediate ban.

Posts containing exam content from the CKA/CKAD will be immediately removed and the information passed to the CNCF with a likely result of having your certificate being revoked.


it was not my intention to use the forum improperly.
I have consulted your forum a lot for the study of k8s. I apologize if I made you work today to remove posts and utilities.
I hope you won’t delete this user account, because it has been very useful to me to study and discover things, and I would like to consult it and vice-versa in an active way.
Good work and good life

1 Like

I suggest also for example pod
Kubect explain pod --recursive | less
To see the structure or
Kubectl explain pod --recursive | grep -I version
Too looking for the value of APIversion for example

We put together this kubectl cheat sheet, thought it might be helpful to learn some initial tips and tricks:

1 Like

I practice a lot in Katacoda (it’s a temporary - 60mins - workspace). That’s why every time I need to download the latest kubectl client and set up .bashrc and .vimrc. Pasting those lines saves time:

curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl $(which kubectl)
echo 'source <(kubectl completion bash)' >> ~/.bashrc
echo 'alias k=kubectl' >> ~/.bashrc
echo 'complete -F __start_kubectl k' >> ~/.bashrc
source .bashrc
echo 'set nu ts=2 sw=2 expandtab ruler' > ~/.vimrc
echo 'set backspace=indent,eol,start' >> ~/.vimrc
source ~/.vimrc
export do="--dry-run=client -oyaml"
export now="--grace-period=0 --force"

I find useful the below lines:
alias k=kubectl
source <(k completion bash)
source <(k completion bash | sed s/kubectl/k/g)

vi ~/.vimrc

autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab ruler incsearch
set number
set ignorecase
set backspace=indent,eol,start
set cursorline
set list

If you explain a little more about these things. It will help a lot to us.

Thank you Aydarsh

I found very useful your set of tricks and tips and he has helped me a lot to define my own set and to figure it out the way I faced the exam.

I am sharing some of my thoughts on them.

First of all, I prefer to deal with a limited set of alias, easy to remember and that saves a lot of time. I also look for the very basic configuration of my editor (line number, tabs as spaces, etc). Choose the ones you think would help and practice with them all the time. I am listing below the ones that were useful for me.

  echo "set tabsize 2" | tee -a ~/.nanorc
  echo "set tabstospaces" | tee -a ~/.nanorc
  echo "set linenumbers" | tee -a ~/.nanorc

  alias k=kubectl
  alias kn='k config set-context --current --namespace'
  alias kcc='k config get-contexts'
  alias kd='k --force --grace-period=0 delete po'
  alias ka='k get all'
  alias kaa='k get all -A'
  alias kcf='k create -f'
  . <(kubectl completion bash)
  complete -F __start_kubectl k
  export do='-o yaml --dry-run=client'
  export KUBE_EDITOR=nano

When time is an important variable to consider, and you are like me --not a born in the terminal guy-- Nano is your editor, here the most recommended shortcuts to practice with.

Remember you can always use F1/^G

Useful Keyboard Shortcuts

Goto, Find, Replace

^_ (M-G) Go to line and column number
^W (F6) Search forward for a string or a regular expression
^\ (M-R) Replace a string or a regular expression

Copy and paste

^K (F9) Cut current line (or marked region) and store it in cutbuffer
^U (F10) Paste the contents of cutbuffer at current cursor position


M-6 (M-^) Copy current line (or marked region) and store it in cutbuffer
M-A (^6) Mark text starting from the cursor position


M-} (Tab) Indent the current line (or marked lines)
M-{ (Sh-Tab) Unindent the current line (or marked lines)

Reading, Writing, Exiting

^X (F2) Close the current buffer / Exit from nano
^O (F3) Write the current buffer (or the marked region) to disk
^R (Ins) Insert another file into current buffer (or into new buffer)

I will just add to the connectivity commands recommended by aydarsh, the ones I think should help to create test pod on-demand or connect quickly to running containers

wget -O-
curl 80
nc -vz 80
k exec -it pod -- [command]
k run bash -it --image=bash --rm -- [command]

My config, if anyone interested:

set nu tabstop=2 shiftwidth=2 expandtab

source <(kubectl completion bash)
alias k=kubectl
complete -F __start_kubectl k
export do="-o yaml --dry-run=client"
export gf="–grace-period=0 --force"
alias kn='kubectl config set-context --current --namespace ’

1 Like