0
0
Kubernetesdevops~5 mins

Resource quotas per namespace in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When many teams share the same Kubernetes cluster, some teams might use too many resources and leave none for others. Resource quotas help limit how much CPU, memory, and other resources each team can use in their own space, called a namespace.
When you want to make sure one team does not use all the CPU or memory in a shared cluster.
When you want to control how many pods or services a namespace can create to avoid overload.
When you want to track resource usage per project or team to plan capacity better.
When you want to prevent accidental resource hogging by limiting storage or load balancers.
When you want to enforce fair resource sharing in a multi-tenant Kubernetes environment.
Config File - resource-quota.yaml
resource-quota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
  name: example-quota
  namespace: example-namespace
spec:
  hard:
    pods: "10"
    requests.cpu: "4"
    requests.memory: "8Gi"
    limits.cpu: "8"
    limits.memory: "16Gi"
    persistentvolumeclaims: "5"

This file defines a ResourceQuota named example-quota in the example-namespace.

The hard section sets limits: maximum 10 pods, 4 CPUs requested, 8Gi memory requested, 8 CPUs limit, 16Gi memory limit, and 5 persistent volume claims allowed.

This ensures the namespace cannot use more resources than these limits.

Commands
This command creates the resource quota in the specified namespace to limit resource usage.
Terminal
kubectl apply -f resource-quota.yaml
Expected OutputExpected
resourcequota/example-quota created
This command lists all resource quotas in the example-namespace to verify the quota was created.
Terminal
kubectl get resourcequota -n example-namespace
Expected OutputExpected
NAME AGE example-quota 10s
-n - Specifies the namespace to look in
This command shows detailed information about the resource quota, including current usage and limits.
Terminal
kubectl describe resourcequota example-quota -n example-namespace
Expected OutputExpected
Name: example-quota Namespace: example-namespace Resource Used Hard -------- --- ---- pods 0 10 requests.cpu 0 4 requests.memory 0 8Gi limits.cpu 0 8 limits.memory 0 16Gi persistentvolumeclaims 0 5
-n - Specifies the namespace
Key Concept

If you remember nothing else from this pattern, remember: resource quotas limit how much CPU, memory, pods, and other resources a namespace can use to keep cluster resources fairly shared.

Common Mistakes
Applying a resource quota without specifying the correct namespace in the YAML file.
The quota will be created in the default namespace or wrong namespace, so it won't limit the intended namespace.
Always set the correct namespace under metadata.namespace in the resource quota YAML.
Setting resource limits in the quota but not setting matching requests and limits in pod specs.
Resource quota counts requests and limits; if pods don't specify them, quota enforcement may not work as expected.
Ensure pods specify resource requests and limits that align with the quota to enforce limits properly.
Not checking current usage with 'kubectl describe resourcequota' after applying quota.
You might miss that some resources are already over quota or that usage is near limits.
Use 'kubectl describe resourcequota' to monitor usage and adjust quotas as needed.
Summary
Create a resource quota YAML file specifying limits for CPU, memory, pods, and other resources in a namespace.
Apply the quota with 'kubectl apply -f' to enforce resource limits in that namespace.
Verify quota creation with 'kubectl get resourcequota' and check usage details with 'kubectl describe resourcequota'.