Kubernetes Namespaces
After reading this post you will be understanding the high level of Kubernetes namespace and basic kubectl commands related to namespaces.
What is namespace 🤔
In Kubernetes, namespaces provide a mechanism for isolating groups of resources within a single cluster. Names of resources need to be unique within a namespace, but not across namespaces.
Whenever you create a Kubernetes object without specifying a namespace, the object is placed in the Default namespace.
When you set up the cluster for the first time, Kubernetes will deploy the pod & services for the internal management these pods are created under the Kube-system namespaces by default. The purpose of the isolate is to prevent you from accidentally deleting or modifying these services.
kube-node-lease This namespace holds Lease objects associated with each node. Node leases allow the kubelet to send heartbeats so that the control plane can detect node failure.
kube-public this is another default namespace in the Kubernetes,
Kube-system output
coredns-6d4b75cb6d-2vqw4 1/1 Running 1 (6d2h ago) 10d
coredns-6d4b75cb6d-9dlh8 1/1 Running 1 (6d2h ago) 10d
etcd-kubernet-master 1/1 Running 1 (6d2h ago) 10d
kube-apiserver-kubernet-master 1/1 Running 1 (6d2h ago) 10d
kube-controller-manager-kubernet-master 1/1 Running 1 (6d2h ago) 10d
kube-proxy-6drkc 1/1 Running 1 (6d2h ago) 10d
kube-proxy-c5jr7 1/1 Running 1 (6d2h ago) 10d
kube-scheduler-kubernet-master 1/1 Running 1 (6d2h ago) 10d
đź‘ŚNamespace you can short to ns
If your environment is small you shouldn’t really have to worry about namespaces, you could continue to work in the default namespace.
Viewing the current namespace
kubectl get namespace
Default output
NAME STATUS AGE
default Active 1d
kube-node-lease Active 1d
kube-public Active 1d
kube-system Active 1d
How to create namespace through manifest file
apiVersion: v1
kind: Namespace
metadata:
name: test
labels:
name: test
kubectl apply -f test.yaml
Create Namespace through CLI
kubectl create namespace test
How to create the container inside the namespace
kubectl run nginx -n test --image=nginx
apiVersion: v1
kind: Pod
metadata:
name: mypod
namespace: test
labels:
name: mypod
spec:
containers:
- name: mypod
image: nginx
If you try to find your Pod, you might notice you can’t!
This is because all commands are run against the currently active Namespace. To find your Pod, you need to use the “namespace” flag.
kubectl get pods --namespace=test
or
kubectl get pod -n test
How to delete the namespace
kubectl delete namespace test
What happens if I delete a namespace?
Deleting the namespace also deletes all the residing components. However, removing all deployments within a namespace does not remove the namespace.
Namespace overview
Refer to this link for more understanding about Namespaces 👉Name sapces
Do you know how to create the Pod? 🤔
Don't worry please refer to this link 👉 Pod Creation
Hope you have got an idea about Kubernetes Namespaces and how we can implement them in our product environments
Happy Learning !!!
Thank you!