What are Namespaces and Resource Quota in Kubernetes?
Kubernetes is an open source platform for managing containerized applications. It allows for efficient running, scaling, and deployment of applications. Kubernetes namespaces provide an additional layer of abstraction in the Kubernetes environment, allowing users to group related resources together. Resource quotas are a feature of Kubernetes that enable users to limit the amount of resources that can be consumed in a given namespace. In this article, we’ll discuss what namespaces and resource quotas are and how they can be used in a Kubernetes cluster. We’ll also go over a lab example and commands related to namespaces and resource quotas.
What are Kubernetes Namespaces?
Kubernetes namespaces provide an additional layer of abstraction in the Kubernetes environment. Namespaces allow users to group related resources together, such as Pods, Services, ConfigMaps, and PersistentVolumes. They can also be used to isolate resources from each other, allowing for more efficient resource utilization and control.
Namespaces can be used in a variety of ways. They can be used to separate resources for different teams or environments, such as development, staging, and production. They can also be used to separate resources for different applications or services, such as web servers, databases, and microservices.
What are Kubernetes Resource Quotas?
Kubernetes resource quotas are a feature of Kubernetes that enable users to limit the amount of resources that can be consumed in a given namespace. Resource quotas can be used to limit the number of resources of each type, such as Pods, Services, ConfigMaps, and PersistentVolumes. They can also be used to limit the amount of compute resources, such as CPU, memory, and storage, that can be consumed in a namespace.
Resource quotas are a great way to ensure that resources are not over-consumed in a Kubernetes cluster. They can be used to ensure that one team or application does not consume too many resources and prevent other teams or applications from having access to the resources they need.
Lab Example: Setting Up Namespaces and Resource Quotas
In this lab example, we’ll set up two namespaces, “dev” and “prod”. We’ll then set up resource quotas for each namespace.
1. Create the Namespaces
apiVersion: v1
kind: Namespace
metadata:
name: dev
labels:
name: dev
2. Create a Pod
kind: Pod
apiVersion: v1
metadata:
name: testpod
spec:
containers:
- name: c00
image: ubuntu
command: ["/bin/bash", "-c", "while true; do echo Shishir-Tambe; sleep 5 ; done"]
restartPolicy: Never
Then run:
$ kubectl config set-context $(kubectl config current-context) --namespace=dev
$ kubectl config view | grep namespace:
apiVersion: v1
kind: Pod
metadata:
name: resources
spec:
containers:
- name: resource
image: centos
command: ["/bin/bash", "-c", "while true; do echo Technical-Guftgu; sleep 5 ; done"]
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "200m"
Once the namespace is created, administrators can add resource objects to it using the same command. The following command adds a pod to the “my-namespace” namespace:
$ kubectl create -f my-pod.yaml --namespace=my-namespace
How to Create Resource Quotas in Kubernetes:
Resource quotas can be created using the kubectl command-line tool. The following command creates a resource quota named “my-quota” in the “my-namespace” namespace:
$ kubectl create -f my-quota.yaml --namespace=my-namespace
apiVersion: v1
kind: Pod
metadata:
name: resources
spec:
containers:
- name: resource
image: centos
command: ["/bin/bash", "-c", "while true; do echo Technical-Guftgu; sleep 5 ; done"]
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "200m"
create a Resource quota
apiVersion: v1
kind: ResourceQuota
metadata:
name: myquota
spec:
hard:
limits.cpu: "400m"
limits.memory: "400Mi"
requests.cpu: "200m"
requests.memory: "200Mi"
and for deploying resource quota:
kind: Deployment
apiVersion: apps/v1
metadata:
name: deployments
spec:
replicas: 3
selector:
matchLabels:
objtype: deployment
template:
metadata:
name: testpod8
labels:
objtype: deployment
spec:
containers:
- name: c00
image: ubuntu
command: ["/bin/bash", "-c", "while true; do echo Technical-Guftgu; sleep 5 ; done"]
resources:
requests:
cpu: "200m"
Create Limit Range:
apiVersion: v1
kind: LimitRange
metadata:
name: cpu-limit-range
spec:
limits:
- default:
cpu: 1
defaultRequest:
cpu: 0.5
type: Container
Create a 2 default CPU (default-cpu-demo-2 & default-cpu-demo-3)
apiVersion: v1
kind: Pod
metadata:
name: default-cpu-demo-2
spec:
containers:
- name: default-cpu-demo-2-ctr
image: nginx
resources:
limits:
cpu: "1"
apiVersion: v1
kind: Pod
metadata:
name: default-cpu-demo-2
spec:
containers:
- name: default-cpu-demo-2-ctr
image: nginx
resources:
limits:
cpu: "1"
The resource quota object includes a list of resource limits, such as the maximum number of CPU cores and memory that can be used by the namespace.
Conclusion:
In this blog post, we discussed what namespaces and resource quotas are in Kubernetes and how they can be used to manage and allocate resources. We also discussed how to create namespaces and resource quotas using the kubectl command-line tool.
Namespaces and resource quotas are essential tools for managing resources in a Kubernetes cluster. They allow administrators to divide the cluster into multiple isolated environments and limit the resources that can be used by each namespace. By using namespaces and resource quotas, administrators can ensure that each user or team has access to the resources they need and that no one user or team has too much control over resources.
Subscribe to my newsletter
Read articles from Shishir Tambe directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Shishir Tambe
Shishir Tambe
Newbie to IT/CS