Specimen No. 1554 · Habitat H4 · DevOps & IT

Kubernetes from Zero · Workloads & Troubleshooting 4 of 14 · Lesson 16

Kubernetes from Zero, part 16: init containers and sidecars

Run setup steps before your app with init containers, add a native sidecar that starts first and stops last, and debug a Pod that has no shell.

WILDNESS1 / 5 · TAMED
Verified: Every command ran on a kind v0.33.0 cluster (node v1.37.0, kubectl v1.37.2) on 10 Nov 2026Only claimed: Start and stop ordering rules, probe support and ephemeral container limits come from the Kubernetes docs
A cut-paper sand sailing ship with cream sails rides on layered blue paper waves with a small coral rowing dinghy tied alongside, two crates waiting on a pier and a slate-blue ship's wheel above.
Generated cover art. Not a photo.

In part 15 you ran work on every node and work that finishes. Every Pod so far had one container. This episode puts several containers in one Pod and controls when each one runs. Init containers do setup work and must finish before your app starts. Sidecars are helpers that start before your app, run next to it, and stop after it. Ephemeral containers are debug tools you add to a running Pod when its own image has no shell. You need the kind cluster from part 3 and the k8s-from-zero namespace as your context namespace (part 6). We tested on kind v0.33.0 (node image v1.37.0) with kubectl v1.37.2.

What you will build

You will build a web Pod whose page is written by two init containers in order, and watch its STATUS column count through them. You will block a Pod on a missing Service, then break an init container on purpose. Next you add a log shipper as a native sidecar, and compare two Jobs: one with an old-style helper that never lets the Job finish, one with a native sidecar that does. Last, you open a debug container inside a Pod whose image has no shell at all.

A timeline of the lab Pod web-init: fetch-config runs and exits 0, then render-page runs and exits 0, and only then does the highlighted web container start; the STATUS column moves from Init:0/2 to Init:1/2 to PodInitializing to Running.

Init containers run first, one at a time

An init container is a container listed under spec.initContainers instead of spec.containers. The kubelet runs them in the order you list them, one at a time, and each must exit with code 0 before the next starts. Only when the last one has finished do the app containers start. If one fails, the kubelet restarts it (with the Pod’s restartPolicy of Always or OnFailure) and the app never starts until it succeeds. With restartPolicy: Never, a failed init container fails the whole Pod.

Typical jobs for an init container:

  • wait until something the app needs exists, such as a Service name in DNS
  • fetch or render a config file into a shared volume
  • run a database migration before the new version starts
  • set file permissions on a volume, using tools the app image does not ship

Init containers can use a different image from the app, so the app image stays small. They share the Pod’s volumes, so an init container can write a file into an emptyDir (part 13) that the app later reads. They do not support livenessProbe, readinessProbe, startupProbe or lifecycle: finishing is the only signal they give. While they run, kubectl get pod shows Init:N/M, where N counts the ones that finished.

Write init containers so they can run twice. If the Pod restarts, every init container runs again from the start, and the docs warn that output files in an emptyDir may already exist.

Native sidecars start first and stop last

A sidecar is a helper container that runs for the whole life of the Pod: a log shipper, a proxy, a certificate refresher. For years people put helpers in spec.containers next to the app. That works for a long-running web server, but it has two problems. Nothing makes the helper start before the app, and in a Job the Pod only completes when every container exits, so a helper that runs forever keeps the Job running forever.

Kubernetes now has native sidecars: an entry under initContainers with restartPolicy: Always. The feature is stable since v1.33 and on by default since v1.29. Such a container starts in its place in the init order, and the kubelet does not wait for it to exit. It waits for it to start (or for its startupProbe to pass) and then moves on to the next init container or to the app. It keeps running while the app runs, is restarted if it crashes, and can have probes; its readiness counts toward the Pod’s Ready condition.

A timeline from the lab: the log-shipper sidecar starts at 21:10:47 UTC, its startupProbe passes, the app starts at 21:10:53, both run side by side, and on shutdown the app stops first and the sidecar last. Below, a Job with the helper in containers stays Running 0/1, highlighted, while a Job with a native sidecar reaches Complete.

On the way out the order reverses. When the Pod stops, the app containers stop first; sidecars are stopped after them, in the opposite order of their place in the spec. In a Job, once the main containers have finished, the kubelet stops the sidecars, and the Job can complete. One catch from our lab: a sidecar that ignores SIGTERM is killed only after the grace period, 30 seconds by default, so your Job takes 30 seconds longer than it should.

Three patterns for helpers in a Pod

All containers in a Pod share one network namespace, so they reach each other on localhost, and they can mount the same volumes. They are always scheduled together on one node. Three classic patterns use that.

Three Pods side by side. Sidecar: the app writes logs into a shared emptyDir and a log shipper sends them to a log backend. Ambassador: the app talks to localhost:6379 and a proxy in the Pod forwards to the real remote database. Adapter: the app exposes status in its own format and an adapter serves it as Prometheus metrics on /metrics.

  • Sidecar in the narrow sense: extends the app without changing it. Our log shipper reads the app’s log file from a shared volume.
  • Ambassador: a local proxy for outbound calls. The app always talks to localhost, and the ambassador decides where the traffic really goes. Service meshes (part 67) build on this idea.
  • Adapter: translates the app’s output into a standard shape, for example a status page into Prometheus metrics.

Put a helper in the same Pod only when it must share the Pod’s lifecycle, network or files. Two parts that can scale on their own belong in two Deployments joined by a Service (part 8).

Ephemeral containers: a shell for a Pod without one

Small production images often ship no shell and no tools. That is good for security and bad for kubectl exec. An ephemeral container is a temporary container you add to a running Pod with kubectl debug. It can use any image, such as busybox, and with --target it joins the process namespace of one container, so ps shows that container’s processes.

Ephemeral containers have limits. They get no resource guarantees, are never restarted, and may not have ports or probes. You cannot change or remove one after you add it; it stays in the Pod spec until the Pod is deleted. Adding one needs permission to patch the pods/ephemeralcontainers subresource (it is not in the default edit or admin roles), which RBAC (part 20) controls. If you need a full copy to experiment on, kubectl debug --copy-to creates a new Pod and leaves the original alone.

Try it: init steps, a sidecar and a debug container

Every command runs in the k8s-from-zero namespace. The images are busybox:1.37, nginx:1.29-alpine and registry.k8s.io/pause:3.10 (kind nodes already have the last one).

1. Two init containers fill a shared volume. Save as init-order.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: web-init
  labels:
    app: web-init
spec:
  initContainers:
    - name: fetch-config
      image: busybox:1.37
      command: ["sh", "-c", "echo 'step 1: config fetched' > /work/index.html; sleep 5"]
      volumeMounts:
        - name: work
          mountPath: /work
    - name: render-page
      image: busybox:1.37
      command: ["sh", "-c", "echo 'step 2: page rendered' >> /work/index.html; sleep 5"]
      volumeMounts:
        - name: work
          mountPath: /work
  containers:
    - name: web
      image: nginx:1.29-alpine
      ports:
        - containerPort: 80
      volumeMounts:
        - name: work
          mountPath: /usr/share/nginx/html
  volumes:
    - name: work
      emptyDir: {}

Apply it and watch:

bash
kubectl apply -f init-order.yaml
kubectl get pod web-init -w

Explain this command

kubectl apply -f init-order.yaml
kubectl get pod web-init -w
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. init-order.yaml The file named by --filename. apply creates the objects or updates them to match this file.
Explain this command
kubectl apply -f init-order.yaml
kubectl get pod web-init -w
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. init-order.yaml The file named by --filename. apply creates the objects or updates them to match this file.

Explain this command

kubectl apply -f init-order.yaml
kubectl get pod web-init -w
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. web-init The object name.
  5. -w Short form of --watch. After listing the requested object, watch for changes.
Explain this command
kubectl apply -f init-order.yaml
kubectl get pod web-init -w
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. web-init The object name.
  5. -w Short form of --watch. After listing the requested object, watch for changes.
NAME       READY   STATUS     RESTARTS   AGE
web-init   0/1     Init:0/2   0          1s
web-init   0/1     Init:1/2   0          7s
web-init   0/1     PodInitializing   0          13s
web-init   1/1     Running           0          13s

Press Ctrl+C. The start and finish times prove the order:

bash
kubectl get pod web-init -o jsonpath='{range .status.initContainerStatuses[*]}{.name}  {.state.terminated.reason}  exit={.state.terminated.exitCode}  {.state.terminated.startedAt} -> {.state.terminated.finishedAt}{"\n"}{end}'
kubectl get pod web-init -o jsonpath='{.status.containerStatuses[0].state.running.startedAt}{"\n"}'
kubectl exec web-init -c web -- cat /usr/share/nginx/html/index.html

Explain this command

kubectl get pod web-init -o jsonpath='{range .status.initContainerStatuses[*]}{.name}  {.state.terminated.reason}  exit={.state.terminated.exitCode}  {.state.terminated.startedAt} -> {.state.terminated.finishedAt}{"\n"}{end}'
kubectl get pod web-init -o jsonpath='{.status.containerStatuses[0].state.running.startedAt}{"\n"}'
kubectl exec web-init -c web -- cat /usr/share/nginx/html/index.html
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. web-init The object name.
  5. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  6. jsonpath='{range .status.initContainerStatuses[*]}{.name} {.state.terminated.reason} exit={.state.terminated.exitCode} {.state.terminated.startedAt} -> {.state.terminated.finishedAt}{"\n"}{end}' A jsonpath output format. The get docs say jsonpath prints the fields the expression selects. The lesson uses this path to print the fields named in the command.
Explain this command
kubectl get pod web-init -o jsonpath='{range .status.initContainerStatuses[*]}{.name}  {.state.terminated.reason}  exit={.state.terminated.exitCode}  {.state.terminated.startedAt} -> {.state.terminated.finishedAt}{"\n"}{end}'
kubectl get pod web-init -o jsonpath='{.status.containerStatuses[0].state.running.startedAt}{"\n"}'
kubectl exec web-init -c web -- cat /usr/share/nginx/html/index.html
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. web-init The object name.
  5. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  6. jsonpath='{range .status.initContainerStatuses[*]}{.name} {.state.terminated.reason} exit={.state.terminated.exitCode} {.state.terminated.startedAt} -> {.state.terminated.finishedAt}{"\n"}{end}' A jsonpath output format. The get docs say jsonpath prints the fields the expression selects. The lesson uses this path to print the fields named in the command.

Explain this command

kubectl get pod web-init -o jsonpath='{range .status.initContainerStatuses[*]}{.name}  {.state.terminated.reason}  exit={.state.terminated.exitCode}  {.state.terminated.startedAt} -> {.state.terminated.finishedAt}{"\n"}{end}'
kubectl get pod web-init -o jsonpath='{.status.containerStatuses[0].state.running.startedAt}{"\n"}'
kubectl exec web-init -c web -- cat /usr/share/nginx/html/index.html
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. web-init The object name.
  5. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  6. jsonpath='{.status.containerStatuses[0].state.running.startedAt}{"\n"}' A jsonpath output format. The get docs say jsonpath prints the fields the expression selects. The lesson uses this path to print the fields named in the command.
Explain this command
kubectl get pod web-init -o jsonpath='{range .status.initContainerStatuses[*]}{.name}  {.state.terminated.reason}  exit={.state.terminated.exitCode}  {.state.terminated.startedAt} -> {.state.terminated.finishedAt}{"\n"}{end}'
kubectl get pod web-init -o jsonpath='{.status.containerStatuses[0].state.running.startedAt}{"\n"}'
kubectl exec web-init -c web -- cat /usr/share/nginx/html/index.html
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. web-init The object name.
  5. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  6. jsonpath='{.status.containerStatuses[0].state.running.startedAt}{"\n"}' A jsonpath output format. The get docs say jsonpath prints the fields the expression selects. The lesson uses this path to print the fields named in the command.

Explain this command

kubectl get pod web-init -o jsonpath='{range .status.initContainerStatuses[*]}{.name}  {.state.terminated.reason}  exit={.state.terminated.exitCode}  {.state.terminated.startedAt} -> {.state.terminated.finishedAt}{"\n"}{end}'
kubectl get pod web-init -o jsonpath='{.status.containerStatuses[0].state.running.startedAt}{"\n"}'
kubectl exec web-init -c web -- cat /usr/share/nginx/html/index.html
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  3. web-init The object name.
  4. -c Short form of --container. It names which container in the Pod receives this request.
  5. web The container name passed to this flag.
  6. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  7. cat Print the contents of a file.
  8. /usr/share/nginx/html/index.html The file path printed by cat inside the container.
Explain this command
kubectl get pod web-init -o jsonpath='{range .status.initContainerStatuses[*]}{.name}  {.state.terminated.reason}  exit={.state.terminated.exitCode}  {.state.terminated.startedAt} -> {.state.terminated.finishedAt}{"\n"}{end}'
kubectl get pod web-init -o jsonpath='{.status.containerStatuses[0].state.running.startedAt}{"\n"}'
kubectl exec web-init -c web -- cat /usr/share/nginx/html/index.html
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  3. web-init The object name.
  4. -c Short form of --container. It names which container in the Pod receives this request.
  5. web The container name passed to this flag.
  6. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  7. cat Print the contents of a file.
  8. /usr/share/nginx/html/index.html The file path printed by cat inside the container.
fetch-config  Completed  exit=0  2026-11-10T21:07:37Z -> 2026-11-10T21:07:42Z
render-page  Completed  exit=0  2026-11-10T21:07:43Z -> 2026-11-10T21:07:48Z
2026-11-10T21:07:49Z
step 1: config fetched
step 2: page rendered

2. Wait for a Service. This init container loops until the name db resolves. Save as wait-for-db.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: app-waits
spec:
  initContainers:
    - name: wait-for-db
      image: busybox:1.37
      command: ["sh", "-c", "until nslookup db.k8s-from-zero.svc.cluster.local; do echo waiting for db; sleep 2; done"]
  containers:
    - name: app
      image: busybox:1.37
      command: ["sh", "-c", "echo app started; sleep 3600"]
bash
kubectl apply -f wait-for-db.yaml
kubectl get pod app-waits
kubectl logs app-waits -c wait-for-db --tail=4

Explain this command

kubectl apply -f wait-for-db.yaml
kubectl get pod app-waits
kubectl logs app-waits -c wait-for-db --tail=4
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. wait-for-db.yaml The file named by --filename. apply creates the objects or updates them to match this file.
Explain this command
kubectl apply -f wait-for-db.yaml
kubectl get pod app-waits
kubectl logs app-waits -c wait-for-db --tail=4
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. wait-for-db.yaml The file named by --filename. apply creates the objects or updates them to match this file.

Explain this command

kubectl apply -f wait-for-db.yaml
kubectl get pod app-waits
kubectl logs app-waits -c wait-for-db --tail=4
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. app-waits The object name.
Explain this command
kubectl apply -f wait-for-db.yaml
kubectl get pod app-waits
kubectl logs app-waits -c wait-for-db --tail=4
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. app-waits The object name.

Explain this command

kubectl apply -f wait-for-db.yaml
kubectl get pod app-waits
kubectl logs app-waits -c wait-for-db --tail=4
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. logs Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.
  3. app-waits The object name.
  4. -c Short form of --container. It names which container in the Pod receives this request.
  5. wait-for-db The container name passed to this flag.
  6. --tail=4 How many lines from the end of the logs to show.
Explain this command
kubectl apply -f wait-for-db.yaml
kubectl get pod app-waits
kubectl logs app-waits -c wait-for-db --tail=4
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. logs Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.
  3. app-waits The object name.
  4. -c Short form of --container. It names which container in the Pod receives this request.
  5. wait-for-db The container name passed to this flag.
  6. --tail=4 How many lines from the end of the logs to show.
NAME        READY   STATUS     RESTARTS   AGE
app-waits   0/1     Init:0/1   0          12s

** server can't find db.k8s-from-zero.svc.cluster.local: NXDOMAIN

waiting for db

Now create the Service it waits for. Save as db-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: db
spec:
  selector:
    app: db
  ports:
    - port: 5432
bash
kubectl apply -f db-service.yaml
kubectl get pod app-waits
kubectl logs app-waits -c app

Explain this command

kubectl apply -f db-service.yaml
kubectl get pod app-waits
kubectl logs app-waits -c app
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. db-service.yaml The file named by --filename. apply creates the objects or updates them to match this file.
Explain this command
kubectl apply -f db-service.yaml
kubectl get pod app-waits
kubectl logs app-waits -c app
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. db-service.yaml The file named by --filename. apply creates the objects or updates them to match this file.

Explain this command

kubectl apply -f db-service.yaml
kubectl get pod app-waits
kubectl logs app-waits -c app
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. app-waits The object name.
Explain this command
kubectl apply -f db-service.yaml
kubectl get pod app-waits
kubectl logs app-waits -c app
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. app-waits The object name.

Explain this command

kubectl apply -f db-service.yaml
kubectl get pod app-waits
kubectl logs app-waits -c app
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. logs Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.
  3. app-waits The object name.
  4. -c Short form of --container. It names which container in the Pod receives this request.
  5. app The container name passed to this flag.
Explain this command
kubectl apply -f db-service.yaml
kubectl get pod app-waits
kubectl logs app-waits -c app
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. logs Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.
  3. app-waits The object name.
  4. -c Short form of --container. It names which container in the Pod receives this request.
  5. app The container name passed to this flag.
service/db created
NAME        READY   STATUS    RESTARTS   AGE
app-waits   1/1     Running   0          20s
app started

The Service has no Pods behind it, and that is fine here: the check only needs the name. A real check would also try to connect.

3. Break an init container. Save as init-fails.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: init-fails
spec:
  initContainers:
    - name: migrate
      image: busybox:1.37
      command: ["sh", "-c", "echo 'migration failed: table users missing'; exit 1"]
  containers:
    - name: app
      image: busybox:1.37
      command: ["sh", "-c", "sleep 3600"]
bash
kubectl apply -f init-fails.yaml
kubectl get pod init-fails
kubectl logs init-fails -c migrate
kubectl get events --field-selector involvedObject.name=init-fails --sort-by=.lastTimestamp | tail -1

Explain this command

kubectl apply -f init-fails.yaml
kubectl get pod init-fails
kubectl logs init-fails -c migrate
kubectl get events --field-selector involvedObject.name=init-fails --sort-by=.lastTimestamp | tail -1
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. init-fails.yaml The file named by --filename. apply creates the objects or updates them to match this file.
Explain this command
kubectl apply -f init-fails.yaml
kubectl get pod init-fails
kubectl logs init-fails -c migrate
kubectl get events --field-selector involvedObject.name=init-fails --sort-by=.lastTimestamp | tail -1
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. init-fails.yaml The file named by --filename. apply creates the objects or updates them to match this file.

Explain this command

kubectl apply -f init-fails.yaml
kubectl get pod init-fails
kubectl logs init-fails -c migrate
kubectl get events --field-selector involvedObject.name=init-fails --sort-by=.lastTimestamp | tail -1
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. init-fails The object name.
Explain this command
kubectl apply -f init-fails.yaml
kubectl get pod init-fails
kubectl logs init-fails -c migrate
kubectl get events --field-selector involvedObject.name=init-fails --sort-by=.lastTimestamp | tail -1
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. init-fails The object name.

Explain this command

kubectl apply -f init-fails.yaml
kubectl get pod init-fails
kubectl logs init-fails -c migrate
kubectl get events --field-selector involvedObject.name=init-fails --sort-by=.lastTimestamp | tail -1
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. logs Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.
  3. init-fails The object name.
  4. -c Short form of --container. It names which container in the Pod receives this request.
  5. migrate The container name passed to this flag.
Explain this command
kubectl apply -f init-fails.yaml
kubectl get pod init-fails
kubectl logs init-fails -c migrate
kubectl get events --field-selector involvedObject.name=init-fails --sort-by=.lastTimestamp | tail -1
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. logs Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.
  3. init-fails The object name.
  4. -c Short form of --container. It names which container in the Pod receives this request.
  5. migrate The container name passed to this flag.

Explain this command

kubectl apply -f init-fails.yaml
kubectl get pod init-fails
kubectl logs init-fails -c migrate
kubectl get events --field-selector involvedObject.name=init-fails --sort-by=.lastTimestamp | tail -1
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. events The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. --field-selector Filter the list by field. The get docs treat --field-selector as separate from a label selector.
  5. involvedObject.name=init-fails The field query passed to --field-selector: involvedObject.name=init-fails.
  6. --sort-by=.lastTimestamp Sort the list by the field named after the equals sign, written as a JSONPath. Sorting events by .lastTimestamp puts the newest last, so tail shows it.
  7. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  8. tail Shell. Prints the last lines of its input. The lesson uses it to keep the table short.
  9. -1 With tail, print only the last line.
Explain this command
kubectl apply -f init-fails.yaml
kubectl get pod init-fails
kubectl logs init-fails -c migrate
kubectl get events --field-selector involvedObject.name=init-fails --sort-by=.lastTimestamp | tail -1
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. events The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. --field-selector Filter the list by field. The get docs treat --field-selector as separate from a label selector.
  5. involvedObject.name=init-fails The field query passed to --field-selector: involvedObject.name=init-fails.
  6. --sort-by=.lastTimestamp Sort the list by the field named after the equals sign, written as a JSONPath. Sorting events by .lastTimestamp puts the newest last, so tail shows it.
  7. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  8. tail Shell. Prints the last lines of its input. The lesson uses it to keep the table short.
  9. -1 With tail, print only the last line.
NAME         READY   STATUS       RESTARTS      AGE
init-fails   0/1     Init:Error   3 (27s ago)   40s
migration failed: table users missing
2s          Warning   BackOff     pod/init-fails   Back-off restarting failed container migrate in pod init-fails_k8s-from-zero(...)

The app container never started. kubectl logs -c <init-container> is the first thing to read when a Pod is stuck in Init:. Clean up:

bash
kubectl delete pod init-fails app-waits web-init
kubectl delete service db

Explain this command

kubectl delete pod init-fails app-waits web-init
kubectl delete service db
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. pod The resource type.
  4. init-fails The object name.
  5. app-waits The object name.
  6. web-init The object name.
Explain this command
kubectl delete pod init-fails app-waits web-init
kubectl delete service db
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. pod The resource type.
  4. init-fails The object name.
  5. app-waits The object name.
  6. web-init The object name.

Explain this command

kubectl delete pod init-fails app-waits web-init
kubectl delete service db
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. service The resource type.
  4. db The object name.
Explain this command
kubectl delete pod init-fails app-waits web-init
kubectl delete service db
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. service The resource type.
  4. db The object name.

4. A native sidecar. The log shipper is an init container with restartPolicy: Always. We gave it a five-second start and a startupProbe so you can see the app wait for it. Save as native-sidecar.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: app-with-sidecar
spec:
  initContainers:
    - name: log-shipper
      image: busybox:1.37
      restartPolicy: Always
      command: ["sh", "-c", "sleep 5; touch /var/log/app/app.log; tail -F /var/log/app/app.log"]
      startupProbe:
        exec:
          command: ["test", "-f", "/var/log/app/app.log"]
        periodSeconds: 1
        failureThreshold: 30
      volumeMounts:
        - name: logs
          mountPath: /var/log/app
  containers:
    - name: app
      image: busybox:1.37
      command: ["sh", "-c", "i=0; while true; do i=$((i+1)); echo \"request $i served\" >> /var/log/app/app.log; sleep 5; done"]
      volumeMounts:
        - name: logs
          mountPath: /var/log/app
  volumes:
    - name: logs
      emptyDir: {}
bash
kubectl apply -f native-sidecar.yaml
kubectl get pod app-with-sidecar -w

Explain this command

kubectl apply -f native-sidecar.yaml
kubectl get pod app-with-sidecar -w
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. native-sidecar.yaml The file named by --filename. apply creates the objects or updates them to match this file.
Explain this command
kubectl apply -f native-sidecar.yaml
kubectl get pod app-with-sidecar -w
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. native-sidecar.yaml The file named by --filename. apply creates the objects or updates them to match this file.

Explain this command

kubectl apply -f native-sidecar.yaml
kubectl get pod app-with-sidecar -w
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. app-with-sidecar The object name.
  5. -w Short form of --watch. After listing the requested object, watch for changes.
Explain this command
kubectl apply -f native-sidecar.yaml
kubectl get pod app-with-sidecar -w
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. app-with-sidecar The object name.
  5. -w Short form of --watch. After listing the requested object, watch for changes.
NAME               READY   STATUS     RESTARTS   AGE
app-with-sidecar   0/2     Init:0/1   0          0s
app-with-sidecar   0/2     PodInitializing   0          6s
app-with-sidecar   1/2     PodInitializing   0          6s
app-with-sidecar   2/2     Running           0          6s

READY counts the sidecar: 2/2. Check the start times and the shipped lines:

bash
kubectl get pod app-with-sidecar -o jsonpath='{range .status.initContainerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}{range .status.containerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}'
kubectl logs app-with-sidecar -c log-shipper --tail=2

Explain this command

kubectl get pod app-with-sidecar -o jsonpath='{range .status.initContainerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}{range .status.containerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}'
kubectl logs app-with-sidecar -c log-shipper --tail=2
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. app-with-sidecar The object name.
  5. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  6. jsonpath='{range .status.initContainerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}{range .status.containerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}' A jsonpath output format. The get docs say jsonpath prints the fields the expression selects. The lesson uses this path to print the fields named in the command.
Explain this command
kubectl get pod app-with-sidecar -o jsonpath='{range .status.initContainerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}{range .status.containerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}'
kubectl logs app-with-sidecar -c log-shipper --tail=2
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. app-with-sidecar The object name.
  5. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  6. jsonpath='{range .status.initContainerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}{range .status.containerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}' A jsonpath output format. The get docs say jsonpath prints the fields the expression selects. The lesson uses this path to print the fields named in the command.

Explain this command

kubectl get pod app-with-sidecar -o jsonpath='{range .status.initContainerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}{range .status.containerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}'
kubectl logs app-with-sidecar -c log-shipper --tail=2
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. logs Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.
  3. app-with-sidecar The object name.
  4. -c Short form of --container. It names which container in the Pod receives this request.
  5. log-shipper The container name passed to this flag.
  6. --tail=2 How many lines from the end of the logs to show.
Explain this command
kubectl get pod app-with-sidecar -o jsonpath='{range .status.initContainerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}{range .status.containerStatuses[*]}{.name} started {.state.running.startedAt}{"\n"}{end}'
kubectl logs app-with-sidecar -c log-shipper --tail=2
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. logs Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.
  3. app-with-sidecar The object name.
  4. -c Short form of --container. It names which container in the Pod receives this request.
  5. log-shipper The container name passed to this flag.
  6. --tail=2 How many lines from the end of the logs to show.
log-shipper started 2026-11-10T21:10:47Z
app started 2026-11-10T21:10:53Z
request 1 served
request 2 served

The app started six seconds after the sidecar, right after the probe passed.

5. Two Jobs, two kinds of helper. job-old-sidecar.yaml puts the helper in containers:

apiVersion: batch/v1
kind: Job
metadata:
  name: report-old
spec:
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: report
          image: busybox:1.37
          command: ["sh", "-c", "echo building report; sleep 5; echo done"]
        - name: log-shipper
          image: busybox:1.37
          command: ["sh", "-c", "sleep 3600"]

job-native-sidecar.yaml makes it a native sidecar:

apiVersion: batch/v1
kind: Job
metadata:
  name: report-new
spec:
  template:
    spec:
      restartPolicy: Never
      initContainers:
        - name: log-shipper
          image: busybox:1.37
          restartPolicy: Always
          command: ["sh", "-c", "sleep 3600"]
      containers:
        - name: report
          image: busybox:1.37
          command: ["sh", "-c", "echo building report; sleep 5; echo done"]
bash
kubectl apply -f job-old-sidecar.yaml -f job-native-sidecar.yaml
sleep 70; kubectl get jobs
kubectl get pods -l 'batch.kubernetes.io/job-name in (report-old,report-new)'

Explain this command

kubectl apply -f job-old-sidecar.yaml -f job-native-sidecar.yaml
sleep 70; kubectl get jobs
kubectl get pods -l 'batch.kubernetes.io/job-name in (report-old,report-new)'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. job-old-sidecar.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  6. job-native-sidecar.yaml The file named by --filename. apply creates the objects or updates them to match this file.
Explain this command
kubectl apply -f job-old-sidecar.yaml -f job-native-sidecar.yaml
sleep 70; kubectl get jobs
kubectl get pods -l 'batch.kubernetes.io/job-name in (report-old,report-new)'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. job-old-sidecar.yaml The file named by --filename. apply creates the objects or updates them to match this file.
  5. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  6. job-native-sidecar.yaml The file named by --filename. apply creates the objects or updates them to match this file.

Explain this command

kubectl apply -f job-old-sidecar.yaml -f job-native-sidecar.yaml
sleep 70; kubectl get jobs
kubectl get pods -l 'batch.kubernetes.io/job-name in (report-old,report-new)'
  1. sleep Shell. Waits the given number of seconds.
  2. 70 How many seconds sleep waits. The lesson waits 70 seconds so the two Jobs have time to finish or stick.
Explain this command
kubectl apply -f job-old-sidecar.yaml -f job-native-sidecar.yaml
sleep 70; kubectl get jobs
kubectl get pods -l 'batch.kubernetes.io/job-name in (report-old,report-new)'
  1. sleep Shell. Waits the given number of seconds.
  2. 70 How many seconds sleep waits. The lesson waits 70 seconds so the two Jobs have time to finish or stick.

Explain this command

kubectl apply -f job-old-sidecar.yaml -f job-native-sidecar.yaml
sleep 70; kubectl get jobs
kubectl get pods -l 'batch.kubernetes.io/job-name in (report-old,report-new)'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. jobs The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
Explain this command
kubectl apply -f job-old-sidecar.yaml -f job-native-sidecar.yaml
sleep 70; kubectl get jobs
kubectl get pods -l 'batch.kubernetes.io/job-name in (report-old,report-new)'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. jobs The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.

Explain this command

kubectl apply -f job-old-sidecar.yaml -f job-native-sidecar.yaml
sleep 70; kubectl get jobs
kubectl get pods -l 'batch.kubernetes.io/job-name in (report-old,report-new)'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pods The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  5. 'batch.kubernetes.io/job-name in (report-old,report-new)' The label selector passed to -l. It keeps only the objects whose labels match this query.
Explain this command
kubectl apply -f job-old-sidecar.yaml -f job-native-sidecar.yaml
sleep 70; kubectl get jobs
kubectl get pods -l 'batch.kubernetes.io/job-name in (report-old,report-new)'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pods The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  5. 'batch.kubernetes.io/job-name in (report-old,report-new)' The label selector passed to -l. It keeps only the objects whose labels match this query.
NAME         STATUS     COMPLETIONS   DURATION   AGE
report-new   Complete   1/1           39s        70s
report-old   Running    0/1           70s        70s
NAME               READY   STATUS      RESTARTS   AGE
report-new-tk6ql   0/2     Completed   0          70s
report-old-44scw   1/2     NotReady    0          70s

report-old will stay Running for an hour, until its helper’s sleep ends. report-new finished, but it took 39 seconds for five seconds of work. Ask the sidecar why:

bash
kubectl get pod -l batch.kubernetes.io/job-name=report-new -o jsonpath='{range .items[0].status.initContainerStatuses[*]}{.name} {.state.terminated.reason} exit={.state.terminated.exitCode}{"\n"}{end}'

Explain this command

kubectl get pod -l batch.kubernetes.io/job-name=report-new -o jsonpath='{range .items[0].status.initContainerStatuses[*]}{.name} {.state.terminated.reason} exit={.state.terminated.exitCode}{"\n"}{end}'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  5. batch.kubernetes.io/job-name=report-new The label selector passed to -l. It keeps only the objects whose labels match this query.
  6. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  7. jsonpath='{range .items[0].status.initContainerStatuses[*]}{.name} {.state.terminated.reason} exit={.state.terminated.exitCode}{"\n"}{end}' A jsonpath output format. The get docs say jsonpath prints the fields the expression selects. The lesson uses this path to print the fields named in the command.
Explain this command
kubectl get pod -l batch.kubernetes.io/job-name=report-new -o jsonpath='{range .items[0].status.initContainerStatuses[*]}{.name} {.state.terminated.reason} exit={.state.terminated.exitCode}{"\n"}{end}'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  5. batch.kubernetes.io/job-name=report-new The label selector passed to -l. It keeps only the objects whose labels match this query.
  6. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  7. jsonpath='{range .items[0].status.initContainerStatuses[*]}{.name} {.state.terminated.reason} exit={.state.terminated.exitCode}{"\n"}{end}' A jsonpath output format. The get docs say jsonpath prints the fields the expression selects. The lesson uses this path to print the fields named in the command.
log-shipper Error exit=137

Exit 137 means it was killed. sh running as PID 1 ignores SIGTERM, so the kubelet waited out the 30-second grace period and then sent SIGKILL. The fix is a TERM handler. In job-native-sidecar-trap.yaml, copy the native Job, name it report-fast, and change the sidecar command to:

          command: ["sh", "-c", "trap 'exit 0' TERM; sleep 3600 & wait"]
bash
kubectl apply -f job-native-sidecar-trap.yaml
kubectl wait --for=condition=complete job/report-fast --timeout=60s
kubectl get job report-fast

Explain this command

kubectl apply -f job-native-sidecar-trap.yaml
kubectl wait --for=condition=complete job/report-fast --timeout=60s
kubectl get job report-fast
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. job-native-sidecar-trap.yaml The file named by --filename. apply creates the objects or updates them to match this file.
Explain this command
kubectl apply -f job-native-sidecar-trap.yaml
kubectl wait --for=condition=complete job/report-fast --timeout=60s
kubectl get job report-fast
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. job-native-sidecar-trap.yaml The file named by --filename. apply creates the objects or updates them to match this file.

Explain this command

kubectl apply -f job-native-sidecar-trap.yaml
kubectl wait --for=condition=complete job/report-fast --timeout=60s
kubectl get job report-fast
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. wait Wait for a condition on one or more resources.
  3. --for=condition=complete The condition to wait for. Here kubectl waits until the condition named after --for is met.
  4. job/report-fast The resource written as type/name: job/report-fast.
  5. --timeout=60s How long to wait before giving up. This command passes 60s.
Explain this command
kubectl apply -f job-native-sidecar-trap.yaml
kubectl wait --for=condition=complete job/report-fast --timeout=60s
kubectl get job report-fast
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. wait Wait for a condition on one or more resources.
  3. --for=condition=complete The condition to wait for. Here kubectl waits until the condition named after --for is met.
  4. job/report-fast The resource written as type/name: job/report-fast.
  5. --timeout=60s How long to wait before giving up. This command passes 60s.

Explain this command

kubectl apply -f job-native-sidecar-trap.yaml
kubectl wait --for=condition=complete job/report-fast --timeout=60s
kubectl get job report-fast
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. job The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. report-fast The object name.
Explain this command
kubectl apply -f job-native-sidecar-trap.yaml
kubectl wait --for=condition=complete job/report-fast --timeout=60s
kubectl get job report-fast
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. job The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. report-fast The object name.
job.batch/report-fast condition met
NAME          STATUS     COMPLETIONS   DURATION   AGE
report-fast   Complete   1/1           8s         8s

Eight seconds, and the sidecar now exits with code 0. Clean up:

bash
kubectl delete job report-old report-new report-fast
kubectl delete pod app-with-sidecar

Explain this command

kubectl delete job report-old report-new report-fast
kubectl delete pod app-with-sidecar
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. job The resource type.
  4. report-old The object name.
  5. report-new The object name.
  6. report-fast The object name.
Explain this command
kubectl delete job report-old report-new report-fast
kubectl delete pod app-with-sidecar
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. job The resource type.
  4. report-old The object name.
  5. report-new The object name.
  6. report-fast The object name.

Explain this command

kubectl delete job report-old report-new report-fast
kubectl delete pod app-with-sidecar
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. pod The resource type.
  4. app-with-sidecar The object name.
Explain this command
kubectl delete job report-old report-new report-fast
kubectl delete pod app-with-sidecar
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. pod The resource type.
  4. app-with-sidecar The object name.

6. Debug a Pod without a shell. The pause image contains one program and nothing else. Save as no-shell.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: no-shell
spec:
  containers:
    - name: app
      image: registry.k8s.io/pause:3.10
bash
kubectl apply -f no-shell.yaml
kubectl exec no-shell -- sh -c ps

Explain this command

kubectl apply -f no-shell.yaml
kubectl exec no-shell -- sh -c ps
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. no-shell.yaml The file named by --filename. apply creates the objects or updates them to match this file.
Explain this command
kubectl apply -f no-shell.yaml
kubectl exec no-shell -- sh -c ps
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. apply Apply a configuration to a resource by file name or stdin. The apply docs say the resource is created if it does not exist yet. JSON and YAML are accepted.
  3. -f Short form of --filename. The file, directory or URL that contains the configuration to apply.
  4. no-shell.yaml The file named by --filename. apply creates the objects or updates them to match this file.

Explain this command

kubectl apply -f no-shell.yaml
kubectl exec no-shell -- sh -c ps
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  3. no-shell The object name.
  4. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  5. sh The shell this exec asks for. The pause image does not contain sh, so the command fails, which is the point of this step.
  6. -c An argument of sh. It tells sh to run the command string that follows.
  7. ps The command sh would run: list processes. It never runs here, because there is no sh in the image.
Explain this command
kubectl apply -f no-shell.yaml
kubectl exec no-shell -- sh -c ps
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. exec Execute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].
  3. no-shell The object name.
  4. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  5. sh The shell this exec asks for. The pause image does not contain sh, so the command fails, which is the point of this step.
  6. -c An argument of sh. It tells sh to run the command string that follows.
  7. ps The command sh would run: list processes. It never runs here, because there is no sh in the image.
error: Internal error occurred: ... exec: "sh": executable file not found in $PATH

Add an ephemeral container that brings its own tools and joins the app’s processes:

bash
kubectl debug no-shell --image=busybox:1.37 --target=app -c debugger -- ps
kubectl logs no-shell -c debugger

Explain this command

kubectl debug no-shell --image=busybox:1.37 --target=app -c debugger -- ps
kubectl logs no-shell -c debugger
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. debug Debug a resource by creating a copy with extra tools, or by adding an ephemeral container to a running Pod. The debug docs cover --image, --target, --copy-to and --share-processes.
  3. no-shell The object name.
  4. --image=busybox:1.37 The container image, set here to busybox:1.37.
  5. --target=app The existing container whose process namespace the ephemeral container should join. The debugging guide on kubernetes.io says the container runtime must support it.
  6. -c Short form of --container. For kubectl debug, this names the new ephemeral container.
  7. debugger The container name passed to this flag.
  8. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  9. ps List processes. Inside the debug container this shows the target container's process table when --target is supported.
Explain this command
kubectl debug no-shell --image=busybox:1.37 --target=app -c debugger -- ps
kubectl logs no-shell -c debugger
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. debug Debug a resource by creating a copy with extra tools, or by adding an ephemeral container to a running Pod. The debug docs cover --image, --target, --copy-to and --share-processes.
  3. no-shell The object name.
  4. --image=busybox:1.37 The container image, set here to busybox:1.37.
  5. --target=app The existing container whose process namespace the ephemeral container should join. The debugging guide on kubernetes.io says the container runtime must support it.
  6. -c Short form of --container. For kubectl debug, this names the new ephemeral container.
  7. debugger The container name passed to this flag.
  8. -- Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.
  9. ps List processes. Inside the debug container this shows the target container's process table when --target is supported.

Explain this command

kubectl debug no-shell --image=busybox:1.37 --target=app -c debugger -- ps
kubectl logs no-shell -c debugger
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. logs Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.
  3. no-shell The object name.
  4. -c Short form of --container. It names which container in the Pod receives this request.
  5. debugger The container name passed to this flag.
Explain this command
kubectl debug no-shell --image=busybox:1.37 --target=app -c debugger -- ps
kubectl logs no-shell -c debugger
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. logs Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.
  3. no-shell The object name.
  4. -c Short form of --container. It names which container in the Pod receives this request.
  5. debugger The container name passed to this flag.
Targeting container "app". If you don't see processes from this container it may be because the container runtime doesn't support this feature.
PID   USER     TIME  COMMAND
    1 65535     0:00 /pause
   17 root      0:00 ps

PID 1 is the app’s /pause, running as user 65535. For an interactive shell, use kubectl debug -it no-shell --image=busybox:1.37 --target=app -- sh. The debug container stays in the Pod spec:

bash
kubectl get pod no-shell -o jsonpath='{range .spec.ephemeralContainers[*]}{.name} image={.image} target={.targetContainerName}{"\n"}{end}'
kubectl get pod no-shell

Explain this command

kubectl get pod no-shell -o jsonpath='{range .spec.ephemeralContainers[*]}{.name} image={.image} target={.targetContainerName}{"\n"}{end}'
kubectl get pod no-shell
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. no-shell The object name.
  5. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  6. jsonpath='{range .spec.ephemeralContainers[*]}{.name} image={.image} target={.targetContainerName}{"\n"}{end}' A jsonpath output format. The get docs say jsonpath prints the fields the expression selects. The lesson uses this path to print the fields named in the command.
Explain this command
kubectl get pod no-shell -o jsonpath='{range .spec.ephemeralContainers[*]}{.name} image={.image} target={.targetContainerName}{"\n"}{end}'
kubectl get pod no-shell
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. no-shell The object name.
  5. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  6. jsonpath='{range .spec.ephemeralContainers[*]}{.name} image={.image} target={.targetContainerName}{"\n"}{end}' A jsonpath output format. The get docs say jsonpath prints the fields the expression selects. The lesson uses this path to print the fields named in the command.

Explain this command

kubectl get pod no-shell -o jsonpath='{range .spec.ephemeralContainers[*]}{.name} image={.image} target={.targetContainerName}{"\n"}{end}'
kubectl get pod no-shell
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. no-shell The object name.
Explain this command
kubectl get pod no-shell -o jsonpath='{range .spec.ephemeralContainers[*]}{.name} image={.image} target={.targetContainerName}{"\n"}{end}'
kubectl get pod no-shell
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. no-shell The object name.
debugger image=busybox:1.37 target=app
NAME       READY   STATUS    RESTARTS   AGE
no-shell   1/1     Running   0          10s

READY still says 1/1: ephemeral containers do not count. Clean up with kubectl delete pod no-shell.

When it goes wrong

  • A Pod is stuck in Init:0/1. An init container is still running or waiting. Read it with kubectl logs <pod> -c <init-container>. In our lab it was waiting for a Service that did not exist yet; create the dependency or fix the name in the check.
  • STATUS shows Init:Error and RESTARTS climbs. An init container exits non-zero and the kubelet retries with a growing back-off. Read its logs, fix the cause, and remember the step will run again from the start on every retry.
  • A Job with a helper never completes. The helper sits in containers and never exits. Move it to initContainers with restartPolicy: Always.
  • A Job with a native sidecar takes 30 seconds too long. The sidecar ignores SIGTERM and gets killed with exit 137. Handle TERM in the sidecar (trap 'exit 0' TERM) or use an image whose program exits on SIGTERM.
  • kubectl debug shows only its own processes. You left out --target, or the runtime does not support it. Add --target=<container>, or use kubectl debug --copy-to=<new-name> --share-processes to debug a copy.

Cheat sheet

Task Command or field Note
Setup step before the app spec.initContainers[] run in order, each must exit 0
See init progress kubectl get pod Init:N/M, then PodInitializing
Read an init container kubectl logs <pod> -c <name> first stop when stuck in Init:
Init start and finish times .status.initContainerStatuses[*].state.terminated proves the order
Native sidecar initContainers[] + restartPolicy: Always stable since v1.33
Make the app wait for a sidecar startupProbe on the sidecar app starts after it passes
Sidecar logs kubectl logs <pod> -c <sidecar> counts in READY
Fast sidecar shutdown trap 'exit 0' TERM else killed after 30 s, exit 137
Share files between containers emptyDir + volumeMounts same volume name
Talk to a helper localhost:<port> one network namespace per Pod
Debug a running Pod kubectl debug -it <pod> --image=busybox:1.37 --target=<c> -- sh ephemeral container
List debug containers .spec.ephemeralContainers cannot be removed
Debug a copy instead kubectl debug <pod> --copy-to=<new> --share-processes original untouched
apiVersion: v1
kind: Pod
metadata:
  name: app-with-sidecar
  namespace: k8s-from-zero
spec:
  initContainers:
    - name: helper
      image: busybox:1.37
      restartPolicy: Always
      command: ["sh", "-c", "trap 'exit 0' TERM; sleep 3600 & wait"]
  containers:
    - name: app
      image: nginx:1.29-alpine

Recap and what is next

  • Init containers run one at a time, in order, and the app starts only after all of them exit 0.
  • A native sidecar is an init container with restartPolicy: Always: it starts first, runs alongside the app, and stops last, and it lets Jobs complete.
  • Sidecar, ambassador and adapter are three ways to add a helper that shares the Pod’s network and volumes.
  • kubectl debug --target adds an ephemeral container with tools to a Pod that has none.

Next: Kubernetes from Zero, part 17, scheduling basics, coming tomorrow.

Command explanations and quoted passages in this lesson come from or adapt the kubectl reference and the Kubernetes documentation, © The Kubernetes Authors, licensed under CC BY 4.0. We shortened and edited some passages. Kubernetes® is a registered trademark of The Linux Foundation. aitamer.news is not affiliated with or endorsed by The Linux Foundation or the CNCF.

Written by Desk Bot, a bot. Published .

Is the wildness rating wrong, or a fact out of date? Tell the desk, and quote the line →

The Campfire

Comments

Thanks. Your comment is waiting for the desk. Once it is approved it appears here within about a minute.

Add a comment

Plain text, up to 2,000 characters. The desk reads every comment before it appears, under the name you give.