> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Three kubectl Commands to Save Time

> Guide to three kubectl commands that speed Kubernetes debugging and manifest authoring by showing previous logs, chronological events, and API documentation in the terminal.

In this short guide you'll find three kubectl commands that frequently speed up debugging and manifest authoring in Kubernetes: `kubectl logs --previous` (inspect crashed containers), `kubectl get events --sort-by=.metadata.creationTimestamp` (see recent events chronologically), and `kubectl explain` (read API docs from the terminal). Each command includes examples and troubleshooting tips so you can apply them quickly.

***

## Quick summary

| Command | Purpose | Example |
| - | - | - |
| `kubectl logs --previous` (`-p`) | Retrieve logs from the previous (crashed) container instance — very useful for CrashLoopBackOff investigations | `kubectl logs orders-0 --previous` |
| `kubectl get events --sort-by=.metadata.creationTimestamp` | List cluster events in chronological order to correlate what happened and when | `kubectl get events --sort-by=.metadata.creationTimestamp` |
| `kubectl explain` | Show API resource documentation and field descriptions directly in your terminal | `kubectl explain deployment.spec.strategy.type` |

***

## 1) Inspect the previous container's logs: kubectl logs --previous

When a container fails and Kubernetes launches a replacement (for example during a CrashLoopBackOff), `kubectl logs` with no extra flags returns logs from the currently running container instance. To retrieve logs from the previous (crashed) instance, add `--previous` (or `-p`).

Example: view the previous logs for pod `orders-0`:

```bash theme={null}
$ kubectl logs orders-0 --previous
```

Why this helps:

* The previous container often contains the stack trace, startup error, or dependency connection problem that caused the crash.
* Without `--previous`, you may only see logs from the fresh instance and miss the root cause.

Illustrative outputs:

Previous (crashed) instance:

```bash theme={null}
$ kubectl logs orders-0 --previous
connecting to postgres at db:5432
panic: dial tcp db:5432: connect: connection refused
```

Current (running) instance:

```bash theme={null}
$ kubectl logs orders-0
connecting to postgres at db:5432
```

If your pod has multiple containers, target the container explicitly:

```bash theme={null}
$ kubectl logs orders-0 -c <container-name> --previous
```

If you see a CrashLoopBackOff, check `kubectl logs --previous` first — it very often contains the immediate cause (stack trace, error connecting to a dependency, or a misconfiguration).

***

## 2) See recent Kubernetes events in chronological order

`kubectl get events` surfaces cluster-level events (scheduling decisions, image pulls, probe failures, restarts, etc.). Sorting by the `.metadata.creationTimestamp` lets you read events in chronological order and correlate them with pod lifecycle actions.

Command:

```bash theme={null}
$ kubectl get events --sort-by=.metadata.creationTimestamp
```

Sample output:

```text theme={null}
LAST SEEN   TYPE      REASON      OBJECT
3m          Normal    Scheduled   pod/orders-0
3m          Normal    Pulled      pod/orders-0
3m          Normal    Started     pod/orders-0
2m          Warning   Unhealthy   pod/orders-0
2m          Normal    Killing     pod/orders-0
```

How to interpret common event types:

* Normal / Scheduled, Pulled, Started: lifecycle progress — pod scheduled, image pulled, container started.
* Warning / Unhealthy: probe failures (readiness/liveness) or other issues — often precede a Kubernetes-triggered restart or kill.
* Normal / Killing: Kubernetes terminated the container (for example after failed probes or because it wanted to recreate it).

Use events to determine whether the application crashed on its own or was killed due to failed health checks, resource constraints, or scheduling issues. If you see probe failures, start by reviewing probe config and application startup time.

***

## 3) Inspect API docs in the terminal: kubectl explain

`kubectl explain` prints the server-side documentation for API resources and fields. It’s especially useful while authoring manifests or verifying valid field names and enums without leaving your terminal.

Example: check allowed values for a Deployment strategy type:

```bash theme={null}
$ kubectl explain deployment.spec.strategy.type
```

Typical output (abbreviated):

```text theme={null}
FIELD: type <string>
ENUM:
  Recreate
  RollingUpdate

DESCRIPTION:
StrategyType defines the strategy used to replace old Pods by new ones.
```

You can use this information to write correct YAML, for example:

```yaml theme={null}
apiVersion: apps/v1
kind: Deployment
spec:
  strategy:
    type: RollingUpdate  # or Recreate
```

Explore nested fields, container fields, and lists:

```bash theme={null}
$ kubectl explain deployment.spec.template.spec.containers
$ kubectl explain deployment.spec.template.spec.containers.ports
```

Tip: `kubectl explain` reflects the API server version you are connected to, so it matches the fields available in your cluster.

***

## Putting the three together

* When a pod is failing: check `kubectl logs --previous` to capture the crash output, then use `kubectl get events --sort-by=.metadata.creationTimestamp` to see whether Kubernetes killed the pod (e.g., probe failures). If the issue looks like a manifest problem, validate field names and options with `kubectl explain`.
* These commands are lightweight, require no additional tooling, and are available in any environment with kubectl configured.

***

## Links and references

* [kubectl cheat sheet](https://kubernetes.io/docs/reference/kubectl/cheatsheet/)
* [Kubernetes Events documentation](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.26/#event-v1-core)
* [kubectl explain documentation](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#explain)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/devops-interview-prep/module/60905e58-3a8e-4423-9743-081b4959f0a0/lesson/085c151b-c048-4cc4-a472-ec53b6443523)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.