Kubectl tips and tricks

Looks like support for CRDs is being worked on at the moment, https://github.com/kubernetes/kube-openapi/issues/97

A useful command in order to delete all non-running pods:

$ kubectl delete po $(kubectl get po --no-headers | grep -v Running | awk '{print $1}')

Hi @scraly, you can delete the non-running pods with the following command without using grep and awk. as kubectl provide this functionality natively

kubectl delete $(kubectl get pod --no-headers --field-selector=status.phase!=Running -o name )


This is what I would use for vi configuration:
vi ~/.vimrc

set ts=2 sw=2 expandtab ruler
set backspace=indent,eol,start
1 Like

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


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

1 Like

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.

1 Like


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"
1 Like