How to get the current namespace of current context using kubectl?

4/25/2019

I am trying to get the namespace of the currently used Kubernetes context using kubectl.

I know there is a command kubectl config get-contexts but I see that it cannot output in json/yaml. The only script I've come with is this:

kubectl config get-contexts --no-headers | grep '*' | grep -Eo '\S+
#x27;
-- Mikhail Golubtsov
kubernetes

2 Answers

4/25/2019

This works fine if you have a namespace selected in your context

kubectl config view --minify --output 'jsonpath={..namespace}'

You can always show up your current context and namespace in your prompt with kube-ps1

-- Jose Armesto
Source: StackOverflow

10/31/2019

Easy to memorize command line

kubectl config view | grep namespace
-- Kim G.
Source: StackOverflow