Kubernetes 未记录打印

iro*_*rom 3 python logging python-2.x kubernetes kubectl

我有一个简单的 python 2.7 程序在 Kubernetes (AKS) 的容器中运行,它将调试信息打印到标准输出

response = requests.post(uri,data=body, headers=headers)
if (response.status_code >= 200 and response.status_code <= 299):
    print 'Accepted ' + log_type + ' on ' + rfc1123date
else:
    print "Response code: {}".format(response.status_code)
Run Code Online (Sandbox Code Playgroud)

我没有看到它kubectl logs container_name,输出是空的(但我知道这很好,因为成功发布)。我尝试添加"-u"CMD ["-u","syslog2la.py"]在Dockerfile,但它并没有帮助。如何在“ kubectl logs ”中获取python打印?

Sog*_*rno 14

将以下内容添加到您的 Dockerfile:

ENV PYTHONUNBUFFERED=0
Run Code Online (Sandbox Code Playgroud)

  • 作为旁注,您还可以使用“-u”标志启动 python 以获取无缓冲输出。例如 /bin/python3 -u /path/to/script.py (4认同)
  • `PYTHONUNBUFFERED=1` 对我有用。`PYTHONUNBUFFERED=0` 没有。 (2认同)

sta*_*onk 6

将其添加到您的 Dockerfile 中:

ENV PYTHONUNBUFFERED=1
Run Code Online (Sandbox Code Playgroud)

感谢@frederix