This is going to be a quick post – but I wanted to put it here for my own reference, since it’s something I have to look up pretty often. I might as well make my notes about it public so that others can benefit, too.
What are ‘Contexts’?
In Kubernetes, a Context is essentially the configuration that you use to access a particular cluster & namespace with a user account. In most cases, this will be your user account, but it could also be a service account.
In my particular case, there are at least a few Kubernetes clusters that I need to access pretty regularly. We have one in our data center and two or three different clusters (depending on the day) configured in GCP to work on our migration there. When I need to work in one cluster, I need to remember how to activate the context that grants me access to that cluster.
List Your Kubernetes Contexts
kubectl config view -o jsonpath='{.contexts[*].name}' | tr " " "\n"
This will show all your configured contexts in Kubernetes. I included the | tr ...
to replace the spaces with newlines so that it’s easier to parse the results. This way, you can easily see the exact names of your contexts, so that you can easily switch between them.
Show your Current Context
kubectl config current-context
This just shows your current context. It’s pretty self-explanatory, but I often forget the exact syntax that lists my context.
Set your Context
kubectl config use-context <context_name>
And this, not surprisingly, sets your context. So if you need to switch from your minikube
context to your gcp-project-cluster-context
, you just use this nifty command, and suddenly your commands are pointing at an entirely different cluster.
Leave a Reply