Cluster Resource Optimization

Objectives

  • Understand resource requests and limits.

  • Implement resource quotas and limit ranges.

  • Monitor and manage resource utilization.

Task 1: Set Resource Requests and Limits for Pods

Objective: Set resource requests and limits for a set of pods.

Explanation: Resource requests and limits define the amount of CPU and memory resources a container can use. Setting these appropriately ensures optimal performance and prevents resource contention.

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: mycontainer
      image: kodekloud/webapp-delayed-start 
      resources:
        requests:
          memory: "64Mi"
          cpu: "250m"
        limits:
          memory: "128Mi"
          cpu: "500m"
oc create -f pod-definition.yaml

Task 2: Configure and Manage Resource Quotas for a Namespace

Objective: Configure and manage resource quotas for a namespace.

Explanation: Resource quotas limit the aggregate resource consumption per namespace. Configuring and managing them ensures fair resource distribution and prevents overconsumption.

apiVersion: v1
kind: ResourceQuota
metadata:
  name: mem-cpu-quota
  namespace: labs
spec:
  hard:
    requests.memory: "1Gi"
    limits.memory: "2Gi"
    requests.cpu: "1"
    limits.cpu: "2"
oc create -f resource-quota.yaml

Use Monitoring Tools to Observe Resource Utilization

Additionally, monitoring can be done using setting up popular monitoring tools (e.g., Prometheus, Grafana) and how they can be integrated into OpenShift and Kubernetes.

Query CPU Usage in Prometheus UI:

sum(rate(container_cpu_usage_seconds_total{container_name!="POD"}[5m])) by (pod_name)