简体   繁体   English

Kubernetes 集群中的 FileNotFoundError Python-Flask/Docker

[英]FileNotFoundError Python-Flask/Docker in Kubernetes Cluster

I am having problems with opening files in my Flask app, running in Docker container in a Kubernetes cluster.我在 Flask 应用程序中打开文件时遇到问题,在 Kubernetes 集群的 Docker 容器中运行。

Whenever I try to open a file i directory I am getting a FileNotFoundError .每当我尝试打开文件 i 目录时,我都会收到FileNotFoundError S3FS Object Storage is provisioned and the PersistentVolumeclaim is successfully mounted to my pod, so no issue there. S3FS 对象存储已配置,并且 PersistentVolumeclaim 已成功挂载到我的 pod,因此没有问题。

However, when trying to read a file from the mounted storage I get the said error.但是,当尝试从已安装的存储中读取文件时,出现上述错误。 Here is how I read the file.这是我读取文件的方式。 The current_dir is /var/www current_dir/var/www

current_dir = os.path.dirname(__file__)
abs_path = os.path.join(current_dir, path)
with open(abs_path, "rb") as file:
    bin_data = file.read()

Additonally I have also tried all of the variants below, but none seem to work:此外,我还尝试了以下所有变体,但似乎都不起作用:

data_path = pathlib.Path('/var/www/uploads/')
abs_path = data_path / path_id

data_path = pathlib.Path('uploads/')
abs_path = data_path / path_id

abs_path = current_dir + '/uploads' + path_id

tmp = str('/uploads/' + path_id).split('/')
abs_path = os.path.join(os.path.dirname(__file__), *tmp)

The error I am getting is following:我得到的错误如下:

[2020-04-02 18:16:49,359] ERROR in app: Exception on /internal/tusd [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/var/www/api_controller.py", line 95, in tusd_handler
    hexdigest = hash_file(str(upload_id), hash_type)
  File "/var/www/file_hasher.py", line 17, in hash_file
    with open(abs_path, "rb") as file:
FileNotFoundError: [Errno 2] No such file or directory: '/var/www/uploads/f7f1c686fbc40649879e8aee7f529c96'

However, when I exec -it into the container.但是,当我exec -it进入容器时。 The file, among others is present.该文件等存在。

$ kubectl exec hasher-savoury-695669b858-nwg5j -- ls /var/www/uploads

f7f1c686fbc40649879e8aee7f529c96
f7f1c686fbc40649879e8aee7f529c96.info

Here is the Dockerfile I have been using.这是我一直在使用的 Dockerfile。 The commented fields are options I have tried out in order to get it working.注释字段是我为了让它工作而尝试的选项。

FROM python:3.7-alpine as base

ENV GROUP_ID=1000 \
    USER_ID=1000

RUN apk upgrade && apk update \
    && apk --no-cache add openssl-dev openssl
RUN apk --no-cache --update add --virtual build-dependencies gcc g++ make libffi-dev

RUN mkdir /var/www && mkdir /var/www/uploads
COPY . /var/www
WORKDIR /var/www

RUN pip install --upgrade pip && pip install -r requirements.txt && apk del build-dependencies
RUN pip install gunicorn

#RUN addgroup -g $GROUP_ID savoury
#RUN adduser -D -u $USER_ID -G savoury savoury -s /bin/sh \
#    && chown savoury:savoury -R var/www/

#USER savoury

EXPOSE 5080

ENTRYPOINT ["gunicorn"]
CMD ["-w", "4", "--bind", "0.0.0.0:5080", "wsgi"]

I have tried reading these files in Java an have succesfully read them.我曾尝试用 Java 读取这些文件并成功读取它们。 I have also tried to ´´´exec´´´ into the container and was able to perform this easily in the python console.我还尝试“执行”到容器中,并且能够在 python 控制台中轻松执行此操作。 I have no idea why the script above is not working.我不知道为什么上面的脚本不起作用。

Any help is greatly appreciated!任何帮助是极大的赞赏!

The reason behind the error was in fact that the file was not found.错误背后的原因实际上是找不到文件。 It did not exist during time of reading because of improper timing of async events with the uploads server.由于上传服务器的异步事件计时不当,它在读取期间不存在。 The fault was undiscovered due to poor logging.由于记录不佳,该故障未被发现。 Lesson: Always log and monitor.教训:始终记录和监视。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM