13. Resource Quotas and Limits in Kubernetes

13. Resource Quotas and Limits in Kubernetes

Resource management is a critical aspect of maintaining a balanced Kubernetes cluster. Kubernetes provides features like Resource Quotas and Limits to control the allocation of CPU and memory resources for containers. These mechanisms ensure that applications do not exceed the cluster’s capacity and help prevent resource contention.


Understanding Resource Quotas and Limits

What are Resource Quotas?

Resource Quotas are applied at the namespace level to restrict the total amount of resources that can be consumed by all the objects within a namespace. This ensures fair usage of cluster resources among different teams or projects.

What are Resource Limits?

Resource Limits are defined at the container level to specify the minimum and maximum amount of resources a container can use. These limits help optimize resource allocation and ensure stability in the cluster.


Example: NGINX Deployment with Resource Requests and Limits

Let’s create an NGINX deployment that uses resource requests and limits to manage CPU and memory usage:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 200m
            memory: 256Mi
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: my-volume
      volumes:
      - name: my-volume
        persistentVolumeClaim:
          claimName: local-pvc

create nginx deployment and verify (Before creating deployment make sure PV and PVC created)

kubectl apply -f nginx-deployment.yml -n nginx
kubectl get deployment -n nginx

Key Points:

  • Requests: The guaranteed minimum resources a container needs to run.

    • Example: cpu: 100m, memory: 128Mi
  • Limits: The maximum resources a container is allowed to consume.

    • Example: cpu: 200m, memory: 256Mi

Benefits of Resource Quotas and Limits

  • Fair Resource Allocation: Prevents a single application from monopolizing cluster resources.

  • Improved Cluster Stability: Ensures that critical applications have guaranteed resources.

  • Enhanced Scalability: Makes it easier to manage large clusters with multiple teams and workloads.

  • Cost Optimization: Avoids over-provisioning and under-utilization of resources.


Conclusion

By implementing Resource Quotas and Limits, you can effectively manage the allocation of resources in your Kubernetes cluster. These features play a crucial role in maintaining a stable, efficient, and cost-effective environment for running applications. Start leveraging these tools today to ensure your Kubernetes workloads are optimized for performance and reliability.