5 Tips to Be More Productive with Kubernetes
I like to read and see how people set up their environments and any tools, tips, and tricks they use to be more productive when working with Kuberentes and Istio. What follows is a collection of 5 tips and tools that I use daily and I think it makes me be more productive with Kuberentes and Istio.
1. Switching between Kubernetes contexts
kubectl
) has commands available that allow you to work with different contexts:- current-context
- get-contexts
- rename-context
- delete-context
- set-context – use-context
kubectl use-context [CONTEXT-NAME]
$ kubectl get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* docker-desktop docker-desktop docker-desktop
minikube minikube minikube
cloudc crdambvg43d user-crdambvg43d
$ kubectl use-context minikube
kubectx
like this:$ kubectx
docker-desktop
minikube
cloudc
$ kubectx [CONTEXT-NAME]
2. Switching between Kubernetes namespaces
--namespace
flag that is supported on the Kubernetes CLI. For example, to get all pods in the namespace called test , you can run kubectl get pods -n test
. By default, if you don't provide the namespace flag, the default Kubernetes namespace is used - which is appropriately named default.--namespace
flag when querying for resources. However, the command to change this is awkward:$ kubectl config set contexts.my-context.namespace my-namespace
my-context
context and changes it to my-namespace
. This means if you switch to my-context
and run kubectl get pods
for example, you would only see pods from the my-namespace
namespace.kubectx
tool, you also get a tool called kubens
- it helps you list and switch to different namespaces.$ kubens
default
docker
kube-node-lease
kube-public
kube-system
$ kubens default
Context "docker-desktop" modified.
Active namespace is "default".
3. Alias the Kubernetes CLI
kubectl
a lot, and you will get tired of typing the whole name at some point. You might be thinking it's only seven characters, but it adds up.kubectl
to something shorter, k
for example:$ alias k=kubectl
$ k get po
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 18 43h
k=kubectl
in your bash_profile
, so it gets set each time you open your terminal.4. Get a terminal inside the Kubernetes cluster
kbash
and use it like this:$ kbash
If you don't see a command prompt, try pressing enter.
[ root@curl:/ ]$
exit
and if you want to attach back to the pod, run kbash
and it will attach to the existing pod. I have this function defined as part of my dotfiles as well.5. Quickly open Grafana/Jaeger/Kiali (or anything else)
kubectl
, accessing these services required you to get the pod name first and then set up a port forward to that pod and finally open the browser to the forwarded address. With the latest kubectl
versions you can port forward to the service itself and you don't need to figure out what the pod name is anymore. Regardless, the commands are quite long to type out each time:$ kubectl --namespace istio-system port-forward svc/grafana 3000:3000
$ open http://localhost:3000
#!/bin/bash
alias grafana="kubectl --namespace istio-system port-forward svc/grafana 3000:3000 & open http://localhost:3000"
alias jaeger="kubectl --namespace istio-system port-forward svc/jaeger 16686:16686 & open http://localhost:16686"
alias kiali="kubectl --namespace istio-system port-forward svc/kiali 20001:20001 & open http://localhost:20001"
jaeger
and it will set up the port-forward and open the browser.