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.
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.
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.
- 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:
kubectl apply -f init-order.yaml
kubectl get pod web-init -wExplain this command
kubectl apply -f init-order.yaml
kubectl get pod web-init -wkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.applyApply 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.-fShort form of --filename. The file, directory or URL that contains the configuration to apply.init-order.yamlThe 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 -wkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.web-initThe object name.-wShort 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:
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.htmlExplain 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.htmlkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.web-initThe object name.-oShort form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.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.htmlkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.web-initThe object name.-oShort form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.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.htmlkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.execExecute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].web-initThe object name.-cShort form of --container. It names which container in the Pod receives this request.webThe container name passed to this flag.--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.catPrint the contents of a file./usr/share/nginx/html/index.htmlThe 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"]
kubectl apply -f wait-for-db.yaml
kubectl get pod app-waits
kubectl logs app-waits -c wait-for-db --tail=4Explain this command
kubectl apply -f wait-for-db.yaml
kubectl get pod app-waits
kubectl logs app-waits -c wait-for-db --tail=4kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.applyApply 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.-fShort form of --filename. The file, directory or URL that contains the configuration to apply.wait-for-db.yamlThe 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=4kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.app-waitsThe 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=4kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.logsPrint the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.app-waitsThe object name.-cShort form of --container. It names which container in the Pod receives this request.wait-for-dbThe container name passed to this flag.--tail=4How 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
kubectl apply -f db-service.yaml
kubectl get pod app-waits
kubectl logs app-waits -c appExplain this command
kubectl apply -f db-service.yaml
kubectl get pod app-waits
kubectl logs app-waits -c appkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.applyApply 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.-fShort form of --filename. The file, directory or URL that contains the configuration to apply.db-service.yamlThe 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 appkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.app-waitsThe object name.
Explain this command
kubectl apply -f db-service.yaml
kubectl get pod app-waits
kubectl logs app-waits -c appkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.logsPrint the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.app-waitsThe object name.-cShort form of --container. It names which container in the Pod receives this request.appThe 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"]
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 -1Explain 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 -1kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.applyApply 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.-fShort form of --filename. The file, directory or URL that contains the configuration to apply.init-fails.yamlThe 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 -1kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.init-failsThe 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 -1kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.logsPrint the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.init-failsThe object name.-cShort form of --container. It names which container in the Pod receives this request.migrateThe 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 -1kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.eventsThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.--field-selectorFilter the list by field. The get docs treat --field-selector as separate from a label selector.involvedObject.name=init-failsThe field query passed to --field-selector: involvedObject.name=init-fails.--sort-by=.lastTimestampSort 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.|A shell pipe. The output of the command on the left becomes the input of the command on the right.tailShell. Prints the last lines of its input. The lesson uses it to keep the table short.-1With 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:
kubectl delete pod init-fails app-waits web-init
kubectl delete service dbExplain this command
kubectl delete pod init-fails app-waits web-init
kubectl delete service dbkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.deleteDelete resources by file names, stdin, resources and names, or by resources and label selector.podThe resource type.init-failsThe object name.app-waitsThe object name.web-initThe object name.
Explain this command
kubectl delete pod init-fails app-waits web-init
kubectl delete service dbkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.deleteDelete resources by file names, stdin, resources and names, or by resources and label selector.serviceThe resource type.dbThe 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: {}
kubectl apply -f native-sidecar.yaml
kubectl get pod app-with-sidecar -wExplain this command
kubectl apply -f native-sidecar.yaml
kubectl get pod app-with-sidecar -wkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.applyApply 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.-fShort form of --filename. The file, directory or URL that contains the configuration to apply.native-sidecar.yamlThe 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 -wkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.app-with-sidecarThe object name.-wShort 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:
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=2Explain 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=2kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.app-with-sidecarThe object name.-oShort form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.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=2kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.logsPrint the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.app-with-sidecarThe object name.-cShort form of --container. It names which container in the Pod receives this request.log-shipperThe container name passed to this flag.--tail=2How 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"]
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)'kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.applyApply 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.-fShort form of --filename. The file, directory or URL that contains the configuration to apply.job-old-sidecar.yamlThe file named by --filename. apply creates the objects or updates them to match this file.-fShort form of --filename. The file, directory or URL that contains the configuration to apply.job-native-sidecar.yamlThe 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)'sleepShell. Waits the given number of seconds.70How 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)'kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.jobsThe 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)'kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podsThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.-lShort form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.'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:
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}'kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.-lShort form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.batch.kubernetes.io/job-name=report-newThe label selector passed to -l. It keeps only the objects whose labels match this query.-oShort form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.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"]
kubectl apply -f job-native-sidecar-trap.yaml
kubectl wait --for=condition=complete job/report-fast --timeout=60s
kubectl get job report-fastExplain this command
kubectl apply -f job-native-sidecar-trap.yaml
kubectl wait --for=condition=complete job/report-fast --timeout=60s
kubectl get job report-fastkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.applyApply 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.-fShort form of --filename. The file, directory or URL that contains the configuration to apply.job-native-sidecar-trap.yamlThe 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-fastkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.waitWait for a condition on one or more resources.--for=condition=completeThe condition to wait for. Here kubectl waits until the condition named after --for is met.job/report-fastThe resource written as type/name: job/report-fast.--timeout=60sHow 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-fastkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.jobThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.report-fastThe 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:
kubectl delete job report-old report-new report-fast
kubectl delete pod app-with-sidecarExplain this command
kubectl delete job report-old report-new report-fast
kubectl delete pod app-with-sidecarkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.deleteDelete resources by file names, stdin, resources and names, or by resources and label selector.jobThe resource type.report-oldThe object name.report-newThe object name.report-fastThe object name.
Explain this command
kubectl delete job report-old report-new report-fast
kubectl delete pod app-with-sidecarkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.deleteDelete resources by file names, stdin, resources and names, or by resources and label selector.podThe resource type.app-with-sidecarThe 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
kubectl apply -f no-shell.yaml
kubectl exec no-shell -- sh -c psExplain this command
kubectl apply -f no-shell.yaml
kubectl exec no-shell -- sh -c pskubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.applyApply 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.-fShort form of --filename. The file, directory or URL that contains the configuration to apply.no-shell.yamlThe 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 pskubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.execExecute a command in a container. The synopsis is kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...].no-shellThe object name.--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.shThe shell this exec asks for. The pause image does not contain sh, so the command fails, which is the point of this step.-cAn argument of sh. It tells sh to run the command string that follows.psThe 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:
kubectl debug no-shell --image=busybox:1.37 --target=app -c debugger -- ps
kubectl logs no-shell -c debuggerExplain this command
kubectl debug no-shell --image=busybox:1.37 --target=app -c debugger -- ps
kubectl logs no-shell -c debuggerkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.debugDebug 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.no-shellThe object name.--image=busybox:1.37The container image, set here to busybox:1.37.--target=appThe existing container whose process namespace the ephemeral container should join. The debugging guide on kubernetes.io says the container runtime must support it.-cShort form of --container. For kubectl debug, this names the new ephemeral container.debuggerThe container name passed to this flag.--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.psList 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 debuggerkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.logsPrint the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.no-shellThe object name.-cShort form of --container. It names which container in the Pod receives this request.debuggerThe 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:
kubectl get pod no-shell -o jsonpath='{range .spec.ephemeralContainers[*]}{.name} image={.image} target={.targetContainerName}{"\n"}{end}'
kubectl get pod no-shellExplain this command
kubectl get pod no-shell -o jsonpath='{range .spec.ephemeralContainers[*]}{.name} image={.image} target={.targetContainerName}{"\n"}{end}'
kubectl get pod no-shellkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.no-shellThe object name.-oShort form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.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-shellkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.no-shellThe 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 withkubectl 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:Errorand 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
containersand never exits. Move it toinitContainerswithrestartPolicy: 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 debugshows only its own processes. You left out--target, or the runtime does not support it. Add--target=<container>, or usekubectl debug --copy-to=<new-name> --share-processesto 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 --targetadds 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.

The Campfire
CommentsThanks. Your comment is waiting for the desk. Once it is approved it appears here within about a minute.
Add a comment