Kubectl tips and tricks

I don’t see autocompletion for fish. Do you know if it is a good idea to use fish with kubectl ?

A very usefull tool to get logs in realtime and in color.


Viewing Resource Utilization

Show resource utilization per node:

kubectl top node

Show resource utilization per pod:

kubectl top pod

Now if you want to have a terminal show the output of these commands every 2 seconds without having to run the command over and over you can use the watch command such as:

watch kubectl top node

1 Like

Retrieving Pod Name

Often times you need to get the name of a pod. We can easily retrieve this and store it into a shell variable. In this case we’ll retrieve a pod by it’s label:

kubectl get pods --all-namespaces -lapp=my-awesome-app -o jsonpath='{.items[0].metadata.name}

Now you can take the output of this command and store it in a variable like:

MY_POD=$(kubectl get pods --all-namespaces -lapp=my-awesome-app -o jsonpath='{.items[0].metadata.name})

So later you can run commands like:

kubectl logs -f $MY_POD
kubectl exec -it $MY_POD sh

…and so on!


Port scan a service using nmap:

$ kubectl run --image=mateothegreat/docker-alpine-nmap \
              --rm -i -t nm -- \
              -Pn -p9200,9300 elasticsearch

Does anyone know how do I get the current namespace for my plugin in kubectl 1.12? They changed a bit in kubectl this new release, and my custom plugin is now broken in some use cases.

1 Like
1 Like

Some aliases I use
export KUBECONFIG_PRODUCTION=$HOME/.kube/config.production
export KUBECONFIG_DEVELOPMENT=$HOME/.kube/config.development

alias kkprod=‘kubectl --kubeconfig $KUBECONFIG_PRODUCTION’
alias kkdev=‘kubectl --kubeconfig $KUBECONFIG_DEVELOPMENT --namespace=development’

and in same way for RC, UAT, minikube, GCP, AWS and so on, so one could easily manage access to multiple clusters.


I’m a big fan of using kubectl explain <object> for getting information about k8s objects. This is very helpful when dealing with objects you don’t use often and forget certain properties.

For example

⇒  kubectl explain service.spec.type
KIND:     Service

FIELD:    type <string>

     type determines how the Service is exposed. Defaults to ClusterIP. Valid
     options are ExternalName, ClusterIP, NodePort, and LoadBalancer.
     "ExternalName" maps to the specified externalName. "ClusterIP" allocates a
     cluster-internal IP address for load-balancing to endpoints. Endpoints are
     determined by the selector or if that is not specified, by manual
     construction of an Endpoints object. If clusterIP is "None", no virtual IP
     is allocated and the endpoints are published as a set of endpoints rather
     than a stable IP. "NodePort" builds on ClusterIP and allocates a port on
     every node which routes to the clusterIP. "LoadBalancer" builds on NodePort
     and creates an external load-balancer (if supported in the current cloud)
     which routes to the clusterIP. More info:

True, this command is very, very useful

I usually source this :

source <(kubectl completion bash)
alias kk=kubectl
complete -o default -F __start_kubectl kk
alias grem='grep -i -A 10'
echo 'set number' >> ~/.vimrc
1 Like

How can I make this explain work with a CRD?

kubectl explain Foo
error: Couldn't find resource for "tools.example.com/v1beta1, Kind=Foo"

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

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