When deploying Python applications using Docker and Kubernetes, you may experience inconsistent log output. Specifically, usedocker logs
You can see the log output normally, but it is used in Kuberneteskubectl logs
But there is no log output. This article will analyze this problem in detail and provide a solution.
Problem description
Suppose we have a simple Python application, the contents are as follows:
import time import logging (level=) logger = (__name__) def main(): while True: ("This is a test log message") (5) if __name__ == "__main__": main()
Docker runs
Run the application using Docker:
docker build -t my-python-app . docker run -d --name my-python-container my-python-app docker logs my-python-container
at this time,docker logs my-python-container
The logs can be output normally:
INFO:__main__:This is a test log message INFO:__main__:This is a test log message ...
Kubernetes Deployment
Deploy the application to a Kubernetes cluster:
apiVersion: v1 kind: Pod metadata: name: my-python-app spec: containers: - name: my-python-container image: my-python-app command: ["python", ""]
Application configuration file:
kubectl apply -f kubectl logs my-python-app
at this time,kubectl logs my-python-app
But no logs were output.
Problem analysis
Standard output buffering
Python's standard output (stdout) and standard error stream (stderr) are buffered by default. This means that when Python writes to the log, it first stores the log data in the memory buffer instead of outputting it to the terminal or log system immediately. When the buffer is full or the program exits, the data in the buffer will be flushed to the output stream.
In Docker, run directlydocker run
When , the standard output is usually refreshed to the terminal immediately, so the log output can be seen normally. But in Kubernetes,kubectl logs
Rely on the container's standard output and error streams, if these streams are buffered,kubectl logs
The log may not be obtained in time.
Solution
To solve this problem, you can use-u
Option to start the Python interpreter and force disable the output buffering. The specific methods are as follows:
Modify Dockerfile
Use in Dockerfile-u
Options start Python:
FROM python:3.9-slim WORKDIR /app COPY . CMD ["python", "-u", ""]
Modify Kubernetes configuration
Or modify it in the Kubernetes configuration filecommand
Fields:
apiVersion: v1 kind: Pod metadata: name: my-python-app spec: containers: - name: my-python-container image: my-python-app command: ["python", "-u", ""]
verify
Rebuild the Docker image and deploy it to Kubernetes:
docker build -t my-python-app . docker push my-python-app kubectl apply -f kubectl logs my-python-app
at this time,kubectl logs my-python-app
The log should be output normally.
Summarize
The inconsistent log output problem of Python programs in Docker and Kubernetes is mainly caused by the standard output buffering mechanism. By using-u
Option to start the Python interpreter, which can disable output buffering, ensure that the logs can be output to the standard output stream in time, and thus be used in Kuberneteskubectl logs
Check the log normally.
This is the article about the content of the python program not displaying logs in the k8s cluster. For more related content of the python k8s log, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!