获取运行 python 文件的 Kubernetes pod 的输出

问题描述 投票:0回答:1

我正在 kubernetes pod 内的 docker 容器上运行 python 脚本,我的脚本是一个 mqtt 脚本,当我接收消息时,它会打印出该消息(我希望将来能够保存它或发送它)到另一个.txt)。 现在我的日志中没有收到任何输出,这是为什么? 我正在获取输出,但只是关于客户端连接、新客户端连接等。我想获取消息,现在我只是发送“hello”,如果我在终端 mosquitto_pub/mosquitto_sub 中这样做,我就会收到此 mqtt 消息,但不是在 pod 脚本的输出上。谢谢你:)

Dockerfile:

FROM docker.deere.com/python:3.8-slim

RUN pip install paho-mqtt

COPY subscriber.py /subscriber.py

CMD ["python", "./subscriber.py"]

Kubernetes部署: api版本:apps/v1 种类:部署 元数据: 名称: 蚊子经纪人 规格: 副本:1 选择器: 匹配标签: 应用程序:MQTT经纪人 模板: 元数据: 标签: 应用程序:MQTT经纪人 规格: 容器: - 名称:mqtt-broker-init 图片:我的蚊子:最新 端口: - 集装箱港口:1883 - 名称:mqtt-subs 图片:mosquitto-subscriber:最新

python脚本:

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))
    client.subscribe("test/topic")

def on_message(client, userdata, msg):
    print("Received message: " + msg.payload.decode())

client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "SubscriberTest")
client.on_connect = on_connect
client.on_message = on_message

client.connect("172.30.0.130", 30988, 60)
client.loop_forever()

pod 输出:

❯  kubectl logs deployment/mosquitto-broker
Found 4 pods, using pod/mqtt-broker-68c7545bc8-4fs86
Defaulted container "mqtt-broker" out of: mqtt-broker, mosquitto-subs
1708114383: mosquitto version 2.0.18 starting
1708114383: Config loaded from /mosquitto/config/mosquitto.conf.
1708114383: Opening ipv4 listen socket on port 1883.
1708114383: Opening ipv6 listen socket on port 1883.
1708114383: mosquitto version 2.0.18 running
1708114429: New connection from 10.244.0.1:32866 on port 1883.
1708114429: New client connected from 10.244.0.1:32866 as SubscriberTest (p2, c1, k60).
1708114471: New connection from 10.244.0.1:23628 on port 1883.
1708114471: New client connected from 10.244.0.1:23628 as auto-8F54FBAD-6D14-FBB1-6C0B-9E9A5E6101AB (p2, c1, k60).
1708114471: Client auto-8F54FBAD-6D14-FBB1-6C0B-9E9A5E6101AB disconnected.
1708114474: New connection from 10.244.0.1:14961 on port 1883.
1708114474: Client SubscriberTest already connected, closing old connection.
1708114474: New client connected from 10.244.0.1:14961 as SubscriberTest (p2, c1, k60).
1708114475: New connection from 10.244.0.1:18285 on port 1883.
1708114475: Client SubscriberTest already connected, closing old connection.
1708114475: New client connected from 10.244.0.1:18285 as SubscriberTest (p2, c1, k60).
1708114476: New connection from 10.244.0.1:11965 on port 1883.
1708114476: Client SubscriberTest already connected, closing old connection.
1708114476: New client connected from 10.244.0.1:11965 as SubscriberTest (p2, c1, k60).
1708114477: New connection from 10.244.0.1:50342 on port 1883.
1708114477: Client SubscriberTest already connected, closing old connection.
1708114477: New client connected from 10.244.0.1:50342 as SubscriberTest (p2, c1, k60).
1708114604: New connection from 10.244.0.1:48118 on port 1883.
1708114604: New client connected from 10.244.0.1:48118 as auto-6CE6C67F-7CC7-53BD-2627-F3A483F7E774 (p2, c1, k60).
1708114604: Client auto-6CE6C67F-7CC7-53BD-2627-F3A483F7E774 disconnected.
1708115507: New connection from 10.244.0.1:33888 on port 1883.
1708115507: Client SubscriberTest already connected, closing old connection.
1708115507: New client connected from 10.244.0.1:33888 as SubscriberTest (p2, c1, k60).
1708115512: New connection from 10.244.0.1:60569 on port 1883.
1708115512: Client SubscriberTest already connected, closing old connection.
1708115512: New client connected from 10.244.0.1:60569 as SubscriberTest (p2, c1, k60).
1708115565: New connection from 10.244.0.1:14096 on port 1883.
1708115565: New client connected from 10.244.0.1:14096 as auto-451A6E10-1B48-73E8-5BA8-A7606A5D4AC9 (p2, c1, k60).
1708115565: Client auto-451A6E10-1B48-73E8-5BA8-A7606A5D4AC9 disconnected.
1708115567: New connection from 10.244.0.1:15433 on port 1883.
1708115567: New client connected from 10.244.0.1:15433 as auto-3B09DB6C-5735-EEF5-2F17-279A7221E179 (p2, c1, k60).
1708115567: Client auto-3B09DB6C-5735-EEF5-2F17-279A7221E179 disconnected.
1708115929: New connection from 10.244.0.1:23931 on port 1883.
1708115929: New client connected from 10.244.0.1:23931 as auto-7ECB485C-F10A-3DB3-9CB4-DF4F481EFF46 (p2, c1, k60).
1708115929: Client auto-7ECB485C-F10A-3DB3-9CB4-DF4F481EFF46 disconnected.
1708115939: New connection from 10.244.0.1:33648 on port 1883.
1708115939: New client connected from 10.244.0.1:33648 as auto-A4247317-B4D0-1B57-A339-E29F57E70313 (p2, c1, k60).
1708115939: Client auto-A4247317-B4D0-1B57-A339-E29F57E70313 disconnected.
docker kubernetes mqtt
1个回答
0
投票

只需将 Dockerfile 的 CMD 命令更改为:

CMD ["python", "-u", "./subscriber.py"]

您只需在终端中输入

python -h
即可阅读更多内容:

-u: force the stdout and stderr streams to be unbuffered; 
    this option does not affect stdin; also PYTHONUNBUFFERED=x                                                                                                                     

这是任何包含 Python 的 Docker 镜像中“必须要做的事情”。

© www.soinside.com 2019 - 2024. All rights reserved.