Using Kubectl

July 10, 2019

Lately I have fallen in love with Kubernetes! Below are some “tips” that have helped me traverse my (k8s) clusters.

Aliases

Working with kubectl can result in some long commands. Here are some aliases which helped lower the word count. This list will be updated in the future.

Dealing with many clusters

kubectl --kubeconfig='PATH TO KUBECONFIG'

  • This alias can be set to an alias like kk where any extra letter can be relative to the specific cluster you need. For example kkp get ns

kubectl -- kubeconfig='PATH TO KUBECONFIG' -n NAMESPACE

  • This alias is helpful when you are also dealing with multiple namespaces. Again for example kkpd get deploy

Tips

Here are some tips I have found in my own workings with kubectl. This list will be updated in the future.

Appending -w to more get commands

When adding -w to kubectl get pods it will watch for any changes on your pods within whichever namespace you are in. The same works with deployments, services and other resources.

Kubectl Get

  • This command is used to show a list of resources within your given namespace. Or with -A flag at the end, it will show you all resources within all namespaces.
  • kubectl get api-resources will show you all the possible resources within your cluster that may exist.

Kubectl Explain

This command will give you documentation pertaining to your resource yamls. For example kubectl explain deployments will give you a list of the all the attributes you can apply to them like metadata , spec and so on.

Kubernetes secret resources need to be base64 encoded

The best way to encode them is to use echo -n "secret" | base64. However if you are using some other fancy shell like zsh you should use the original echo for your machine. In my case it was /bin/echo -n "secret" | base64.

Label Flag

The -l flag will filter resources based on the label you are looking for. For example:

kubectl get deployment -l app=nginx

Will display all deployments with a label app: nginx. Seems trivial but it can be put to good use, for example if you need to watch all logs for pods with a specific label:

kubectl logs pod -l app=nginx -f

Reading Logs

  • If you have multiple pods under the same deployment, you can use their labels to group them and read all of their logs at the same time like so, kubectl logs -l app=nginx -f