Namespace¶
Basics¶
- In Kubernetes, namespaces provides a mechanism for isolating groups of resources within a single cluster.
- Names of resources need to be unique within a namespace, but not across namespaces.
- Namespace-based scoping is applicable only for namespaced objects (e.g. Deployments, Services, etc) and not for cluster-wide objects (e.g. StorageClass, Nodes, PersistentVolumes, etc).
Remember
Namespaces cannot be nested inside one another and each Kubernetes resource can only be in one namespace.
An example of namespace is shown below:
Initial namespaces¶
Kubernetes starts with 4 initial namespaces:
default¶
Kubernetes includes this namespace so that you can start using your new cluster without first creating a namespace.
kube-node-lease¶
This namespace holds Lease objects associated with each node. Node leases allow the kubelet to send heartbeats
so that the control plane can detect node failure.
kube-public¶
This namespace is readable by all clients (including those not authenticated). This namespace is mostly reserved for cluster usage, in case that some resources should be visible and readable publicly throughout the whole cluster. The public aspect of this namespace is only a convention, not a requirement.
kube-system¶
The namespace for objects created by the Kubernetes system.
Namespaces and DNS¶
When you create a Service, it creates a corresponding DNS entry. This entry is of the form <service-name>.<namespace-name>.svc.cluster.local
, which means that if a container only uses <service-name>
, it will resolve to the service which is local to a namespace. This is useful for using the same configuration across multiple namespaces such as Development, Staging and Production. If you want to reach across namespaces, you need to use the fully qualified domain name (FQDN)
.
Commands¶
Set the default namespace¶
kubectl config set-context --current --namespace=<insert-namespace-name-here>
# Validate it
kubectl config view --minify | grep namespace:
See namespaced and non-namespaced resources¶
Most Kubernetes resources (e.g. pods, services, replication controllers, and others) are in some namespaces. However, namespace resources are not themselves in a namespace. And low-level resources, such as nodes
and persistentVolumes
, are not in any namespace.
# In a namespace
kubectl api-resources --namespaced=true
# Not in a namespace
kubectl api-resources --namespaced=false