StaffCoder
Kubernetes
15 modules · 60 challengesSign up
Module 5 · Multi-Container Pods

Sidecar Log Shipper

easySidecar ContainersShared Pod Volumes—No data yet

Problem statement

The orders service predates every logging convention the platform has: it writes everything it has to say into a file inside its own container. Namespace bundle-shipping runs it as orders-legacy, and asking the cluster what it printed returns nothing at all - the lines exist, they are simply somewhere nobody can reach. Rewriting the app is not on the table, so give it a companion that can reach them.

About this exercise

A pod can hold more than one container, and the extra ones are usually there to do something for the main one. A sidecar is exactly that: a container that runs beside the app for the whole life of the pod, adding a capability the app does not have and was never changed to provide.

Two properties of a pod make it work:

  • Containers in a pod can share volumes. An emptyDir volume is created when the pod is scheduled and lives as long as the pod. Every container that mounts it sees the same directory - one writes, the other reads. Without a shared volume, a file written inside one container is invisible to every other container, which is precisely the situation orders-legacy is in.
  • Every container has its own log stream. What the cluster serves as a container's log is that container's standard output, kept per container. A pod with several containers therefore has several streams, and a log read has to say which one it wants; asked without a container, it answers for the first one.

Put those together and the shape of the fix appears: the app keeps writing its file into a shared volume, and a second container reads that file and prints it on its standard output - where the cluster can serve it like any other log.

kubectl explain pod.spec.volumes, kubectl explain pod.spec.containers.volumeMounts and kubectl logs --help are the reference.

How this project is graded

The grader reads your live pod: what it declares, what each container reports, what the sidecar can see on disk, and what each container's log stream actually contains. orders-legacy is read, never repaired - your saved answer is compared against what the cluster reports for it. Re-apply your manifest after every edit; the cluster is graded, not the file.

What you will build

A pod in manifest.yaml, applied to the cluster, and one answer in answers/:

pod orders-api, namespace bundle-shipping

one emptyDir volume, mounted by both containers at /var/log/app container api busybox:1.36 prints ORDERS-API STARTED on its own standard output, once, then appends one line ORDER <n> accepted to /var/log/app/orders.log every second, n counting up from 1 (this is the legacy app - the starter manifest already has it) container shipper busybox:1.36 its standard output carries the lines of /var/log/app/orders.log, from the first line onward, as they are appended

answers/silent.txt the number of log lines the cluster serves for orders-legacy (that number alone)

Step 1 - the app that nobody can hear

Ask the cluster for the legacy pod's log and record how much of it there is.

legacy_stdout_recorded_as_silent should pass

Step 2 - two containers, one volume

Bring up orders-api with both containers running, and a volume both of them mount, so the file the app writes is a file the sidecar can open.

both_containers_running_in_one_pod, shared_volume_mounted_by_both_containers and app_log_file_readable_from_the_sidecar should pass

Step 3 - out to the cluster

The sidecar's job is to put those lines on its own standard output, so the cluster serves them as its log - while the app's own stream keeps saying only what the app itself printed.

sidecar_stdout_carries_the_app_file_lines and per_container_log_selection_isolates_each_stream should pass

When every test passes

You have:

  • A file inside one container turned into a log stream the cluster can serve
  • Two containers cooperating through a volume neither of them owns alone
  • A legacy app given a new capability without a line of it being changed
  • Per-container log reads that return one container's output and not its neighbour's

You do not have yet:

  • Containers that talk to each other over the network. A shared volume is only one of the things a pod shares; the next exercise uses the other.

Verify

Click Test after every step.

More in Multi-Container Pods

See the module
Init ContainersGate an app container on init containers that prepare its config in a shared volume, prove the declared order from the cluster's own timestamps, and read what a failed preparation records.—No data yeteasyLocalhost CooperationRun an app and its helper as containers of one pod talking over localhost, confirm the shared pod IP from inside both, and read what happens when two containers want the same port.—No data yeteasyComposed Application PodCompose one pod out of an init container, the app, a log-shipping sidecar and a localhost helper, and prove ordering, shared volumes, one pod IP and per-container log streams.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.