Kubernetes Sidecar pattern

Hi Team,

I am facing an issue with the Kubernetes Sidecar Pattern task and need help verifying the expected solution.

Task says to create:

  • a regular nginx container
  • an init container named sidecar-container

with this command:

"sh","-c","while true; do cat /var/log/nginx/access.log /var/log/nginx/error.log; sleep 30; done"

I created the following YAML based on the task description:

apiVersion: v1
kind: Pod
metadata:
  name: webserver

spec:
  volumes:
    - name: shared-logs
      emptyDir: {}

  initContainers:
    - name: sidecar-container
      image: ubuntu:latest
      command: ["sh","-c","while true; do cat /var/log/nginx/access.log /var/log/nginx/error.log; sleep 30; done"]
      volumeMounts:
        - name: shared-logs
          mountPath: /var/log/nginx

  containers:
    - name: nginx-container
      image: nginx:latest
      volumeMounts:
        - name: shared-logs
          mountPath: /var/log/nginx

But the pod remains stuck in:

Init:0/1

because the init container never exits due to the infinite loop.

Could someone please confirm whether:

  1. the task really expects an initContainer, or
  2. sidecar-container should actually be a normal running container instead?

Thanks.

A sidecar container is a particular kind of initContainer – it needs to have the setting restartPolicy: Always . See this page in the K8s docs for details.

You might check my problem & my fixed code via my bug report here: 100 days of DevOps - Day 55