Kubernetes log not showing output of python print method

1/29/2020

I've a python application in which I'm using print() method tho show text to a user. When I interact with this application manually using kubectl exec ... command I can see the output of prints.

However, when script is executed automatically on container startup with CMD python3 /src/my_app.py (last entry in Dockerfile) then, the prints are gone (not shown in kubectl logs). Ayn suggestin on how to fix it?

-- Lukasz Dynowski
docker
kubernetes
python

2 Answers

1/29/2020

It turned out to be a problem of python environment. Setting, these two environment variables PYTHONUNBUFFERED=1 and PYTHONIOENCODING=UTF-8 fixed the issue.

-- Lukasz Dynowski
Source: StackOverflow

1/29/2020

This is because stdout and stderr streams are buffered in Python.

You can set the Environment variable to PYTHONUNBUFFERED=1 or True

Also you could use -u option in your Dockerfile.

-u

Force the stdout and stderr streams to be unbuffered. This option has no effect on the stdin stream.

Changed in version 3.7: The text layer of the stdout and stderr streams now is unbuffered.

-- Crou
Source: StackOverflow