-
Namespace are for logical division of resources in kubernetes.
-
Get all namespace:
kubectl get namespaces
okubectl get namespaces --show-labels
- To create namespace:
-
Create json file
namespace-dev
with content:{ "kind": "Namespace", "apiVersion": "v1", "metadata": { "name": "dev", "labels": { "name": "dev" } } }
-
Execute this
kubectl create -f namespace-dev.json
-
Similary for creating namespace for prod-env, create
namespace-prod.json
with content{ "kind": "Namespace", "apiVersion": "v1", "metadata": { "name": "prod", "labels": { "name": "prod" } } }
-
Execute this
kubectl create -f namespace-prod.json
-
To execute command in specific namespace, we create a context and when to switch to that context, our kubectl command would create/update/delete objects in that namespace.
-
Creating the context to work in namespace, setting context to view
- kubectl config set-context dev --namespace=dev
- kubectl config set-context prod --namespace=prod
-
To switch the context:
kubectl config use-context dev
kubectl config use-context prod
-
View all the context:
kubectl config view | grep namespace
- or
kubectl config list-contexts
-
View current context:
kubectl config current-context
-
Delete a namespace
kubectl delete namespaces dev
Ref: