visit
If you're working with a local Kubernetes instance and one or more cloud instances of Kubernetes, you will at some point need to switch between contexts. Kubernetes CLI (
kubectl
) has commands available that allow you to work with different contexts:kubectl use-context [CONTEXT-NAME]
In case you are working with multiple clusters and you don't know the context names, you would need to list the contexts first, and then run the use-context command like this:$ 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
Luckily, there's an easier way to do this. I am using a tool called that allows you to list and switch to different Kubernetes context quickly. To list the context, you can run
kubectx
like this:$ kubectx
docker-desktop
minikub
cloudc
$ kubectx [CONTEXT-NAME]
Another quite common thing you do when working with Kubernetes is to work with resources from multiple namespaces. For example, you might want to list pods in one namespace, check on services in another etc. My workflow here is to use the
--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.This default value can be changed in the kubeconfig file - you could, for example, set the default namespace to be test or kube-system or any other namespace. That way you don't need to use
--namespace
flag when querying for resources. However, the command to change this is awkward:$ kubectl config set contexts.my-context.namespace my-namespace
The command above modifies namespace field in the
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. Together with the
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".
This is a straightforward tip. If you are working with Kubernetes you will be typing
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.The tip here is to alias
kubectl
to something shorter, k
for example: $ alias k=kubectl
$ k get po
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 18 43h
Ideally, you would put the alias
k=kubectl
in your bash_profile
, so it gets set each time you open your terminal. However, sometimes you want to run plain curl command without exposing any services or forwarding ports. To do so, I use a function that gets loaded as part of my bash profile that runs a pod with radial/busyboxplus:curl image inside the cluster and gives me access to the terminal. That way, I can run curl against services and IPs inside the cluster. I call the function
kbash
and use it like this:$ kbash
If you don't see a command prompt, try pressing enter.
[ root@curl:/ ]$
From the prompt, I can run curl against internal Kubernetes DNS names or IP addresses. To exit, just type
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 as well.$ kubectl get pods --namespace istio-system -l "app=grafana" -o jsonpath="{.items[0].metadata.name}"
grafana-6fb9f8c5c7-hrcqp
$ kubectl --namespace istio-system port-forward grafana-6fb9f8c5c7-hrcqp 3000:3000
$ open //localhost:3000
#!/bin/bash
export GRAFANA_POD=$(kubectl get pods --namespace istio-system -l "app=grafana" -o jsonpath="{.items[0].metadata.name}")
export JAEGER_POD=$(kubectl get pod -n istio-system -l app=jaeger -o jsonpath='{.items[0].metadata.name}')
export KIALI_POD=$(kubectl -n istio-system get pod -l app=kiali -o jsonpath='{.items[0].metadata.name}')
alias grafana="kubectl --namespace istio-system port-forward $GRAFANA_POD 3000:3000 & open //localhost:3000"
alias jaeger="kubectl --namespace istio-system port-forward $JAEGER_POD 16686:16686 & open //localhost:16686"
alias kiali="kubectl --namespace istio-system port-forward $KIALI_POD 20001:20001 & open //localhost:20001"
Now if I want to open Jaeger, I can run
jaeger
and it will get the pod name, create the port-forward and open the browser.If you have other services running inside the cluster you are frequently opening, you can set up the aliases the same way.