In Kubernetes, a context is a combination of a cluster, a user and a namespace, which defines the operational environment for interacting with a Kubernetes cluster.
The short answer
In Kubernetes, to get concise information about all the existing contexts defined in the [.inline-code] kubeconfig[.inline-code] file, such as their cluster name, user name and namespace, you can use the [.inline-code] kubectl config[.inline-code] command with the [.inline-code] get-contexts[.inline-code] subcommand as follows:
Once executed, this command will produce an output similar to the following one, where the current context is identified by an asterisk ([.inline-code] *[.inline-code] ):
[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering [.inline-code] list contexts in kubeconfig[.inline-code] in the AI Command Suggestions will prompt a [.inline-code] kubectl[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code] CMD+ENTER[.inline-code] .
[#get-current-context-information] Getting information about the current context [#get-current-context-information]
To get information about the current context, you can use the [.inline-code] current-context[.inline-code] subcommand as follows:
[#get-specific-context-information] Getting information about a specific context [#get-specific-context-information]
To get information about a specific context, you can use the [.inline-code] get-contexts[.inline-code] subcommand as follows:
Where:
- [.inline-code] context_name[.inline-code] is the name of the context.
For example, the following command will display the information related to the context named [.inline-code] staging-context[.inline-code] :
If you want to learn more about modifying existing contexts in Kubernetes, you can read our other article on how to set a context with kubectl.
[#get-comprehensive-context-information] Getting comprehensive context overview [#get-comprehensive-context-information]
To output a comprehensive overview of the [.inline-code] kubeconfig[.inline-code] file settings, including information about clusters, users, API servers and more, you can use the [.inline-code] view[.inline-code] subcommand as follows:
Note that, by default, this command will output information in the YAML format.
To output it in the JSON format instead, you can use the [.inline-code] -o[.inline-code] flag (short for [.inline-code] --output[.inline-code] ) as follows:
[#get-current-context-details] Viewing the details of the current context [#get-current-context-details]
To only output the detailed overview of the current context, you can use the [.inline-code] --minify[.inline-code] flag as follows:
[#get-specific-context-details] Viewing the details of a specific context [#get-specific-context-details]
To only output the detailed overview of a specific context, you can combine the [.inline-code] --minify[.inline-code] flag with the [.inline-code] --context[.inline-code] flag as follows:
[#retrieve-context-properties] Retrieving context properties [#retrieve-context-properties]
To retrieve a particular property from a specific context, you can use the [.inline-code] -o jsonpath[.inline-code] flag combined with a JSONPath expression as follows:
Where:
- [.inline-code] property[.inline-code] is the name of the property you want to retrieve (e.g., [.inline-code] cluster[.inline-code] , [.inline-code] user[.inline-code] , [.inline-code] namespace[.inline-code] , etc).
[#retrieve-a-context-namespace] Retrieving the namespace of the current context [#retrieve-a-context-namespace]
To retrieve the namespace of the current context, you can use the aforementioned command with the following JSONPath expression:
[#retrieve-all-contexts-namespaces] Retrieving the namespaces of all contexts [#retrieve-all-contexts-namespaces]
To retrieve the namespaces of all the contexts defined in [.inline-code] kubeconfig[.inline-code] file in the form of a list, you can use the aforementioned command with the following JSONPath expression:
Alternatively, to also retrieve the name of the context each namespace is associated to, you can use the following [.inline-code] for[.inline-code] loop instead:
Where:
- The [.inline-code] for[.inline-code] loop iterates over the list of context names returned by the [.inline-code] kubectl config get-contexts -o name[.inline-code] command.
- Retrieves the namespace related to that context using the [.inline-code] kubectl config view -o jsonpath="{.contexts[?(@.name == \"${context}\")].context.namespace}"[.inline-code] command.
- Outputs the context name and namespace using the [.inline-code] echo[.inline-code] command.
[#get-a-context-resources-information] Getting information about the resources of a specific context [#get-a-context-resources-information]
To get information on a resource type related to a specific context, you can use the [.inline-code] kubectl get[.inline-code] command with the [.inline-code] --context[.inline-code] flag as follows:
Where:
- [.inline-code] resource_type[.inline-code] is the name of the resource type (e.g., [.inline-code] pods[.inline-code] , [.inline-code] nodes[.inline-code] , [.inline-code] services[.inline-code] , etc).
- [.inline-code] context_name[.inline-code] is the name of the context.
For example, the following command will output information about the Services related to the context named [.inline-code] staging-context[.inline-code] :