Kubernetes Namespaces
A practical guide to Kubernetes Namespaces: what they are, how they provide logical isolation, how to create and manage them, and how to use them with workloads, Services, RBAC, quotas, and Helm.
Kubernetes Namespaces provide a way to divide resources inside a cluster into separate logical environments. Instead of placing every Pod, Deployment, Service, ConfigMap, and other namespaced resource into one shared space, you can organize them into namespaces such as development, staging, and production.
Namespaces are especially useful when multiple applications, teams, or environments share the same Kubernetes cluster. They make resource organization easier and can also be combined with RBAC, resource quotas, network policies, and labels to create stronger operational boundaries.
What Is a Kubernetes Namespace?
A Kubernetes Namespace is a logical grouping of Kubernetes resources within a cluster. A namespace does not create a separate cluster or a separate virtual machine environment. Instead, it provides a scope in which many Kubernetes objects can be organized and addressed.
For example, a single cluster could contain namespaces named development, staging, and production. Each namespace could contain its own Deployments, Services, ConfigMaps, Secrets, and Pods.
A resource name only needs to be unique within its namespace for namespaced resources. This means two namespaces can contain resources with the same name.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: developmentThe Deployment above is named web and belongs to the development namespace. Another Deployment with the same name can exist in the staging namespace without conflicting with it.
Why Use Namespaces?
Namespaces are primarily an organizational and administrative mechanism. They allow a cluster to be divided into logical areas without requiring a separate cluster for every application or environment.
- Separate development, staging, and production workloads.
- Organize resources belonging to different applications or teams.
- Apply ResourceQuota and LimitRange policies to groups of resources.
- Control access with namespace-scoped RBAC rules.
- Make kubectl commands target a specific environment.
- Use labels and naming conventions to improve organization.
- Reduce the need to create separate clusters for every small environment.
Namespaces Are Not Complete Security Boundaries
For example, putting two applications into different namespaces does not by itself guarantee that their Pods cannot communicate over the network. Network behavior depends on the networking implementation and applicable NetworkPolicy rules.
Default Kubernetes Namespaces
A Kubernetes cluster normally contains several namespaces created by Kubernetes itself. The exact set can vary by Kubernetes distribution and cluster configuration, but common namespaces include default, kube-system, kube-public, and kube-node-lease.
| Namespace | Typical purpose |
|---|---|
| default | Default namespace used when no namespace is explicitly specified. |
| kube-system | Resources created by Kubernetes system components. |
| kube-public | Namespace intended for resources that should be publicly readable across the cluster. |
| kube-node-lease | Contains Lease objects used for node-related heartbeats. |
You generally should not use kube-system for application workloads. Keeping application resources in dedicated namespaces makes cluster administration and troubleshooting much easier.
The Default Namespace
The default namespace is used when a Kubernetes command or manifest does not specify another namespace. For example, running kubectl get pods without a namespace normally queries Pods in the default namespace.
kubectl get podsThis behavior can be convenient for small experiments, but production workloads are usually easier to manage when they are placed into purpose-specific namespaces.
Creating a Namespace
You can create a namespace directly with kubectl or define it as a Kubernetes manifest.
kubectl create namespace developmentYou can verify that the namespace exists with:
kubectl get namespacesThe shorter kubectl get ns command is also commonly used.
kubectl get nsCreating a Namespace with YAML
Defining a namespace as YAML is useful when your infrastructure is stored in Git and managed declaratively.
apiVersion: v1
kind: Namespace
metadata:
name: developmentApply the manifest with kubectl apply:
kubectl apply -f namespace.yamlThe namespace itself can also have labels and annotations, which can be useful for automation, policy selection, ownership, and environment identification.
apiVersion: v1
kind: Namespace
metadata:
name: production
labels:
environment: production
team: platformListing Namespaces
Use kubectl get namespaces to list namespaces in the current cluster.
kubectl get namespacesFor more detailed information about a namespace, use kubectl describe namespace.
kubectl describe namespace developmentThis can show metadata and information related to resources and quota configuration associated with the namespace.
Deleting a Namespace
A namespace can be deleted with kubectl delete namespace.
kubectl delete namespace developmentPutting Resources into a Namespace
For namespaced resources, the namespace can be specified in the metadata.namespace field.
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: development
data:
APP_ENV: developmentThe same approach can be used for Deployments, Services, Secrets, Jobs, CronJobs, and many other namespaced resources.
Namespace and Resource Names
Namespaced resources are identified within their namespace. Therefore, the combination of namespace and resource name is important when working with objects.
apiVersion: v1
kind: Service
metadata:
name: api
namespace: developmentAnother Service named api can exist in the staging namespace:
apiVersion: v1
kind: Service
metadata:
name: api
namespace: stagingThese are separate resources because they belong to different namespaces.
Checking Resources in a Namespace
Use the -n or --namespace option to target a particular namespace.
kubectl get pods -n developmentThe same option works with many other kubectl commands.
kubectl get deployments -n development
kubectl get services -n development
kubectl get configmaps -n development
kubectl get secrets -n developmentListing Resources Across All Namespaces
The --all-namespaces option allows kubectl to query resources across namespaces. The shorter -A form is commonly used.
kubectl get pods --all-namespaceskubectl get pods -AThis is particularly useful when troubleshooting because a workload may not be located in the namespace you initially expect.
Changing the Current Namespace
Kubernetes contexts can store a default namespace. This can reduce the need to repeatedly type -n development.
kubectl config set-context --current --namespace=developmentAfter this command, kubectl commands that do not explicitly specify a namespace use development for the current context.
Namespaces and Deployments
Deployments are namespaced resources, so a Deployment belongs to a specific namespace. Its Pods are also created in that namespace.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: production
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27A Deployment in production will create its managed Pods in production as well. Namespace consistency is therefore important when combining Deployments with Services, ConfigMaps, and Secrets.
Namespaces and Services
Services are namespaced resources. A Service normally discovers Pods in the same namespace using its selector.
apiVersion: v1
kind: Service
metadata:
name: web
namespace: production
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80Because the Service and selected Pods are normally in the same namespace, moving one component to another namespace without adjusting the configuration can cause connectivity or discovery problems.
DNS and Cross-Namespace Communication
Kubernetes Services have DNS names that include their namespace. This allows workloads in different namespaces to communicate with a specific Service.
api.default.svc
api.development.svc
api.production.svcFor example, a Pod in the development namespace can address a Service named api in the production namespace using the appropriate namespace-qualified DNS name, assuming network policies and application configuration allow that communication.
Namespaces and ConfigMaps
ConfigMaps are namespaced resources. A Pod generally consumes a ConfigMap from its own namespace.
apiVersion: v1
kind: ConfigMap
metadata:
name: web-config
namespace: development
data:
API_URL: https://api.example.comIf the ConfigMap is accidentally created in another namespace, a workload may fail to find it even when the ConfigMap name is correct.
Namespaces and Secrets
Secrets are also namespaced. This is important when separating credentials between environments.
apiVersion: v1
kind: Secret
metadata:
name: database-credentials
namespace: production
type: OpaqueA Secret named database-credentials in production is a different resource from a Secret with the same name in development. Access to Secrets should additionally be controlled with appropriate RBAC permissions.
Namespaces and RBAC
Namespaces work particularly well with Kubernetes Role-Based Access Control. A Role is namespace-scoped and can grant permissions to resources within a specific namespace.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: developer
namespace: development
rules:
- apiGroups: [""]
resources: ["pods", "services"]
verbs: ["get", "list", "watch"]This allows a Role to define permissions for resources in development without automatically granting the same namespace-scoped permissions in production.
A RoleBinding connects the Role to a user, group, or ServiceAccount. ClusterRole and ClusterRoleBinding operate at cluster scope and can therefore provide broader permissions.
Namespaces and ResourceQuota
ResourceQuota can limit the total amount of resources consumed by objects in a namespace. This is useful in shared clusters where one team or environment should not consume unlimited resources.
apiVersion: v1
kind: ResourceQuota
metadata:
name: development-quota
namespace: development
spec:
hard:
requests.cpu: "2"
requests.memory: 4Gi
limits.cpu: "4"
limits.memory: 8Gi
pods: "20"The exact quota configuration should match the cluster's workload requirements. A quota that is too restrictive can prevent legitimate workloads from being scheduled.
Namespaces and LimitRange
LimitRange can define default or minimum and maximum resource constraints for individual containers or other supported resources within a namespace.
apiVersion: v1
kind: LimitRange
metadata:
name: container-limits
namespace: development
spec:
limits:
- type: Container
default:
cpu: "500m"
memory: 512Mi
defaultRequest:
cpu: "100m"
memory: 128MiResourceQuota and LimitRange solve different problems. A quota controls aggregate resource usage in a namespace, while a LimitRange can establish defaults and constraints for individual resources.
Namespaces and Labels
Labels can be applied to namespaces just like many other Kubernetes objects. Namespace labels are useful when another Kubernetes feature selects namespaces based on labels.
kubectl label namespace development environment=developmentYou can inspect namespace labels with:
kubectl get namespace development --show-labelsNamespace labels are commonly useful with policies and automation. They should describe relatively stable properties such as environment, team, or ownership rather than constantly changing runtime state.
Namespaces and NetworkPolicy
NetworkPolicy can use namespace selectors to control traffic between workloads in different namespaces. This makes namespace labels particularly useful for network segmentation.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-from-development
namespace: production
spec:
podSelector: {}
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
environment: developmentThe exact effect of a NetworkPolicy depends on the policies present in the cluster and whether the networking implementation supports NetworkPolicy. Namespace separation alone does not provide this kind of traffic control.
Environment Separation with Namespaces
One common pattern is to use separate namespaces for development, staging, and production.
development
staging
productionEach environment can then contain resources with the same application names. For example, web, api, and database-related resources can exist in all three namespaces.
This approach can simplify deployment workflows because the same Kubernetes resource templates can often be reused with different namespace and configuration values.
Namespaces and Helm
Helm uses Kubernetes namespaces when installing and managing releases. A Helm release is associated with a namespace, and resources generated by a chart are normally deployed into the selected namespace according to the chart configuration.
helm install web ./web-chart --namespace development --create-namespaceThe --create-namespace option tells Helm to create the target namespace if it does not already exist.
This is useful for repeatable environment setup, especially when Helm charts are used to deploy the same application to multiple environments.
Namespaces in Helm Templates
Helm templates can use namespace-related values when resource placement needs to be configurable. A common approach is to let Helm determine the release namespace rather than hardcoding an environment-specific namespace into every template.
metadata:
name: {{ include "web.fullname" . }}
namespace: {{ .Release.Namespace }}In many charts, namespace fields are omitted entirely and Helm installs the resources into the release namespace. The correct approach depends on how the chart is designed and which resources it manages.
Namespaces and Kubernetes YAML
When working with Kubernetes manifests, namespace placement is part of the resource metadata. Keeping namespace configuration consistent across related resources reduces deployment errors.
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
namespace: staging
---
apiVersion: v1
kind: Service
metadata:
name: api
namespace: stagingThe Deployment and Service above are both in staging, allowing the Service to target the Pods managed by the Deployment when their selectors and labels match.
Namespaced vs Cluster-Scoped Resources
Not every Kubernetes resource belongs to a namespace. Kubernetes resources can be namespaced or cluster-scoped.
| Namespaced resources | Cluster-scoped resources |
|---|---|
| Pods | Nodes |
| Deployments | PersistentVolumes |
| Services | Namespaces |
| ConfigMaps | ClusterRoles |
| Secrets | ClusterRoleBindings |
| Jobs | StorageClasses |
A cluster-scoped resource does not have a namespace field. Adding metadata.namespace to such a resource does not make it namespaced.
How to Check Whether a Resource Is Namespaced
kubectl api-resources can show whether a resource is namespaced.
kubectl api-resourcesThe output includes a NAMESPACED column. This is useful when you are unsure whether a resource should contain metadata.namespace.
Common Namespace Mistakes
Many Kubernetes deployment problems are caused by resources being created in different namespaces unintentionally. The YAML can look correct while the objects are simply not in the same scope.
Mistake: Deployment and Service Use Different Namespaces
A Service in development cannot automatically select Pods from an identically named Deployment in production. The Pods selected by a Service must be available within the applicable namespace scope.
# Deployment
metadata:
name: api
namespace: production
# Service
metadata:
name: api
namespace: developmentIf the intention was for the Service to expose the production Pods, the namespace configuration needs to be reconsidered.
Mistake: ConfigMap or Secret Is in the Wrong Namespace
A Pod may reference a ConfigMap or Secret by name and still fail if the referenced object is not available in the expected namespace.
Mistake: Assuming Namespaces Provide Network Isolation
Different namespaces do not automatically mean that workloads are unable to communicate. Network isolation requires appropriate network policies and support from the cluster networking implementation.
Mistake: Using the Default Namespace for Everything
Using default is not inherently incorrect, especially for learning or small experiments. However, placing many unrelated production workloads there can make access control, quotas, troubleshooting, and resource organization harder.
Mistake: Hardcoding Environment Names Everywhere
Hardcoding production, staging, or development into every manifest can make deployments harder to reuse. Templates, Helm values, Kustomize overlays, or deployment-specific configuration can help keep environment differences manageable.
A Practical Namespace Structure
There is no single namespace structure that fits every Kubernetes cluster. The right organization depends on the number of teams, applications, environments, security requirements, and operational workflows.
| Approach | Typical use |
|---|---|
| Namespace per environment | Development, staging, and production separation. |
| Namespace per team | Shared clusters where teams need separate administrative scopes. |
| Namespace per application | Applications that need independent policies and resource management. |
| Namespace per environment and team | Larger organizations requiring more detailed administrative boundaries. |
Creating a separate namespace for every small component is not automatically better. Too many namespaces can increase operational complexity, so namespace boundaries should correspond to meaningful organizational or operational requirements.
Namespace Naming Conventions
Namespace names should be predictable, short, and consistent. Kubernetes resource naming rules apply, so names should use valid DNS-compatible formats.
- Use lowercase names.
- Prefer stable names such as development, staging, and production.
- Avoid unnecessary abbreviations that make namespaces difficult to understand.
- Use a consistent naming convention across clusters.
- Avoid embedding frequently changing values into namespace names.
Namespace Labels and Ownership
Labels can communicate ownership and environment information without changing the namespace name.
apiVersion: v1
kind: Namespace
metadata:
name: production
labels:
environment: production
team: platform
cost-center: platformThis makes the namespace easier to select and classify in automation, policy configuration, and cluster-management tooling.
Namespaces in CI/CD
Namespaces are commonly incorporated into deployment pipelines. A pipeline can deploy the same application to different namespaces depending on the target environment.
kubectl apply -f manifests/ --namespace=stagingFor production deployments, the pipeline can target production explicitly while development deployments use a separate namespace. The exact workflow depends on the deployment tooling and repository structure.
Namespaces and GitOps
In GitOps workflows, namespace definitions and namespace-scoped resources can be stored as declarative configuration in Git. This allows namespace creation, labels, quotas, policies, and workloads to be reviewed and versioned together.
Care should be taken with ownership boundaries. Some cluster-scoped resources may need to be managed separately from namespace-specific application resources.
Namespaces and Resource Organization
A namespace is most useful when it represents a meaningful boundary. For example, if a team needs a separate quota, access policy, or deployment workflow, a namespace can provide the scope for those controls.
If two workloads always share the same operational policies and access rules, splitting them into separate namespaces may add complexity without providing a significant benefit.
Troubleshooting Namespace Problems
When a Kubernetes resource appears to be missing or an application cannot find another resource, check the namespace before investigating more complicated causes.
- Check the namespace of the resource with kubectl get resource -o yaml.
- Use kubectl get pods -A to locate workloads across namespaces.
- Verify that Deployment and Service objects use the expected namespace.
- Check ConfigMap and Secret references.
- Inspect Service selectors and Pod labels.
- Check namespace-scoped RBAC permissions.
- Review ResourceQuota and LimitRange configuration.
- Inspect NetworkPolicy when cross-namespace communication is involved.
kubectl get deployment web -A
kubectl get service web -A
kubectl get pods -AThe -A option is especially useful when you know the resource name but are unsure which namespace contains it.
Namespaces vs Separate Kubernetes Clusters
Namespaces and clusters solve different levels of isolation. A namespace provides logical scope inside a cluster, while a separate cluster provides an independent Kubernetes control plane and cluster environment.
| Namespaces | Separate clusters |
|---|---|
| Share the same cluster control plane. | Have separate cluster control planes. |
| Useful for logical organization. | Provide stronger infrastructure-level separation. |
| Can share cluster resources. | Resources are isolated at the cluster level. |
| Usually simpler to create and manage. | Usually require more infrastructure and administration. |
| Can be combined with RBAC, quotas, and policies. | Provide a broader isolation boundary. |
Some organizations use namespaces for ordinary workload separation and separate clusters when stronger isolation, independent upgrades, compliance requirements, or different infrastructure configurations justify the additional operational cost.
Namespace Best Practices
- Use namespaces to represent meaningful operational or organizational boundaries.
- Avoid treating namespaces as complete security boundaries.
- Use RBAC to control who can access resources in each namespace.
- Use ResourceQuota and LimitRange when resource governance is required.
- Use NetworkPolicy when network isolation between namespaces is required.
- Apply consistent labels to namespaces for environment and ownership metadata.
- Keep production workloads separate from development workloads when operational policies differ.
- Avoid unnecessary namespace proliferation.
- Use declarative YAML or infrastructure management for repeatable environments.
- Check namespaces early when troubleshooting missing or inaccessible resources.
- Make namespace selection explicit in deployment workflows.
- Keep related Deployments, Services, ConfigMaps, and Secrets in the intended namespace.
A Complete Namespace Example
The following example creates a production namespace with labels and then places an application Deployment and Service into that namespace.
apiVersion: v1
kind: Namespace
metadata:
name: production
labels:
environment: production
team: platform
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: production
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: web
namespace: production
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80Apply the complete manifest with kubectl apply -f. Afterward, the namespace, Deployment, Pods, and Service can be inspected independently using the production namespace.
kubectl apply -f production.yaml
kubectl get pods -n production
kubectl get deployment -n production
kubectl get service -n productionUseful kubectl Namespace Commands
| Command | Purpose |
|---|---|
| kubectl get namespaces | List namespaces. |
| kubectl describe namespace NAME | Inspect a namespace. |
| kubectl create namespace NAME | Create a namespace. |
| kubectl delete namespace NAME | Delete a namespace and its namespaced resources. |
| kubectl get pods -n NAME | List Pods in a namespace. |
| kubectl get pods -A | List Pods across namespaces. |
| kubectl config set-context --current --namespace=NAME | Set the current namespace for the active context. |
| kubectl label namespace NAME key=value | Add or update a namespace label. |
Frequently Asked Questions
What is a Kubernetes Namespace?
A Kubernetes Namespace is a logical scope used to organize and manage namespaced resources inside a cluster. It can separate workloads by environment, team, application, or another meaningful operational boundary.
Are Kubernetes Namespaces completely isolated?
No. Namespaces provide logical and administrative separation, but they do not automatically create complete network or security isolation. RBAC, NetworkPolicy, quotas, and other controls may be required.
Can two namespaces have resources with the same name?
Yes. Namespaced resources can have the same name when they belong to different namespaces. For example, a Service named api can exist independently in development and production.
Can a Pod access a Service in another namespace?
It can, provided the cluster networking and policies allow the traffic. Kubernetes Service DNS names include the namespace, which makes cross-namespace Service discovery possible.
What is the difference between a Namespace and a cluster?
A namespace provides logical scope inside a shared cluster. A separate cluster provides an independent Kubernetes cluster environment and a stronger infrastructure-level boundary, but normally requires more operational resources.
Should production use a separate Kubernetes Namespace?
Organizations commonly separate production from development or staging when they need different access rules, quotas, policies, or deployment workflows. Whether a separate namespace is appropriate depends on the cluster's operational requirements.
How do I run kubectl commands in a specific Namespace?
Use the -n or --namespace option, such as kubectl get pods -n production. You can also configure a namespace as the default for the current kubectl context.
Helpful Kubernetes Tools
When working with Kubernetes Namespaces, YAML and configuration tools can make manifests easier to create, inspect, and validate. Kubernetes YAML generators can help build resource definitions, YAML formatters can improve readability, and YAML tree viewers can make nested Kubernetes manifests easier to inspect. Helm values tools are also useful when namespace-specific configuration is managed through Helm charts.
Conclusion
Kubernetes Namespaces provide a practical way to organize resources inside a shared cluster. They are commonly used to separate environments, teams, or applications and provide the scope needed for tools such as RBAC, ResourceQuota, LimitRange, and NetworkPolicy.
Namespaces are not a replacement for a separate cluster or a complete security model. Their value comes from combining logical resource organization with the access, resource, networking, and deployment controls appropriate for the cluster. Once namespace boundaries are planned consistently, Kubernetes workloads become easier to operate, troubleshoot, and manage at scale.