Kubernetes Namespaces
What is namespace?
Kubernetes supports multiple virtual clusters backed by the same physical cluster. These
virtual clusters are called namespaces.
Namespaces are intended for use in environments with many users spread across multiple
teams, or projects.
For clusters with a few to tens of users, you should not need to create or think about
namespaces at all.
Namespaces provide a scope for names. Names of resources need to be unique within a
namespace, but not across namespaces.
Using a Kubernetes namespace could isolate namespaces for different environments (dev,
staging, preprod, prod) in the same cluster.
Viewing namespaces
$ kubectl get namespace
NAME STATUS AGE
default Active 1d
kube-system Active 1d
kube-public Active 1d
Below image from my cluster
Kubernetes starts with three initial namespaces:
default The default namespace resource you create are located here.
kube-system : Do not create or modify in kube-system.System process,
master and kubectl process
kube-public This namespace is created automatically and is 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.
Kube-node-relase: Heart beats nodes , determines the availability of Nodes.
The name of a namespace must be a DNS label and follow the following rules:
At most 63 characters
Matching regex [a-z0-9]([-a-z0-9]*[a-z0-9])
Creating Namespace
Type1 :
Create namespace trough command line
$ kubectl create namespace test
Type2 :
Create namespace trough yaml file
$vi newNamespace.yml
apiVersion: v1
kind: Namespace
metadata:
name: new-namespace
$ kubectl apply -f newNamespace.yml
After the namespace is created successfully, list the namespace again:
Type 1 applying namespace while deploying
Let's run the tutum replication controller , Building Your Own Kubernetes in a new
namespace:
$ kubectl run tutum --image=tutum/hello-world --namespace=new-namespace
$ kubectl get pods --namespace=new-namespace
$ sudo kubectl get pods --namespace=default
$ kubectl get pods --all-namespaces
This deployment will create one pod , replicaset and deployment object
Type 2:
You can specify the namespace in yaml file
$vi mytutum.yml
apiVersion: v1
kind: Pod
metadata:
name: mytutum
labels:
zone: prod
version: v1
namespace: test
spec:
containers:
- name: mytutumapp
image: tutum/hello-world
ports:
- containerPort: 80
$ kubectl get pods --namespace=test
By default, if you don't specify any namespace in the command line,
Kubernetes will create the resources in the default namespace. If you want to
create resources by configuration file, just simply specify it when doing
kubectl create:
# kubectl create -f myResource.yaml --namespace=new-namespace
Changing the default namespace
It is possible to switch the default namespace in Kubernetes:
1. Find your current context:
$ kubectl config view | grep current-context
2. No matter whether there is current context or not, using set-context could
create a new one or overwrite the existing one.
$ kubectl config set-context --current --namespace=new-namespace
Deleting a namespace
1. Using kubectl delete could delete the resources including the namespace.
Deleting a namespace will erase all the resources under that namespace:
$ kubectl delete namespaces new-namespace
2. After the namespace is deleted, our tutum pod is gone:
3. However, the default namespace in the context is still set as new-namespace:
Will it be a problem? Yes because we deleted new-namespace we deleted
4. Let's run an nginx replication controller again.
$ kubectl run tutum --image=tutum/hello-world
It will try to create an tutum replication controller and replica pod in the
current namespace we just deleted. Kubernetes will throw out an error if the
namespace is not found.
5. Let's switch back to the default namespace.
$ kubectl config set-context --current --namespace=""
Lets run tutum application now it will deploy on default namespace