I have a python script running inside a container of kubernetes pod.How do i stop the script which runs along with the starting of the pod?

6/8/2021

I am running two threads inside the container of the Kubernetes pod one thread pushes some data to db and other thread (flask app) shows the data from database. So as soon as the pod starts up main.py(starts both the threads mentioned above) will be called.

Docker file:

FROM python:3

WORKDIR /usr/src/app

COPY app/requirements.txt .

RUN pip install -r requirements.txt

COPY app .

CMD ["python3","./main.py"]

I have two questions:

  1. Is logs the only way to see the output of the running script? Can't we see its output continuously as it runs on the terminal?

  2. Also, I m not able to run the same main.py file by going into the container. It throws below error:

Exception in thread Thread-1:
Traceback (most recent call last):
 File "/usr/local/lib/python3.9/threading.py", line 954, in _bootstrap_inner
  self.run()
 File "/usr/local/lib/python3.9/threading.py", line 892, in run
  self._target(*self._args, **self._kwargs)
 File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 920, in run
  run_simple(t.cast(str, host), port, self, **options)
 File "/usr/local/lib/python3.9/site-packages/werkzeug/serving.py", line 1008, in run_simple
  inner()
 File "/usr/local/lib/python3.9/site-packages/werkzeug/serving.py", line 948, in inner
  srv = make_server(
 File "/usr/local/lib/python3.9/site-packages/werkzeug/serving.py", line 780, in make_server
  return ThreadedWSGIServer(
 File "/usr/local/lib/python3.9/site-packages/werkzeug/serving.py", line 686, in __init__
  super().__init__(server_address, handler) # type: ignore
 File "/usr/local/lib/python3.9/socketserver.py", line 452, in __init__
  self.server_bind()
 File "/usr/local/lib/python3.9/http/server.py", line 138, in server_bind
  socketserver.TCPServer.server_bind(self)
 File "/usr/local/lib/python3.9/socketserver.py", line 466, in server_bind
  self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use (edited)

How do I stop the main.py script which starts along with the pod and be able to run the main.py from the container itself directly?

Thank you.

-- Naga sai Kiran
docker
flask
kubernetes
python-3.x
python-multithreading

1 Answer

6/8/2021

The error message says all:

OSError: [Errno 98] Address already in use (edited)

It looks like your python script tries to open the same port twice. You cannot do that. Check your code and fix it.

Now answering your other question:

Is logs the only way to see the output of the running script? Can't we see its output continuously as it runs on the terminal?

Running kubectl logs -f will follow the logs, which should let you see the output continuously as it runs in terminal.

-- Matt
Source: StackOverflow