Kube Bin Packing

Written on June 29, 2025

Script I used to get size of all pods in a Kubernetes cluster.


I used the following script to get the sizes of all the pods in the kube cluster. It calculates the total CPU and memory requests for each pod and outputs them in TSV.

#!/usr/bin/env bash

# Function to parse CPU request (in millicores)
parse_cpu() {
  local cpu_str="$1"
  if [[ "$cpu_str" == *"m"* ]]; then
    # Remove "m" and convert to integer
    echo "$(echo "$cpu_str" | sed 's/m//' | awk '{print int($1)}')"
  else
    # Convert to millicores (e.g., "2" -> 2000, "0.5" -> 500)
    echo "$(echo "$cpu_str" | awk '{print int($1 * 1000)}')"
  fi
}

# Function to parse memory request (in Mi)
parse_memory() {
  local mem_str="$1"
  if [[ "$mem_str" == *"Mi"* ]]; then
    echo "$(echo "$mem_str" | sed 's/Mi//' | awk '{print int($1)}')"
  elif [[ "$mem_str" == *"Gi"* ]]; then
    echo "$(echo "$mem_str" | sed 's/Gi//' | awk '{print int($1 * 1024)}')"
  elif [[ "$mem_str" == *"Ki"* ]]; then
    echo "$(echo "$mem_str" | sed 's/Ki//' | awk '{print int($1 / 1024)}')"
  else
    # Assume Mi if no suffix
    echo "$(echo "$mem_str" | awk '{print int($1)}')"
  fi
}


echo "=== Node Allocatable Resources ==="
kubectl get nodes -o json | jq -r '
  .items[]
      node: .metadata.name,
      cpu_allocatable: .status.allocatable.cpu,
      mem_allocatable: .status.allocatable.memory
    }
'

echo ""
echo "=== Pods and Their Total Requests (CPU in m, Mem in Mi) ==="
kubectl get pods -A -o json |
while IFS= read -r line; do
  # Extract relevant data from the JSON using jq and process it
  local namespace=$(echo "$line" | jq -r '.metadata.namespace')
  local pod=$(echo "$line" | jq -r '.metadata.name')
  local node=$(echo "$line" | jq -r '(.spec.nodeName // "-")')

  local cpu_request_m=0
  local mem_request_mi=0

  # Calculate CPU and Memory requests
  local containers=$(echo "$line" | jq -c '.spec.containers[]')
  if [[ -n "$containers" ]]; then
    while IFS= read -r container; do
      local cpu_req_str=$(echo "$container" | jq -r '.resources.requests.cpu // "0"')
      local mem_req_str=$(echo "$container" | jq -r '.resources.requests.memory // "0"')
      cpu_request_m=$((cpu_request_m + $(parse_cpu "$cpu_req_str")))
      mem_request_mi=$((mem_request_mi + $(parse_memory "$mem_req_str")))
    done <<< "$(echo "$containers" | jq -c '.')"
  fi


  # Output the results in TSV format
  echo -e "$namespace\t$pod\t$node\t${cpu_request_m}m\t${mem_request_mi}Mi"
done < <(kubectl get pods -A -o json | jq -c '.items[]')

Also one of the most useful commands I found was:

eks-node-viewer -resources cpu,memory

You can install it with:

brew tap aws/tap
brew install eks-node-viewer

# or

go install github.com/awslabs/eks-node-viewer/cmd/eks-node-viewer@latest