Kubernetes has a vast number of resources, like deployments, replicasets, services, etc. Namespaces are also one of them and are most useful when you want to isolate your workloads based on environments, teams, projects, etc.
In simple language, namespaces are a kind of isolated environment in which you group resources like pods, replica sets, deployments, services, configmaps, etc.
When you create a Kubernetes resource without specifying a namespace, it will be created in the default namespace.
You can change it by applying the --namespace=development
flag to every resource creation.
Also, if you don't want to use the above flag on every resource creation, you can switch from the default namespace to any random namespace by setting your current context to that random namespace using the kubectl config set-context --current --namespace=random
command and creating your resources there.
Let's see some action on namespaces:
- Check how many namespaces are on the cluster:
$ kubectl get namespaces
>>
NAME STATUS AGE
kube-system Active 48m
kube-public Active 48m
kube-node-lease Active 48m
default Active 48m
finance Active 34m
marketing Active 34m
dev Active 34m
prod Active 34m
manufacturing Active 34m
research Active 34m
You can also use shorthand ns
instead of the full resource name.
- Check the pods running in a specific namespace.
$ kubectl get pods --namespace=research
>>
NAME READY STATUS RESTARTS AGE
dna-2 0/1 CrashLoopBackOff 12 (4m28s ago) 41m
dna-1 0/1 CrashLoopBackOff 12 (4m9s ago) 41m
- Run a specific pod in a specific namespace.
$ kubectl run redis --image=redis --namespace=finance
>>
pod/redis created
If you want to create a pod using the YAML definition file, you can define the namespace name in the metadata section.
apiVersion: v1
kind: Pod
metadata:
name: my-pod
namespace: my-namespace
spec:
containers:
- name: nginx
image: nginx
- does one pod running in namespace A access the other pod running in namespace B?
pods can communicate with other pods residing in different namespaces using their Fully Qualified Domain Names (FQDNs), like pod-a can talk with pod-b using it's FQDN, pod-b.namespace.svc.cluster.local
. The same works for pod-b as well.
Summary
Kubernetes namespaces provide a powerful tool for organising and isolating workloads within a cluster.We will see more about namespaces in advance in upcoming blogs.
Top comments (0)