Kubectl tips and tricks

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.

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:

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


Search and replace in vi

Use :s/foo/bar/g to replace all occurrences of the word foo on the current line with the word bar.
Use :%s/foo/bar/g to replace all occurrences of the word foo in the current file with the word bar.