Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
Kubernetes

Debug Kubernetes Pods That Keep Crashing Before Logs Disappear

- 01.02.26 | 01.02.26 - ErcanOPAK

Your pod crashes in 2 seconds, and logs vanish before you can read them? Here’s how to catch the output before Kubernetes deletes the container.

The Problem: When a pod CrashLoops, the container exits so fast that ‘kubectl logs’ shows nothing useful or “container not found” errors. By the time you run the command, Kubernetes has already removed the crashed container.

Instant Solution – Previous Container Logs:

kubectl logs POD_NAME --previous

The ‘–previous’ flag shows logs from the LAST crashed instance, even after the container is gone. This captures the fatal error that caused the crash.

Better: Keep Failed Container for Investigation:

apiVersion: v1
kind: Pod
metadata:
  name: debug-pod
spec:
  restartPolicy: Never  # Critical: Don't auto-restart
  containers:
  - name: app
    image: your-image
    command: ["sh", "-c"]
    args:
    - |
      echo "Starting debug session..."
      /your/actual/command || sleep 3600  # Keep alive on failure

Why This Works:
‘restartPolicy: Never’ tells Kubernetes “don’t restart this pod even if it fails.” The ‘|| sleep 3600’ keeps the container running for 1 hour after the actual command fails, giving you time to exec in and inspect.

Real-Time Debugging – Exec Before Crash:
For pods that crash mid-execution:

kubectl debug POD_NAME -it --image=busybox --target=CONTAINER_NAME

This attaches a sidecar debugger container sharing the same namespaces (network, PID, filesystem) as your crashing container. You can inspect files, check network connectivity, or examine processes while the actual pod is still running.

Catch Initialization Failures:
If your init containers crash:

kubectl logs POD_NAME -c INIT_CONTAINER_NAME --previous

Init containers run sequentially before main containers. If one fails, the entire pod fails to start. The –previous flag works here too.

Related posts:

Kubernetes: Force Delete Stuck Pods in Terminating State Instantly

Kubernetes — Liveness Probes Can Kill Healthy Pods

Kubernetes Pods Restart Without Logs

Post Views: 5

Post navigation

Recover Deleted WordPress Posts from Database (Even Without Backups)
Reduce Docker Image Sizes by 10x with Multi-Stage Builds

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

February 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
232425262728  
« Jan    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (934)
  • How to add default value for Entity Framework migrations for DateTime and Bool (830)
  • Get the First and Last Word from a String or Sentence in SQL (822)
  • How to select distinct rows in a datatable in C# (799)
  • How to make theater mode the default for Youtube (708)
  • Add Constraint to SQL Table to ensure email contains @ (571)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (552)
  • Average of all values in a column that are not zero in SQL (517)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (473)
  • Find numbers with more than two decimal places in SQL (436)

Recent Posts

  • Windows 11 Snap Layouts: Organize 10+ Windows Without Alt+Tab Hell
  • Kubernetes Ingress: Expose Multiple Services Through One Load Balancer
  • Docker Compose: Launch Full Stack Apps with One Command (Node + Redis + Postgres)
  • Visual Studio Live Share: Real-time Collaborative Coding Like Google Docs
  • WordPress REST API: Turn Your Site into a Headless CMS for React/Vue Apps
  • WordPress Custom Post Types: Build Real Estate, Job Board, or Product Catalogs
  • Photoshop Batch Processing: Edit 1000 Images While You Sleep
  • Photoshop Smart Objects: Edit Once, Update Everywhere Without Quality Loss
  • Windows 11 WSL2: Run Linux at Native Speed Without Dual Boot
  • WordPress Speed Hack: How Lazy Loading Images Cuts Page Load Time in Half

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (934)
  • How to add default value for Entity Framework migrations for DateTime and Bool (830)
  • Get the First and Last Word from a String or Sentence in SQL (822)
  • How to select distinct rows in a datatable in C# (799)
  • How to make theater mode the default for Youtube (708)

Recent Posts

  • Windows 11 Snap Layouts: Organize 10+ Windows Without Alt+Tab Hell
  • Kubernetes Ingress: Expose Multiple Services Through One Load Balancer
  • Docker Compose: Launch Full Stack Apps with One Command (Node + Redis + Postgres)
  • Visual Studio Live Share: Real-time Collaborative Coding Like Google Docs
  • WordPress REST API: Turn Your Site into a Headless CMS for React/Vue Apps

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com