XenonStack

Kubernetes

XenonStack Blog Debug Application in Kubernetes: kubectl Guide

and | Published | Updated

Key takeaway

  • Debug an application running in Kubernetes: kubectl logs, exec, ephemeral containers, Pod copies and Telepresence, plus the access controls to set.

To debug an application running in Kubernetes, work from the least invasive tool to the most invasive. Read the Pod state and events with kubectl describe, read the logs (including the previous crashed container with --previous), run a command with kubectl exec, and only then attach an ephemeral debug container or debug a copy of the Pod with kubectl debug.

This guide is for platform leads, architects and engineering heads who want a repeatable debugging path and clear limits on who may touch production. It covers the built-in tools, when local development against a cluster (Telepresence) fits, the access controls that matter, and how to keep debugging sessions auditable. For the cluster internals behind these tools, see our guide to Kubernetes architecture and its components.

How do you debug an application in Kubernetes, step by step?

The Kubernetes documentation starts debugging with the Pod itself: check its current state and recent events first, then move to logs and an interactive shell. A practical order is below.

  1. Describe the Pod. kubectl describe pod shows the Pod state and recent events.
  2. Read the logs. If a container has crashed, kubectl logs POD -c CONTAINER --previous returns the previous container's crash log.
  3. Exec into the container. If the image includes debugging utilities, kubectl exec runs commands inside a specific container; -c can be omitted for single-container Pods.
  4. Attach an ephemeral container. Use kubectl debug when exec is not enough (see below).
  5. Debug a copy. Use --copy-to so the original Pod keeps serving.
  6. Debug the node. If none of these work, kubectl debug node/NAME starts a Pod on the node.

Sources: Debug Running Pods and Debug Pods in the Kubernetes documentation.

Which debugging approach fits which situation?

ApproachWhen it fitsCurrent status and caveat
kubectl logs / --previousThe container crashed or restarted and you need its last outputStandard kubectl command; --previous reads the prior container's crash log
kubectl execThe image already contains a shell and debugging toolsNot enough for crashed containers or images without tools
Ephemeral container (kubectl debug --target)The container has crashed, or the image has no debugging utilities (for example distroless images)Stable since Kubernetes v1.25; needs permission on the pods/ephemeralcontainers subresource
Pod copy (kubectl debug --copy-to)You want to change the command, image or process sharing without disturbing the original PodCreates a separate Pod, so its behaviour can differ from the original
Node debug (kubectl debug node/)The fault may sit on the node, not in the PodNode root filesystem mounted at /host; Pod is not privileged by default
TelepresenceA developer wants to run one service locally while the rest stays in the clusterSeparate tool, not part of Kubernetes; current docs describe version 2.33

Sources: Ephemeral Containers, Debug Running Pods and Telepresence documentation.

Two-column diagram mapping four debugging situations to tools: crash to logs --previous, no tools to ephemeral container, live Pod to a copy, node fault to node debug.

What are ephemeral containers and when should you use them?

Ephemeral containers are a special type of container that runs temporarily in an existing Pod for user-initiated actions such as troubleshooting. Pods cannot gain a normal container after creation, so this is the supported way to bring debugging tools into a running Pod. The Kubernetes documentation says they are useful when kubectl exec is insufficient because a container has crashed or its image does not include debugging utilities, such as distroless images.

How do you start one?

Run kubectl debug -it POD --image=busybox:1.28 --target=CONTAINER. The --target flag targets the process namespace of another container, so you can see its processes. The container runtime must support it; if not, the ephemeral container may start with an isolated process namespace and ps will not show the other container's processes.

What are the limits?

Ephemeral containers are never restarted automatically, cannot be changed or removed once added, and cannot have ports, liveness or readiness probes or resource settings. They are not for building applications.

How do you debug without disturbing production?

Use --copy-to. The documentation describes it as creating a copy of the Pod with configuration values changed to aid debugging, for example kubectl debug myapp -it --image=ubuntu --share-processes --copy-to=myapp-debug. To change the command of a specific container, name it with --container; otherwise kubectl debug creates a new container for the command.

If the problem looks like a node fault, kubectl debug node/mynode -it --image=ubuntu creates an interactive Pod on that node. The node root filesystem is mounted at /host. The container runs in the host IPC, network and PID namespaces, but the Pod is not privileged, so some process information may be unreadable. For a privileged session, the docs say to create the Pod manually or use --profile=sysadmin.

How do you develop and debug a service locally against a cluster?

Debugging inside a large cluster can be slow, because every change means building an image and redeploying. Telepresence addresses this by connecting your workstation to the cluster so you can run a service locally, with your own IDE and debugger, while the rest of the cluster keeps interacting with it. Its documentation describes an intercept as rerouting traffic for a service port while the remote container keeps running, and lists replace, wiretap and ingest as other ways to attach to a workload.

This suits a developer working on one service among many. It is not a production debugging tool; treat it as a development-time option and apply the same access rules you apply to the cluster. For how services are rolled out and rolled back, see Kubernetes deployment tools and best practices.

What should enterprise teams govern when debugging in Kubernetes?

A debug session is privileged access to a running workload. Three controls matter.

  • Access. Adding an ephemeral container requires permission on the pods/ephemeralcontainers subresource. The documentation states that being authorised to patch a Pod does not allow a user to add ephemeral containers to it, so grant this deliberately and review it. See Kubernetes security automation for the wider control set.
  • Evidence. Record who attached to which Pod, why, and what changed. Operations teams that run production and standby clusters daily, checking node readiness, Pod status and workload health, find that probe failures and memory spikes are the usual starting points, so keep those signals alongside the debug record.
  • Observability first. Logs, metrics and traces should answer most questions before anyone opens a shell. See cloud native observability tools and full-stack observability.

Where AI agents are part of the workload, the same discipline applies to their decisions. ElixirClaw is an agentic OS platform to build, orchestrate and govern AI agents and automation workflows while keeping humans in control, and the Decision Review Agent reviews past decisions across agents, teams and time periods to find inconsistent treatment and policy gaps, leaving humans to decide what is correct.

What are the trade-offs and when not to debug in place?

OptionBenefitTrade-off
Exec into the live PodFastest view of real stateImage may lack tools; changes can affect live traffic
Ephemeral containerBrings tools without rebuilding the imageCannot be removed or restarted; needs specific permission
Pod copyOriginal keeps servingCopy may not reproduce the fault
Local run with TelepresenceFast inner loop with your own debuggerExtra tool to install and secure; development use
Node debugReaches node-level causesBroad host namespaces; treat as high privilege

Do not debug in place when the Pod is serving customers and a copy will reproduce the fault, or when audit rules forbid interactive access to production. Prefer logs, metrics and a copy, and keep shell access for incidents with a named approver.

What is a sensible adoption sequence?

  1. Standardise on kubectl describe, logs and --previous for first-line triage.
  2. Allow kubectl debug with ephemeral containers for named roles only, via the pods/ephemeralcontainers permission.
  3. Publish approved debug images and a rule that production debugging uses a Pod copy by default.
  4. Add observability so most faults are diagnosed without a shell.
  5. Log every debug session and review the log regularly.

Debugging a Pod also depends on how it was deployed and how the control plane schedules it; our guides to Kubernetes deployment tools and best practices and Kubernetes architecture and its components cover that ground.

To map one decision your agents should make in your cluster, and the context and policy it depends on, book a Context Workshop. See what happens in the workshop.

Frequently asked questions

How do I debug a crashing Pod in Kubernetes?

Start with kubectl describe pod for state and events, then read the previous container's crash log with kubectl logs POD -c CONTAINER --previous. If the image has no debugging tools, attach an ephemeral container with kubectl debug.

What is an ephemeral container?

A temporary container that runs in an existing Pod for troubleshooting. It is stable since Kubernetes v1.25, is never restarted automatically, and cannot be changed or removed once added.

What does kubectl debug --copy-to do?

It creates a copy of the Pod with configuration changed to aid debugging, such as a different command or shared process namespace, so the original Pod is not disturbed.

Who is allowed to add an ephemeral container?

Users need permission on the pods/ephemeralcontainers subresource. Being authorised to patch a Pod is not enough.

When should I use Telepresence?

When a developer wants to run one service locally with their own IDE and debugger while the rest of the application stays in the cluster. It is a separate tool, suited to development rather than production debugging.

Follow XenonStack on LinkedIn: XenonStack on LinkedIn for new guides on agentic AI, data and platform engineering.

Table of Contents

navdeep-singh-gill

Navdeep Singh Gill

Global CEO and Founder of XenonStack

LinkedIn profile

Navdeep Singh Gill is serving as Chief Executive Officer and Product Architect at XenonStack. He holds expertise in building SaaS Platform for Decentralised Big Data management and Governance, AI Marketplace for Operationalising and Scaling. His incredible experience in AI Technologies and Big Data Engineering thrills him to write about different use cases and its approach to solutions.

Get the latest articles in your inbox

Subscribe Now

Interested in Solving your Challenges with XenonStack Team
Get Started

Get Started with your requirements and primary focus, that will help us to make your solution

Please Select your Industry
Banking
Fintech
Payment Providers
Wealth Management
Discrete Manufacturing
Semiconductor
Machinery Manufacturing / Automation
Appliances / Electrical / Electronics
Elevator Manufacturing
Defense & Space Manufacturing
Computers & Electronics / Industrial Machinery
Motor Vehicle Manufacturing
Food and Beverages
Distillery & Wines
Beverages
Shipping
Logistics
Mobility (EV / Public Transport)
Energy & Utilities
Hospitality
Digital Gaming Platforms
SportsTech with AI
Public Safety - Explosives
Public Safety - Firefighting
Public Safety - Surveillance
Public Safety - Others
Media Platforms
City Operations
Airlines & Aviation
Defense Warfare & Drones
Robotics Engineering
Drones Manufacturing
AI Labs for Colleges
AI MSP / Quantum / AGI Institutes
Retail Apparel and Fashion

Proceed Next