#!/bin/bash # Script to fix "too many open files" error in Kubernetes # This error occurs when the system hits inotify limits echo "🔧 Fixing Kubernetes inotify limits..." # Check if we're running on macOS (Docker Desktop) or Linux if [[ "$(uname)" == "Darwin" ]]; then echo "🍎 Detected macOS - Docker Desktop environment" echo "" echo "For Docker Desktop on macOS, you need to:" echo "1. Open Docker Desktop settings" echo "2. Go to 'Resources' -> 'Advanced'" echo "3. Increase the memory allocation (recommended: 8GB+)" echo "4. Restart Docker Desktop" echo "" echo "Alternatively, you can run:" echo "docker system prune -a --volumes" echo "Then restart Docker Desktop" # Also check if we can adjust macOS system limits echo "" echo "Checking current macOS inotify limits..." sysctl kern.maxfilesperproc sysctl kern.maxfiles echo "" echo "To increase macOS limits permanently, add to /etc/sysctl.conf:" echo "kern.maxfiles=1048576" echo "kern.maxfilesperproc=65536" echo "Then run: sudo sysctl -w kern.maxfiles=1048576" echo "And: sudo sysctl -w kern.maxfilesperproc=65536" elif [[ "$(uname)" == "Linux" ]]; then echo "🐧 Detected Linux environment" # Check if we're in a Kubernetes cluster if kubectl cluster-info >/dev/null 2>&1; then echo "🎯 Detected Kubernetes cluster" # Check current inotify limits echo "" echo "Current inotify limits:" sysctl fs.inotify.max_user_watches sysctl fs.inotify.max_user_instances sysctl fs.inotify.max_queued_events # Increase limits temporarily echo "" echo "Increasing inotify limits temporarily..." sudo sysctl -w fs.inotify.max_user_watches=524288 sudo sysctl -w fs.inotify.max_user_instances=1024 sudo sysctl -w fs.inotify.max_queued_events=16384 # Verify changes echo "" echo "New inotify limits:" sysctl fs.inotify.max_user_watches sysctl fs.inotify.max_user_instances sysctl fs.inotify.max_queued_events # Check if we can make permanent changes if [[ -f /etc/sysctl.conf ]]; then echo "" echo "Making inotify limits permanent..." sudo bash -c 'cat >> /etc/sysctl.conf << EOF # Increased inotify limits for Kubernetes fs.inotify.max_user_watches=524288 fs.inotify.max_user_instances=1024 fs.inotify.max_queued_events=16384 EOF' sudo sysctl -p fi # Check for Docker containers that might need restarting echo "" echo "Checking for running containers that might need restarting..." docker ps --format "{{.Names}}" | while read container; do echo "Restarting container: $container" docker restart "$container" >/dev/null 2>&1 || echo "Failed to restart $container" done else echo "⚠️ Kubernetes cluster not detected" echo "This script should be run on a Kubernetes node or with kubectl access" fi else echo "❓ Unsupported operating system: $(uname)" fi echo "" echo "📋 Additional recommendations:" echo "1. For Kind clusters: kind delete cluster && kind create cluster" echo "2. For Minikube: minikube stop && minikube start" echo "3. For production: Adjust node system limits and restart kubelet" echo "4. Consider adding resource limits to your deployments" echo "" echo "✅ Inotify fix script completed!"