Kubectl tips and tricks


#43

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


#44

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


#45

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


#46

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!


#47

Port scan a service using nmap:

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

#48

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.


#49

#50

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.


#51

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
VERSION:  v1

FIELD:    type <string>

DESCRIPTION:
     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:
     https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services---service-types

#52

True, this command is very, very useful