Kubernetes Namespace
Published in
2 min readOct 23, 2018
Kubernetes supports multiple virtual clusters backed by the same physical cluster. These virtual clusters are called namespaces.
Namespace helps in managing cluster by providing the cap on:
- resources (pods, services, replication controllers, etc.)
- policies (who can or cannot perform actions)
- constraints (allowed quota, etc.)
Default namespaces
Kubernetes starts with three initial namespaces:
- default: Objects with no namespace are added to default.
- kube-system: The namespace for objects created by the Kubernetes system.
- kube-public: The namespace is created automatically and readable by all users (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.
Not all objects are in a namespace
- Namespace resources are not themselves in a namespace.
- Low-level resources such as nodes and persistent volumes are not in any namespace.
- Events are an exception: they may or may not have a namespace, depending on the object the event is about.
Create namespace:
demo-namespace.ymlapiVersion: v1
kind: Namespace
metadata:
name: demo
labels:
env: dev$ kubectl create -f demo-namespace.yml
$ kubectl get namespaces -o=json
$ kubectl get namespaces demo -o=json/yaml/name
Using namespace:
Get an object from namespace:
$ kubectl namespace=<namespace> get pods
Setting default namespace:
$ kubectl config set-context $(kubectl config current-context) — namespace=<name>$ kubectl config view | grep namespace: