Skip to content

Commit cf3253c

Browse files
[Misc] Auto-size Minikube memory via calculate_safe_memory (vllm-project#637)
* Auto-size Minikube memory via calculate_safe_memory Signed-off-by: Alessandro Sangiorgi <[email protected]> * Fix issues Add error for small machine Signed-off-by: Alessandro Sangiorgi <[email protected]> --------- Signed-off-by: Alessandro Sangiorgi <[email protected]> Co-authored-by: Yuhan Liu <[email protected]>
1 parent 459c9e1 commit cf3253c

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

utils/install-minikube-cluster.sh

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,45 @@ else
4141
echo "BPF JIT hardening configuration not available, skipping..."
4242
fi
4343

44+
calculate_safe_memory() {
45+
local floor_mb=2048
46+
local host_reserve_mb=2048
47+
48+
local total_kb avail_kb total_mb avail_mb
49+
total_kb=$(awk '/MemTotal:/ {print $2}' /proc/meminfo)
50+
avail_kb=$(awk '/MemAvailable:/ {print $2}' /proc/meminfo)
51+
total_mb=$(( total_kb / 1024 ))
52+
avail_mb=$(( avail_kb > 0 ? avail_kb / 1024 : (total_mb * 60 / 100) ))
53+
54+
# cgroup v2 limit if any
55+
local cg_raw cg_mb=0
56+
if [[ -r /sys/fs/cgroup/memory.max ]]; then
57+
cg_raw=$(cat /sys/fs/cgroup/memory.max)
58+
[[ "$cg_raw" != "max" ]] && cg_mb=$(( cg_raw / 1024 / 1024 ))
59+
fi
60+
61+
local target=$(( avail_mb * 80 / 100 ))
62+
local total_cap=$(( total_mb * 90 / 100 ))
63+
(( target > total_cap )) && target=$total_cap
64+
65+
local max_allowed=$(( total_mb - host_reserve_mb ))
66+
if (( cg_mb > 0 )); then
67+
local cg_cap=$(( cg_mb - host_reserve_mb ))
68+
(( cg_cap < max_allowed )) && max_allowed=$cg_cap
69+
fi
70+
71+
# If the machine is too small, fail
72+
if (( max_allowed < floor_mb )); then
73+
echo "ERROR: Not enough RAM to auto-size (total=${total_mb}MB, allowed=${max_allowed}MB). Set MINIKUBE_MEM manually." >&2
74+
return 1
75+
fi
76+
77+
(( target < floor_mb )) && target=$floor_mb
78+
(( target > max_allowed )) && target=$max_allowed
79+
80+
echo "$target"
81+
}
82+
4483
# --- NVIDIA GPU Setup ---
4584
GPU_AVAILABLE=false
4685
if command -v "$NVIDIA_SMI_PATH" >/dev/null 2>&1; then
@@ -55,6 +94,10 @@ else
5594
echo "No NVIDIA GPU detected. Will start minikube without GPU support."
5695
fi
5796

97+
if [[ -z "${MINIKUBE_MEM:-}" ]]; then
98+
MINIKUBE_MEM="$(calculate_safe_memory)"
99+
fi
100+
58101
if [ "$GPU_AVAILABLE" = true ]; then
59102
# Configure Docker for GPU support.
60103
echo "Configuring Docker runtime for GPU support..."
@@ -69,7 +112,7 @@ if [ "$GPU_AVAILABLE" = true ]; then
69112

70113
# Start minikube with GPU support.
71114
echo "Starting minikube with GPU support..."
72-
minikube start --memory=max --driver=docker --container-runtime=docker --gpus=all --force --addons=nvidia-device-plugin
115+
minikube start --memory="${MINIKUBE_MEM}" --driver=docker --container-runtime=docker --gpus=all --force --addons=nvidia-device-plugin
73116

74117
# Update kubeconfig context.
75118
echo "Updating kubeconfig context..."
@@ -85,7 +128,7 @@ else
85128
echo "Starting minikube without GPU support..."
86129
# Fix potential permission issues.
87130
sudo sysctl fs.protected_regular=0
88-
minikube start --memory=max --driver=docker --force
131+
minikube start --memory="${MINIKUBE_MEM}" --driver=docker --force
89132
fi
90133

91134
echo "Minikube cluster installation complete."

0 commit comments

Comments
 (0)