How to log to Kubernetes Container Log from Python process

7/25/2019

With Kubernetes Container running a Python script:

import time
while True:
    try:
        for i in range(10):
            if i==0:
                raise Exception('Exception occurred!')
    except:
        pass
    time.sleep(1)

I would like to pass the Exception's message 'Exception occurred!' down to the Container so this error message could be seen with:

kubectl describe pod pod_id

Would it be possible?

-- alphanumeric
amazon-web-services
docker
eks
kubernetes
python

1 Answer

7/26/2019

Anything you print() will be visible in kubectl logs. (You may need to set an environment variable PYTHONUNBUFFERED=1 in your pod spec.)

Your code as you've written it will never print anything. The construct

try:
  ...
except:
  pass

silently ignores any and all exceptions out of the try block. The bare except: even captures some system-level exceptions like SystemExit or KeyboardInterrupt; this is almost always wrong. Often you want your except blocks to be as tightly scoped as you can, and the Python tutorial on user-defined exceptions is a helpful pattern.

(The exception to this, particularly in a Kubernetes context, is that you will often want a very broad exception handler to do something like return an HTTP 500 error to a network request, rather than crashing the application.)

A better example might look like:

import time

class OneException(Exception):
  pass

def iteration():
  for i in range(10):
    try:
      if i == 1:
        raise OneException("it is one")
      print(i, math.sqrt(i), math.sqrt(-i))
      # will work when i==0 but fail when i==2
    except OneException as e:
      print(i, repr(e))
      # and proceed to the next iteration

if __name__ == '__main__':
  while True:
    # The top-level loop.  We want a very broad catch here.
    try:
      iteration()
    except Exception as e:
      print('iteration failed', repr(e))
    time.sleep(1)
-- David Maze
Source: StackOverflow