k8s-pod-cpu-stressor

CI Status CD Status Docker Image Version Docker Pulls GitHub go.mod Go version GitHub License Go Report Card Contributors GitHub Issues GitHub Stars GitHub Forks Last Commit

Connect with me

X (formerly Twitter) Follow narmidm

k8s-pod-cpu-stressor

The k8s-pod-cpu-stressor is a tool designed to simulate CPU stress on Kubernetes pods. It allows you to specify the desired CPU usage and stress duration, helping you test the behavior of your Kubernetes cluster under different CPU load scenarios.

Features

Getting Started

Prerequisites

To use the k8s-pod-cpu-stressor, you need to have the following installed:

Building the Binary

  1. Clone this repository to your local machine.
  2. Navigate to the repository directory.
  3. Build the binary using the following command:

    go build -o cpu-stress .
    

Running with Docker

Build the Docker image using the provided Dockerfile:

   docker build -t k8s-pod-cpu-stressor .

Run the Docker container, specifying the desired CPU usage, stress duration, and optionally whether to run CPU stress indefinitely:

docker run --rm k8s-pod-cpu-stressor -cpu=0.2 -duration=10s -forever

Replace 0.2 and 10s with the desired CPU usage (fraction) and duration, respectively. Add -forever flag to run CPU stress indefinitely.

CPU Usage and Duration

The k8s-pod-cpu-stressor allows you to specify the desired CPU usage and stress duration using the following parameters:

Adjust these parameters according to your requirements to simulate different CPU load scenarios.

Kubernetes Resource Requests and Limits

It is recommended to specify Kubernetes resource requests and limits to control the amount of CPU resources consumed by the pod, and to prevent overloading your cluster. For example:

Adding requests and limits helps Kubernetes manage resources efficiently and ensures that your cluster remains stable during stress testing.

Example:

resources:
  requests:
    cpu: "100m"
  limits:
    cpu: "200m"

Check the Public Docker Image

The k8s-pod-cpu-stressor Docker image is publicly available on Docker Hub. You can check and pull the image using the following command:

docker pull narmidm/k8s-pod-cpu-stressor:latest

Sample Deployment Manifest

Use the following deployment manifest as a starting point to deploy the k8s-pod-cpu-stressor image in your Kubernetes cluster:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: cpu-stressor-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cpu-stressor
  template:
    metadata:
      labels:
        app: cpu-stressor
    spec:
      containers:
        - name: cpu-stressor
          image: narmidm/k8s-pod-cpu-stressor:latest
          args:
            - "-cpu=0.2"
            - "-duration=10s"
            - "-forever"
          resources:
            limits:
              cpu: "200m"
            requests:
              cpu: "100m"

Sample Job Manifest

If you want to run the CPU stressor for a fixed duration as a one-time job, you can use the following Kubernetes Job manifest:

apiVersion: batch/v1
kind: Job
metadata:
  name: cpu-stressor-job
spec:
  template:
    metadata:
      labels:
        app: cpu-stressor
    spec:
      containers:
        - name: cpu-stressor
          image: narmidm/k8s-pod-cpu-stressor:latest
          args:
            - "-cpu=0.5"
            - "-duration=5m"
          resources:
            limits:
              cpu: "500m"
            requests:
              cpu: "250m"
      restartPolicy: Never
  backoffLimit: 3

This manifest runs the k8s-pod-cpu-stressor as a Kubernetes Job, which will execute the stress test once for 5 minutes and then stop. The backoffLimit specifies the number of retries if the job fails.

Contributing

Contributions are welcome! If you find a bug or have a suggestion, please open an issue or submit a pull request. For major changes, please discuss them first in the issue tracker.

License

This project is licensed under the MIT License. See the LICENSE file for details.