How to get the current namespace of current context using kubectl
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Kubernetes, an open-source platform for automating deployment, scaling, and operations of application containers, uses namespaces to organize resources. In Kubernetes, a namespace provides a mechanism to isolate groups of resources within a cluster. To streamline operations, it's common to need to know which namespace is currently active in your kubectl context. This article walks you through how to determine the current namespace using kubectl, complete with technical explanations and examples.
Understanding Kubernetes Contexts
Before diving into fetching the current namespace, it’s critical to have a basic understanding of Kubernetes contexts. A Kubernetes context is a set of access parameters (cluster, namespace, user) that's defined in the kubeconfig file. This file is usually located at ~/.kube/config, and it allows you to quickly switch between different Kubernetes clusters and namespaces.
Key Components of a Kubernetes Context
- Cluster: The Kubernetes cluster to which you want to connect.
- User: The credentials or identity you want to use to connect to the cluster.
- Namespace: The Kubernetes namespace that acts as the default for commands.
Fetching the Current Namespace
To determine which namespace you're operating in by default, we primarily rely on the kubectl config command. Here's a step-by-step process to reveal the current namespace.
Command to View Current Namespace
You can view the current namespace set in your context with the following command:
Explanation of the Command:
kubectl config view: This command displays the merged kubeconfig settings.--minify: This flag preserves only the content that would be relevant for the current context's setting.--output 'jsonpath={..namespace}': This outputs the desired configuration element using a specific JSONPath expression— in this case, the namespace.
Examples
Suppose your current context is set with the namespace as development. Upon running the command, you should simply see:
If no namespace is specifically set in the context, the default namespace is default.
Modifying the Current Namespace
If you need to change the current namespace for the current context, you can use:
Replace <new-namespace> with the desired namespace.
Detailed Steps
- Set the desired namespace:
- Use the
set-contextcommand to specify the namespace.
- Verify the change:
- Re-run the
kubectl config viewcommand to ensure the namespace is correctly set:
You should now see production if that was your newly set namespace.
Related Commands and Best Practices
Here are a few related commands and practices that can enhance namespace-related operations:
- List all Contexts and their Namespaces:
This lists all contexts with whether they have explicitly defined namespaces.
- List Available Namespaces:Use this command if you quickly want to see all namespaces in the current cluster:
- Use Namespaces in Commands:Instead of changing the default, you can run commands in a specific namespace with the
-nor--namespaceflag:
Summary Table
| Command | Description |
kubectl config view --minify --output 'jsonpath={..namespace}' | Fetches the current namespace in the current context |
kubectl config set-context --current --namespace=<new-namespace> | Sets a new namespace for the current context |
kubectl config get-contexts | Lists all available contexts and their associated namespaces |
kubectl get namespaces | Lists all namespaces in the current Kubernetes cluster |
kubectl get pods -n <namespace> | Runs a command in a specified namespace without changing the context |
Conclusion
Efficiently managing namespaces and Kubernetes contexts is vital when operating multiple environments or clusters. By leveraging kubectl commands, you can retrieve and modify your current namespace, enabling more organized and effective resource management. Knowing these commands and practices enhances your productivity and accuracy when working with Kubernetes clusters.
Related reading
- How to get the namespace from inside a pod in OpenShift?
- How to get the status of a particular pod or container kubectl get pods using jsonpath
- How to gracefully remove a node from Kubernetes?
- How to handle database migrations with Kubernetes and Skaffold
- How to handle S3 events inside a Kubernetes Cluster?
- How to identify schedulable nodes in Kubernetes
- How to identify the storage space left in a persistent volume claim?
- How to import a generated Kubernetes cluster's namespace in terraform

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.