Challenge, Medium,  on  Kubernetes

Deploy a Pod with a Sidecar Log Shipper Using Init Container Pattern

Scenario

Create an Nginx web server Pod with a sidecar container that continuously reads from the shared log directory and outputs log content to stdout, making it visible through kubectl logs.

Use a restartable init container to run the sidecar alongside the main application container while sharing the same volumes and network resources.


Task

Create a Pod named access-logger in the platform-logs namespace with the label app: access-logger.

1. Main container named web-server:

  • Image: nginx:stable
  • Mount the shared volume at /var/log/nginx

2. Sidecar container named log-forwarder:

  • Image: busybox:stable
  • Command: /bin/sh -c "while true; do cat /var/log/nginx/access.log 2>/dev/null; sleep 10; done"
  • Mount the shared volume at /var/log/nginx

3. Shared emptyDir volume named shared-logs mounted by both containers at /var/log/nginx.

Diagram showing Nginx writing logs and Busybox reading logs via shared emptyDir

The Log Flow: Nginx writes logs to /var/log/nginx; the sidecar streams them via kubectl logs.

Once the Pod is running, verify the sidecar is streaming log output:

kubectl logs access-logger -n platform-logs -c log-forwarder

Hint

A restartable init container is an init container configured with:

initContainers:
- name: log-forwarder
  image: busybox:stable
  restartPolicy: Always

Unlike regular init containers (which run once and exit before app containers start), a restartable init container keeps running for the lifetime of the Pod — effectively behaving like a sidecar container.

In this task:

  • web-server writes logs to /var/log/nginx
  • log-forwarder continuously reads the same log file
  • Both containers share the emptyDir volume shared-logs

Useful references:

Sidecar Containers | Shared Volumes between Containers


💡 Test Cases