1

I have an NGINX installation (used as a web proxy) inside a built Docker image in Kubernetes that will eventually have to be upgraded to run multiple pods to handle the traffic volume.

The log output from access.log and error.log needs to not only go into files (for our log collector service) but also appear in the Kubernetes logs, i.e. appear in /dev/stdout and /dev/stderr. My current solution is to run tail -F access.log >> /dev/stdout & and similarly for the error log.

I would like to have these files in a shared PVC storage (so the logs remain after the pod crashes or restarts), and this should work fine as long as I only have one pod.

Once I have multiple pods, I have the problem that all pods will want to write into the same files - access.log and error.log.

I have tried a few things, but it doesn't look like NGINX supports using a variable in the log paths, so I am stuck in writing to static paths.

My best theoretical solution so far is to update the error and access log formats to include the pod name, but that would again involve adding a variable into NGINX, and I'm not sure it would allow that.

I also tried symlinks (ln -sf access-${PODNAME}.log access.log and similar), but this can't work in a multi-pod situation, either, since all pods share the same configuration.

I'm sure I'm not the only one with this situation.

Any ideas I can try?

Thanks!

1 Answer 1

2

Indeed this type of question is common. Here is one way to accomplish it.

Create NGINX Config Template:

Use placeholders for pod-specific log paths.

Init Container:

Replace placeholders with actual pod names.

Sidecar Container:

Tail log files to stdout/stderr.

ConfigMap:

Store the NGINX config template.

Persistent Volume Claim (PVC):

Use shared PVC for log storage.

That is one way to do it.

Recently I answered something somewhat related to logging, it isn't the answer to your question, but it might give you a helpful hint to discriminate logs: Kubernetes filebeat config map for pod events

2
  • Ooh, I was so close... My nginx.conf is supplied by a configmap, and I tried editing it from inside the pod and was told I couldn't - BUT if I took the nginx.conf from the configmap and created an EDITED COPY of it where NGINX is expecting it to be, this should work! I'll try it tomorrow! Thanks! Commented Jun 6 at 2:10
  • This worked like a charm - all I had to do was update the entrypoint script, and it worked. Thank you! Commented Jun 7 at 15:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .