Python Flask weird logging behavior (kubernetes)

7/2/2020

So I made a very simple flask-based app and hosted it in a kubernetes pod.

When I open the logs in Rancher, I can see this warning:

 * Serving Flask app "app/preapproved_limits/api.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off

that I see as well when I start flask on my local machine.

But what I don't see on my local machine and do see in Rancher is this:

10.0.67.20 - - [02/Jul/2020 16:49:20] "GET /health HTTP/1.1" 200 -
INFO:werkzeug:10.0.67.20 - - [02/Jul/2020 16:49:20] "GET /health HTTP/1.1" 200 -

which gets logged every 5 seconds. What the heck?

But the most important and annoying thing is I can't see my logs which I make with the Python's print() function.

Can someone explain:

  • Why do I see something both in Rancher and locally (the initial Flask warning), but
  • why do I see something only in Rancher (the /health or INFO:werkzeug logs)
  • and why do I see something only locally on my machine, but not in Rancher (print())
-- Nurbol Alpysbayev
flask
kubernetes
logging
python
rancher

1 Answer

7/2/2020

/health is a normal healthcheck endpoint that k8s needs.

Flask doesn't print to stdout by default because it buffers lines to make I/O more efficient. You can either call sys.stdout.flush(), print(flush=True), set env var PYTHONUNBUFFERED=1 in your Dockerfile (probably the easiest) or just use logging module as we all should.

You can read more about this env var here.

-- Tom Wojcik
Source: StackOverflow