75

How do I view all the contexts I have set up with kubectl?

It doesn't appear there's a resource known to kubectl as 'contexts'

$ kubectl get contexts
error: the server doesn't have a resource type "contexts"
arshbot
  • 12,535
  • 14
  • 48
  • 71
  • 3
    I would recommend you to add a favorite in your browser to the official Cheat sheet Doc: https://kubernetes.io/docs/reference/kubectl/cheatsheet/#kubectl-context-and-configuration You will find even more good command to use. – Nordes Jan 06 '20 at 03:51

4 Answers4

162

You can view all contexts via the config command

$ kubectl config get-contexts
arshbot
  • 12,535
  • 14
  • 48
  • 71
7

For the kubectl command, use this command to list all the contexts:

kubectl config get-contexts

You can view the default config file that locates at ~/.kube/config, it contains more info like clusters, users, ...

Tai Vu
  • 101
  • 1
  • 3
3

If you are a ZSH user and have ohmyzsh and kubectl plugin enabled, then issue

kcgc command!

2

In addition to you answer, you can use kubectx tool to easily switch between clusters and namespaces.