Namespace
A namespace is a mechanism to isolate a group of resources in kubernetes. It defines resource quotas, policies etc that apply on the pods and other resources being created in that namespace.
Kubernetes has its own namespace for its internal services. We can create custom namespaces for our applications.
DNS
Services in a same namespace can be accessed using their names. For example, service redis
can be accessed by its name. However, services in different namespaces require namespace to be appended with service name. For example,
We have two namespaces default
and dev
. There is a service in dev
named mysql-db
. To access it in a pod running in default
namespace, we require to use mysql-db.dev.svc.cluster.local
as service name.
Where mysql-db
is service name. dev
is namespace. svc
for service and cluster.local
is the domain used by kubernetes.
Create namespace
Namespace can be created using resource yaml file or just a command.
apiVersion: v1
kind: Namespace
metadata:
name: dev
Or using command kubectl create namespace dev
.
Change Default Namespace
We need to set namespace in the kubernetes context.
We can use following command to do so.
kubectl config set-context <context> --namespace dev
We can get context using command kubectl config current-context
.