Using Kubectl

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'

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

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

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