Kubernetes 集群中的 FileNotFoundError Python-Flask/Docker

FileNotFoundError Python-Flask/Docker in Kubernetes Cluster

我在 Flask 应用程序中打开文件时遇到问题,运行 在 Kubernetes 集群的 Docker 容器中。

每当我尝试打开目录中的文件时,我都会收到 FileNotFoundError。 S3FS 对象存储已配置且 PersistentVolumeclaim 已成功安装到我的 pod,因此没有问题。

但是,当我试图从挂载的存储中读取文件时,出现了上述错误。这是我读取文件的方式。 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()

此外,我还尝试了以下所有变体,但 none 似乎有效:

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)

我得到的错误如下:

[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'

但是,当我exec -it进入容器的时候。该文件等都存在。

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

f7f1c686fbc40649879e8aee7f529c96
f7f1c686fbc40649879e8aee7f529c96.info

这是我一直在使用的Docker文件。注释字段是我为使其正常工作而尝试过的选项。

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"]

我已尝试阅读 Java 中的这些文件并已成功阅读。 我也尝试过“执行”到容器中,并且能够在 python 控制台中轻松执行此操作。我不知道为什么上面的脚本不起作用。

非常感谢任何帮助!

错误背后的原因实际上是找不到文件。它在读取期间不存在,因为与上传服务器的异步事件的时间不正确。由于日志记录不佳,故障未被发现。 经验教训:始终记录和监控。