How to create a Kubernetes namespace


Scenario

You have a functioning Kubernetes cluster and want to deploy pods or replication controllers in a separate namespace (other than default).


The first thing you need to do is create a yaml file defining Namespace as the "kind" parameter.

Below is an example yaml file (foobar.namespace.yaml) with a namespace of foobar.

# cat foobar.namespace.yaml
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "foobar",
    "labels": {
      "name": "foobar"
    }
  }
}

Command

Run the kubectl command to create the namespace.

# kubectl create -f foobar.namespace.yaml
namespaces/foobar

Verify the namespace was created:

# kubectl get namespaces
NAME              LABELS                 STATUS
default           <none>                 Active
foobar            name=foobar            Active

To delete the namespace:

# kubectl delete namespace foobar
namespaces/foobar

For additional information on Kubernetes namespaces, vist http://kubernetes.io/docs/user-guide/namespaces.