Hello, readers! In this article, we will be focusing on Kubernetes Namespace, in detail.
So, let us begin! 🙂
What is a Kubernetes Namespace?
Namespaces server the same purpose as they server in the Linux perspective. Yes, a Namespace helps us isolate the environment for the application throughout the cluster or node. It is way to provide proper scopes to the applications or deployments.
Using namespaces, we set an scope for the environment to the resources we offer through it. With which, we can club and have deployments and other resources such as ingress rules, services, pods, etc specific to the namespace.
k8 Default Namespaces
With Kubernetes comes the below mentioned default namespaces to use. In order to get the list of namespaces within a cluster, we use the below command-
kubectl get namespace
NAME STATUS AGE default Active 2d kube-node-lease Active 2d kube-public Active 2d kube-system Active 2d
- default: It is useful for objects or resources with no namespaces.
- kube-public: It gets created automatically and can be viewed by all the users.
- kube-system: It stores and isolates all the resources created by the Kubernetes System.
- kube-node-lease: It is for lease resource associated with one another that helps enhance the performance of the nodes at the cluster level.
Creating a namespace in k8 cluster
A Namespace within kubernetes can be created using two ways:
- kubectl command
- Namespace YAML file
In the first approach, we can create namespace using the kubectl command as shown below–
kubectl create namespace namespace-name
Here, we just need to mention the namespace name and it gets created.
In the second approach, we design a YAML file for the namespace with the below configuration. We have mentioned demo as the namespace name against the .metadata.name key.
apiVersion: v1 kind: Namespace metadata: name: demo
Having created the namespace, we need to use kubectl apply command to configure and create the namespace as shown below:
kubectl apply -f namespace.yaml
Let us now view the above created demo namespace:
kubectl get namespace
NAME STATUS AGE demo Active 13m
Resource Quota configuration with k8 Namespace
As we have earlier read that a Namespace isolates an environment for the resources we offer within it, it would definitely require resources ( CPU and Memory) to hold the resources (deployments and pods).
For the same, we can assign desired amount of request CPU/Memory and limit CPU/Memory using the below YAML structure:
apiVersion: v1 kind: ResourceQuota metadata: name: compute-resources spec: hard: requests.cpu: 2" requests.memory: 4Gi limits.cpu: "4" limits.memory: 6Gi
Here, the label kind specifies the kind of resource we aim to create. In this case, it is ResourceQuota. We have mentioned the requested and limit value of CPU and Memory for the namespace.
Now, let us bind the resource quota to a namespace. For that, we would be making use of the below command:
kubectl apply -f compute.yaml -n demo
Here in the above command, -n namespace_name would bind the resourcequota to the mentioned namespace in the apply command.
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. For more such posts related to Kubernetes, Stay tuned with us.
Till then, Happy Learning!! 🙂