0

I am running a python script on a docker container inside a kubernetes pod, my script is a mqtt script that when I will be receving a message it prints out the message (i want to be able to save it in the future or send it to another .txt). Right now i am not receiving any outputs in my logs, why is this? I am getting outputs but just about the client connection, new clients connecting etc. I want to get the messages, right now I am just sending "hello", i am receiving this mqtt messages if i do in my terminal mosquitto_pub/mosquitto_sub but not on the output of the pod script. Thank you :)

Dockerfile:

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

RUN pip install paho-mqtt

COPY subscriber.py /subscriber.py

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

Kubernetes deployment: apiVersion: apps/v1 kind: Deployment metadata: name: mosquitto-broker spec: replicas: 1 selector: matchLabels: app: mqtt-broker template: metadata: labels: app: mqtt-broker spec: containers: - name: mqtt-broker-init image: my-mosquitto:latest ports: - containerPort: 1883 - name: mqtt-subs image: mosquitto-subscriber:latest

python script:

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 output:

❯  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.
1

1 Answer 1

1

Simply change your Dockerfile's CMD command to:

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

You can read more just by typing python -h in your terminal:

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

Unbuffered streams in Python immediately transfer data without storing it in a buffer. This means that data is directly read or written from the underlying source or destination as soon as it is available, without waiting for the buffer to fill.

This is a "must-be thing" in any Docker image with Python.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.