Skip to main content
⚑ OpenDevDocs
Kubernetes
devopskubernetesGuideIntermediate

Kubernetes Logs, Exec & Debugging

Stream container logs, debug previous crashed containers, open interactive shells with kubectl exec, copy files, and port-forward to local ports.

Verified: 2026-10-01
Tested on:kubernetes 1.31
Platforms:
all
#kubernetes#logs#exec#debugging#port-forward

Kubernetes Logs, Exec & Debugging

Diagnosing running applications and investigating runtime errors requires using kubectl logs, exec, and port-forward.


1. Inspecting Container Logs (kubectl logs)

Stream Live Logs

kubectl logs -f api-deployment-559d7b489d-abc12

View Logs from a Crashed Container Instance (--previous)

When a container dies and enters CrashLoopBackOff, the normal logs command shows the new fresh container. Use --previous to see why the previous container crashed:

kubectl logs -p api-deployment-559d7b489d-abc12

Target a Specific Container in Multi-Container Pods

kubectl logs api-pod -c sidecar-logger

Stream Combined Logs Across All Pods with a Label

kubectl logs -f -l app=backend-api --all-containers=true --tail=50

2. Interactive Terminal Access (kubectl exec)

Open an interactive shell inside a running container:

# Standard interactive shell
kubectl exec -it api-deployment-559d7b489d-abc12 -- /bin/sh

# If bash is installed
kubectl exec -it api-deployment-559d7b489d-abc12 -- /bin/bash

# Run one-off command without interactive shell
kubectl exec api-deployment-559d7b489d-abc12 -- env

3. Port Forwarding (kubectl port-forward)

Access internal cluster services and pods directly on your local localhost workstation without exposing them publicly:

# Forward local port 8080 to internal Pod port 3000
kubectl port-forward pod/web-app-abc12 8080:3000

# Forward local port 5432 to PostgreSQL Service
kubectl port-forward svc/postgres-service 5432:5432

Now visit http://localhost:8080 in your local browser or connect your local database GUI.


4. Copying Files (kubectl cp)

# Copy local file into running Pod container
kubectl cp ./config.json api-pod:/app/config.json

# Copy log file from Pod to local machine
kubectl cp api-pod:/var/log/app.log ./downloaded-app.log

On this page