bnallapeta / resource-consumer-pod

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

resource-consumer-pod

This project includes a Kubernetes CronJob setup designed to create and run a containerized application that consumes system resources such as CPU and memory. It's particularly useful for testing system monitoring and alerting capabilities. The project consists of a Python script to generate the load, a Dockerfile to containerize this script, and a Kubernetes CronJob definition to schedule and run it in your Kubernetes cluster.

Python Script (resource_consumer.py)

The Python script, resource_consumer.py, is designed to create artificial load on both CPU and memory. It does this through continuous calculations and memory allocations, simulating a high-load scenario.

  • CPU Load Generation: The script performs repeated mathematical calculations (square numbers in a loop), which consumes CPU cycles.
  • Memory Load Generation: It gradually allocates memory by appending large strings to a list, thereby increasing memory usage over time.

Dockerfile

The Dockerfile packages the Python script into a Docker container, creating an isolated environment for the script to run. The Dockerfile uses a lightweight Python base image.

  • Base Image: Python 3.9 Slim.
  • Copy Command: The Python script is copied into the Docker image.
  • CMD Directive: Specifies that the Docker container runs the Python script upon startup.

Kubernetes CronJob (resource-consumer-cronjob.yaml)

The Kubernetes CronJob definition (resource-consumer-cronjob.yaml) schedules and manages the execution of the resource-consuming Docker container on your Kubernetes cluster.

  • Schedule: Set to run the job every 5 minutes (*/5 * * * *).
  • Job Template: Specifies the pod template, which includes the container that uses the Docker image built from the provided Dockerfile.
  • Resource Limits/Requests: Defines the CPU and memory resources requests and limits for the container.

Deployment Instructions

  1. Build and Push the Docker Image:
  • Build the Docker image using the provided Dockerfile.

    docker build -t resource-consumer .

  • Tag and push the image to your Docker registry.

    docker push resource-consumer

  1. Update the CronJob YAML:
  • Modify the image field in crcj.yaml to point to your pushed Docker image.
  1. Apply the CronJob:
  • Use the below command to create the CronJob in your Kubernetes cluster.

    kubectl apply -f crcj.yaml

  1. Monitor Resource Consumption:
  • Use Kubernetes monitoring tools to observe the resource consumption generated by the CronJob.

Caution

This setup is intended for testing and should be used cautiously, especially in production environments, as it deliberately generates high CPU and memory loads. Ensure that your cluster has sufficient resources and that the load generated does not impact other critical services running in the same environment.

About


Languages

Language:Python 73.5%Language:Dockerfile 26.5%