Kubernetes - Namespaces
Kubernetes Namespaces
Namespace
Namespace is used to group resources for multiple teams and projects. Kubernetes provides isolation between different namespaces.
Kubernetes starts with three initial namespaces:
- default The default namespace for objects with no other namespace
- kube-system The namespace for objects created by the Kubernetes system
- 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.
Create a Namespace
namespace-dev.yaml
1 | apiVersion: v1 |
use kubectl apply
to create the namespace
1 | $ kubectl apply -f namespace-dev.yaml |
use kubectl get namespaces
command to get all available namespace
1 | $ kubectl get namespaces |
Create Resource in a namespace
myapp.yaml: Example to create a deployment in dev namespace
1 | apiVersion: apps/v1 |
To list the deployname in dev namespace
1 | $ kubectl get deployment -n dev |
If you don’t want to set the namespace in yaml file, you can set the namespace in the kubectl apply
command
1 | kubectl apply -f myapp -n dev |
To list resources for all namespaces, use --all-namespaces
option
1 | # get all deployments |
Setting Current Namespace
permanently save the namespace for all subsequent kubectl commands in that context.
1 | kubectl config set-context --current --namespace=dev |
use kubectl config get-contexts
to check the current namespace.
1 | $ kubectl config get-contexts |
Delete a Namespace
1 | $ kubectl delete namespace dev |
Resources under namespace dev will be deleted.
kubens
ubens is a utility to quickly switch between Kubernetes namespaces. You can download kubens from https://github.com/ahmetb/kubectx and then add kubens to system PATH.
1 | USAGE: |
Reference