Kubernetes resource management and health checks diagram showing CPU limits, resource requests, liveness probes, and readiness probes monitoring container health in a cluster.

βš™οΈ 1. Resource Management Testing

One of the most powerful features in Kubernetes is the ability to control how much CPU and memory a container is allowed to use. Without limits, a single container could consume all the node resources and impact other applications running in the cluster.

Why resource management matters πŸš€ Kubernetes uses requests and limits to schedule workloads efficiently and prevent noisy containers from affecting the rest of the system.
Setting Purpose
Request Minimum resources Kubernetes guarantees for the container
Limit Maximum resources the container is allowed to consume

πŸ“„ Resource Test Deployment File

File: 08-resource-test.yaml

apiVersion: apps/v1
kind: Deployment

metadata:
  name: resource-test

spec:
  replicas: 1

  selector:
    matchLabels:
      app: resource-test

  template:
    metadata:
      labels:
        app: resource-test

    spec:
      containers:

      - name: stress-container

        # This container intentionally generates CPU load
        # It allows us to observe how Kubernetes enforces limits
        image: polinux/stress

        # Run stress tool to consume CPU
        args: ["--cpu", "1"]

        resources:

          # Guaranteed CPU allocation
          requests:
            cpu: "250m"

          # Maximum CPU allowed
          limits:
            cpu: "500m"

This deployment launches a container that intentionally generates CPU load using the stress utility. Because we defined both requests and limits, Kubernetes can enforce resource rules.

  • 🧠 250m request means the container is guaranteed 0.25 CPU.
  • 🚦 500m limit means it cannot exceed half a CPU core.
  • ⚑ If it tries to use more than that, Kubernetes will throttle the container.

πŸš€ Apply the Deployment

kubectl apply -f 08-resource-test.yaml

This command creates the deployment and schedules the pod on a node. The container will immediately begin generating CPU load.

πŸ‘€ Watch the Pod in Real Time

kubectl get pods -l app=resource-test -w

This command streams pod status updates in real time.

  • -l filters pods using a label selector
  • -w enables live watch mode

πŸ”Ž Inspect Pod Resource Settings

kubectl describe pod -l app=resource-test

This command displays detailed pod information including:

  • Configured CPU requests and limits
  • Events related to resource throttling
  • Container lifecycle events

πŸ“Š Check Real-Time Resource Usage

kubectl top pod -l app=resource-test

This shows the actual CPU and memory usage of the running pod.

Tip πŸ’‘ This command requires the metrics-server to be installed in the cluster. It is commonly included in tools like Minikube or K3s.

β€οΈβ€πŸ©Ή 2. Health Check Testing

Kubernetes continuously monitors application health using probes. These checks help Kubernetes automatically detect and recover from application failures.

Probe Purpose
Readiness Probe Determines if the pod is ready to receive traffic
Liveness Probe Restarts containers if they become unhealthy

πŸ“„ Health Check Deployment File

File: 06-health-checks.yaml

apiVersion: apps/v1
kind: Deployment

metadata:
  name: nginx-health

spec:
  replicas: 1

  selector:
    matchLabels:
      app: nginx-health

  template:
    metadata:
      labels:
        app: nginx-health

    spec:
      containers:

      - name: nginx
        image: nginx:1.14

        ports:
        - containerPort: 80

        # Readiness probe checks if pod can receive traffic
        readinessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 5

        # Liveness probe ensures container is still healthy
        livenessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 15
          periodSeconds: 10

This deployment uses two important health mechanisms:

  • 🟒 Readiness probe prevents traffic from reaching a container until it is ready.
  • πŸ” Liveness probe automatically restarts the container if it becomes unhealthy.
Why this matters 🧠 These probes enable Kubernetes to provide self-healing applications. If a container fails, Kubernetes detects the problem and restarts it automatically.

πŸš€ Deploy the Health Check Example

kubectl apply -f 06-health-checks.yaml

πŸ‘€ Watch Pod Status

kubectl get pods -l app=nginx-health -w

Watching pods live allows you to see when:

  • Probes succeed
  • Probes fail
  • Containers restart automatically

πŸ”Ž Inspect Probe Configuration

kubectl describe pod -l app=nginx-health

This command shows detailed probe configuration including:

  • Probe timing settings
  • Failure thresholds
  • Probe success/failure events

πŸ“œ Check Container Logs

kubectl logs -l app=nginx-health

Because probes send HTTP requests to nginx, these requests appear inside the nginx access logs. This makes debugging health check issues much easier.

🧹 3. Cleanup Commands

After testing is complete, it’s good practice to remove temporary resources from your cluster.

kubectl delete -f 08-resource-test.yaml
kubectl delete -f 06-health-checks.yaml

These commands remove both deployments and automatically terminate their associated pods.

βœ… Verify Cleanup

kubectl get all

This command lists all resources in the current namespace. After cleanup, you should only see the default kubernetes service.

Leave a Reply

Your email address will not be published. Required fields are marked *