OpenShift Pod中Docker容器内Nginx日志未写入/不更新排查
Hey there, let's sort out why your Nginx logs aren't updating as expected in your OpenShift Pod's Docker container. Let's break down the current setup and how to fix this:
What's Happening With Your Log Configuration?
Looking at your /var/log/nginx directory output:
lrwxrwxrwx 1 root root 11 Jan 22 2020 access.log -> /dev/stdout lrwxrwxrwx 1 root root 11 Jan 22 2020 error.log -> /dev/stderr
These are symbolic links pointing to the container's standard output (/dev/stdout) and standard error (/dev/stderr) streams. This is a standard default for containerized Nginx—instead of writing logs to disk files, Nginx sends logs directly to the container's output streams.
That's why you don't see file updates when checking inside the container: those "files" aren't actual disk logs, just redirects. OpenShift collects these streams as part of its built-in logging system.
How to Access Your Nginx Logs in OpenShift
You don't need to dig into the container to view logs. Use OpenShift's CLI commands to fetch them easily:
- View the latest logs for your pod:
oc logs <your-pod-name> - Track logs in real-time (like
tail -f):oc logs -f <your-pod-name> - View logs from a previous pod instance (if it restarted):
oc logs --previous <your-pod-name>
If You Really Need Logs Written to Files in the Container
If you have a specific requirement to store logs as files inside the container (note: this isn't recommended for containerized workloads, as logs will be lost when the pod restarts unless you use persistent storage), here's how to adjust:
- Enter the running pod via OpenShift CLI:
oc rsh <your-pod-name> - Remove symbolic links and create actual log files:
rm /var/log/nginx/access.log /var/log/nginx/error.log touch /var/log/nginx/access.log /var/log/nginx/error.log - Set correct permissions so the
nginxuser can write to these files:chown nginx:nginx /var/log/nginx/access.log /var/log/nginx/error.log - Reload Nginx to apply changes (or restart the pod entirely):
nginx -s reload
Persistent Storage Note
To keep logs across pod restarts, create a Persistent Volume Claim (PVC) in OpenShift and mount it to the /var/log/nginx directory in your pod's deployment configuration.
Containerized Nginx Best Practice for OpenShift
Redirecting logs to /dev/stdout and /dev/stderr is the recommended approach for containerized apps. OpenShift integrates with logging stacks like EFK (Elasticsearch, Fluentd, Kibana) to collect, store, and visualize these logs centrally. This avoids managing log files inside containers and ensures logs are retained even when pods are terminated or restarted.
内容的提问来源于stack exchange,提问作者Shabar

