Namespace
In kubernetes cluster, we have to isolate, manage, organize, allocate and secure various resources. Namespace are the way or mechanism to manage the resources such as pod
,deploy
, service
, etc. It provides virtual cluster within a single cluster. It provides the proper optimization of resources such as seperation of user based on role, allows teams to share across a cluster. Basically, it logically isolates the resources with concern of security.
Four namespaces are created automatically while setting up cluster.
~ kubectl get namespaces
Output
NAME STATUS AGE
default Active 11d
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
kubernetes-dashboard Active 11d
- default: All the resources deployment without a namespace are deployed under default namespace.
-
kube-node-lease: All nodes have an
Lease
object in this namespace. -
kube-public:
kube-public
is available to all users with read-only-access and reserved for system usage. - kube-system: This namespace holds all the configurations and deployments.
-
kubernetes-dashboard: Kubernetes dashboard is a web based user interface for managing cluster and its resources. It is not created by default as other namespace but created while setting up kubernetes dashboard. Kubernetes dashboard is deployed in
kubernetes-dashboard
namespace.
Creating namespace
We can create namespace in different ways with kubectl create namespace <namespace-name>
or with yaml file.
Naming conventions
- the characters must be less than or equal to 63
- must start and end with an alphanumeric character
- must consist of lower case alphanumeric characters or "-"
Method 1 : Direct create with CLI
~ kubectl create namespace user
namespace/user created
It will create user namespace in kubernetes cluster. We can use ns
as shortcut for namespace
.
Lets check,
~ kubectl get ns
Output
NAME STATUS AGE
default Active 12d
kube-node-lease Active 12d
kube-public Active 12d
kube-system Active 12d
kubernetes-dashboard Active 12d
user Active 27s
~ kubectl create namespace qa --dry-run=client
namespace/qa created (dry run)
With --dry-run=client
, it will not deploy namespace to server but just simulate it.
Lets check,
~ kubectl get ns
Output
NAME STATUS AGE
default Active 12d
kube-node-lease Active 12d
kube-public Active 12d
kube-system Active 12d
kubernetes-dashboard Active 12d
user Active 27s
We see that, qa
namespace is not deployed to the server.
Method 2 : Create with yaml file
We can also create namespace by writing scripts in yaml file.
Lets create yaml file.
~ vim qa.yaml
apiVersion: v1
kind: Namespace
metadata:
name: qa
We can apply the scripts of qa.yaml with command as:
~ kubectl create -f qa.yaml
namespace/qa created
or
~ kubectl apply -f qa.yaml
namespace/qa created
Now this qa
namespace is created and deployed to server as well.
Lets check,
~ kubectl get ns
Output
NAME STATUS AGE
default Active 12d
kube-node-lease Active 12d
kube-public Active 12d
kube-system Active 12d
kubernetes-dashboard Active 12d
qa Active 89s
user Active 64m
Deploy resource to namespace
In the previous post, we have published a container image nirajpdn/react-app:v1
and nirajpdn/react-app:v2
to docker hub. Lets deploy that image to namespace.
~ kubectl run react-app --image=nirajpdn/react-app:v1
It will deploy that image to the default
namespace by default because we haven't specified the namespace to deploy.
~ kubectl get po
It will shows all the pods in the default namespace.
Output
NAME READY STATUS RESTARTS AGE
react-app 1/1 Running 0 3m1s
Lets check the pods with specifying namespace.
~ kubectl get po -n qa
No resources found in qa namespace.
Alright, we haven't deployed any resource to the qa
namespace.
~ kubectl run react-app --image=nirajpdn/react-app:v1 -n qa
~ kubectl get po -n qa
Output
NAME READY STATUS RESTARTS AGE
react-app 1/1 Running 0 11s
Finally we have successfully deployed docker image to the kubernetes cluster.
~ kubectl describe po react-app -n qa
Output
Name: react-app
Namespace: qa
Priority: 0
Node: minikube/192.168.49.2
Start Time: Mon, 20 Jun 2022 02:17:59 +0545
Labels: run=react-app
Annotations: <none>
Status: Running
IP: 172.17.0.3
IPs:
IP: 172.17.0.3
Containers:
react-app:
Container ID: docker://f956442127390f513901c6aa7049de6be0d95f2f4fa81257ec67827e7268c6c7
Image: nirajpdn/react-app:v1
Image ID: docker-pullable://nirajpdn/react-app@sha256:dcfb3c960e0c6777a098578564ca1deb547e73debcc8aa59702d8ad43dca75c6
Port: <none>
Host Port: <none>
State: Running
Started: Mon, 20 Jun 2022 02:18:00 +0545
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-6tdhw (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-6tdhw:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 15s default-scheduler Successfully assigned qa/react-app to minikube
Normal Pulled 14s kubelet Container image "nirajpdn/react-app:v1" already present on machine
Normal Created 14s kubelet Created container react-app
Normal Started 14s kubelet Started container react-app
We can also delete resource with command:
~ kubectl delete po react-app -n qa
It will delete the react-app
pods from qa
namespace.
~ kubectl get po -n qa
No resources found in qa namespace.
Deleting namespace
We can delete namespace just by,
~ kubectl delete ns qa
namespace "qa" deleted
or if namespace is created with yaml file then we can also do as follows:
~ kubectl delete -f qa.yaml
namespace "qa" deleted
While deleting namespace with delete command, all the resources associated with particular namespace will be deleted.
In next post we will discuss about ReplicaSet
.
Top comments (2)
kubernetes-dashboard
does not appear to be part of the default automated namespaces, I think.I have mentioned that, namespace
kubernetes-dashboard
is not automatically created by default but it is created while setting up Kubernetes Dashboard in minikube or any cluster. Is it ok ?