Namespaces are used for resource isolation. For example, two different namespaces can have a resource(like Pod) with the same name.
Using namespaces, we can split resources into non-overlapping groups.
Are there any resources that are not tied to a single namespace?
Yes, Kubernetes has some resource types that are not tied to a single namespace. For example, a node resource does not tie to any single namespace.
You can execute the command ‘kubectl get all --all-namespaces’ to see a list of namespaces available in your Kubernetes environment.
$kubectl get all --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system pod/coredns-66bff467f8-mdqf8 1/1 Running 4 31d
kube-system pod/coredns-66bff467f8-qrv7b 1/1 Running 4 31d
kube-system pod/etcd-minikube 1/1 Running 4 31d
kube-system pod/kube-apiserver-minikube 1/1 Running 4 31d
kube-system pod/kube-controller-manager-minikube 1/1 Running 8 31d
kube-system pod/kube-proxy-9zlxd 1/1 Running 4 31d
kube-system pod/kube-scheduler-minikube 1/1 Running 8 31d
kube-system pod/storage-provisioner 1/1 Running 8 31d
kubernetes-dashboard pod/dashboard-metrics-scraper-84bfdf55ff-f299w 1/1 Running 3 30d
kubernetes-dashboard pod/kubernetes-dashboard-bc446cc64-wj865 1/1 Running 4 30d
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 30d
kube-system service/kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 31d
kubernetes-dashboard service/dashboard-metrics-scraper ClusterIP 10.110.152.155 <none> 8000/TCP 30d
kubernetes-dashboard service/kubernetes-dashboard ClusterIP 10.100.34.170 <none> 80/TCP 30d
NAMESPACE NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
kube-system daemonset.apps/kube-proxy 1 1 1 1 1 kubernetes.io/os=linux 31d
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
kube-system deployment.apps/coredns 2/2 2 2 31d
kubernetes-dashboard deployment.apps/dashboard-metrics-scraper 1/1 1 1 30d
kubernetes-dashboard deployment.apps/kubernetes-dashboard 1/1 1 1 30d
NAMESPACE NAME DESIRED CURRENT READY AGE
kube-system replicaset.apps/coredns-66bff467f8 2 2 2 31d
kubernetes-dashboard replicaset.apps/dashboard-metrics-scraper-84bfdf55ff 1 1 1 30d
kubernetes-dashboard replicaset.apps/kubernetes-dashboard-bc446cc64 1 1 1 30d
How to create a namespace?
Syntax
kubectl create ns {nameSpaceName}
Example
kubectl create ns data-platform
$kubectl create ns data-platform namespace/data-platform created
Create a namespace from the definition file
namespaceDef.yml
apiVersion: v1
kind: Namespace
metadata:
name: aero
$kubectl create -f namespaceDef.yml namespace/aero created
You can query all the namespaces to confirm that the namespaces are created.
$kubectl get ns
NAME STATUS AGE
aero Active 4s
data-platform Active 34s
default Active 36d
kube-node-lease Active 35d
kube-public Active 36d
kube-system Active 36d
kubernetes-dashboard Active 34d
How to create a pod in the namespace?
You can specify the namespace to be used using ‘metadata.namespace’ property. If you do not specify any namespace, then the resource (can be Pod, Replicaset, Deployment) will get created in the default namespace.
nameSpaceDemoPod.yml
apiVersion: v1
kind: Pod
metadata:
name: employee-service
labels:
app: employee-service
author: krishna
serviceType: webservice
namespace: data-platform
spec:
containers:
- name: employee-service-container
image: jboss/wildfly
Let’s create the Pod by executing the below command.
kubectl create -f nameSpaceDemoPod.yml
$kubectl create -f nameSpaceDemoPod.yml pod/employee-service created
Let’s get the pods in all namespaces by executing below command.
$kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
data-platform employee-service 1/1 Running 0 45s
kube-system coredns-66bff467f8-mdqf8 1/1 Running 4 31d
kube-system coredns-66bff467f8-qrv7b 1/1 Running 4 31d
kube-system etcd-minikube 1/1 Running 4 31d
kube-system kube-apiserver-minikube 1/1 Running 4 31d
kube-system kube-controller-manager-minikube 1/1 Running 8 31d
kube-system kube-proxy-9zlxd 1/1 Running 4 31d
kube-system kube-scheduler-minikube 1/1 Running 8 31d
kube-system storage-provisioner 1/1 Running 8 31d
kubernetes-dashboard dashboard-metrics-scraper-84bfdf55ff-f299w 1/1 Running 3 30d
kubernetes-dashboard kubernetes-dashboard-bc446cc64-wj865 1/1 Running 4 30d
As you see the output, Pod ‘employee-service’ is created in the namespace ‘data-platform’.
You can delete the namespace by executing the below command.
kubectl delete ns data-platform
$kubectl delete ns data-platform
namespace "data-platform" deleted
Once you delete a namespace, all the resources in the namespace will get deleted.
No comments:
Post a Comment