0

I am running a Flask Server in pods on Azure Kubernetes Service (AKS). I cannot see the print-statements of my Flask application nor the logs of Gunicorn when I run kubectl logs <pod-name>. The only time I see the Gunicorn logs is if I put an error in my "app.py" file on purpose (like adding some random letters at the top of the file). From the moment my Flask server runs, no print-statements get through, even if I make one of the routes fail and then call it. Before introducing Gunicorn, I could not see any of the Flask print-statements either.

This is the Dockerfile I am using to build the image:

FROM continuumio/miniconda3
ENV PYTHONUNBUFFERED=TRUE
WORKDIR /app
COPY Calculation/conda.yaml .
RUN conda update -n base conda && \
    conda env create -f conda.yaml --force
COPY Calculation /app/Calculation
SHELL ["conda", "run", "-n", "envi", "/bin/bash", "-c"]
EXPOSE 5003
CMD conda run --no-capture-output -n envi gunicorn --workers 10 --bind 0.0.0.0:5000  --log-level DEBUG --chdir /app/Calculation app:app

My app.py file looks like this:

app = Flask(__name__)

print("test")

@app.route("/")
def index():
    print("Function to test the functionality of the API")
    return "Hello, world!"

if __name__ == "__main__":
    app.run(debug=False)
    print("Main method is started in app.py")

I tried adding ENV PYTHONUNBUFFERED=TRUE, ENV PYTHONUNBUFFERED=1, ENV PYTHONUNBUFFERED 1 to my Dockerfile, as you can see above. According to many sources this would be useful.

I redirected the output with the followign file called RedirectOutput.py. In every file that I imported it with "import RedirectOutput", the print-statements got written to the output.txt file. I could then reach the file via "kubectl exec -it <pod-name> -- /bin/bash".

import sys

MAX_FILE_SIZE = 1024 * 1024 * 1024  # 1 GB in bytes

file_size = os.stat('output.txt').st_size if os.path.exists('output.txt') else 0

if file_size < MAX_FILE_SIZE:
    sys.stdout = open('output.txt', 'a')
    sys.stderr = sys.stdout

I tried using all of the following lines of code:

app.logger.info("This is an info log message")
app.logger.debug("This is a debug log message")
app.logger.error("This is an error log message")
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')

sys.stdout.flush()
sys.stdin.flush()
sys.stderr.flush()

I found this stackoverflow thread, but I am unsure of how to add an error.log file and even then, I would like to be able to use "kubectl logs <podname>" to see the logs.

I found this github thread, which is why I use the --no-capture-output option in CMD conda in the dockerfile

0 Answers0