Kubernetes“日志”不显示在 pod 中的容器中运行的简单 python 程序的任何输出

Luk*_*fer 2 python logging kubernetes

我有一个非常简单的设置。我的 Kubernetes .yaml 配置文件:

apiVersion: batch/v1
kind: Job
metadata:
  name: stdout-test
spec:
  template:
    spec:
      priorityClassName: research-high
      containers:
        - name: container-stdout-test
          image: <here comes my secret image repo>
          imagePullPolicy: "IfNotPresent"
          resources:
            limits:
              nvidia.com/gpu: "1"
              cpu: "1"
              memory: "8Gi"
            requests:
              nvidia.com/gpu: "1"
              cpu: "1"
              memory: "4Gi"
          command: ["python3", "/workspace/main.py"]
          volumeMounts:
            - mountPath: /workspace 
              name: localdir 
      imagePullSecrets:
        - name: lsx-registry
      restartPolicy: "Never"
      volumes:
        - name: localdir
          cephfs:
            monitors:
              - <here come my secret monitors>
            user: <namespace>
            path: "/home/stud/nothelfer/stdout-test" 
            secretRef: 
              name: <my secret>
Run Code Online (Sandbox Code Playgroud)

我的简单Python程序(main.py):

import time

for i in range(0, 1000):
    print(i)
    time.sleep(1.0)
Run Code Online (Sandbox Code Playgroud)

在 kubernetes 中启动和运行作业效果很好。我从以下位置得到这个输出kubectl get pods

NAME                READY   STATUS    RESTARTS   AGE
stdout-test-hl6qs   1/1     Running   0          5s
Run Code Online (Sandbox Code Playgroud)

但我没有通过使用从 stdout 获得预期的输出 0, 1, 2, kubectl logs -f stdout-test-hl6qs... 相反,我只是得到一个空白屏幕。只是没有输出打印到控制台。我期待得到我的简单 python 程序的输出。我已经尝试了所有可能的命令行参数,kubectl logs但没有一个能让我在命令行中看到 Python 程序的输出。图像设置正确,Python 程序在容器中运行良好,我已经检查了所有这些。有人能帮我一下吗?

我的 Dockerfile:

FROM pytorch/pytorch
RUN conda install matplotlib pandas numpy
RUN conda update --all

ENV PYTHONUNBUFFERED 1

RUN python -c "import torch, pandas, matplotlib"
Run Code Online (Sandbox Code Playgroud)

Luk*_*fer 5

经过多次尝试,我弄清楚了如何查看标准输出输出。在 Kubernetes 配置文件中,我必须将 line 替换command: ["python3", "/workspace/main.py"]command: ["python3", "-u", "/workspace/main.py"],这会将 Python 的输出配置为无缓冲。ENV PYTHONUNBUFFERED=1由于不完全清楚的原因,当我只设置变量或ENV PYTHONUNBUFFERED 1在 Dockerfile 中时它不起作用。我现在可以看到程序的输出,无论我是否指定ENV PYTHONUNBUFFERED=1ENV PYTHONUNBUFFERED 1在 Dockerfile 中指定。

Python无缓冲