首页 > 解决方案 > Flask:CSS 和 JS 不会在 kubernetes POD 中加载

问题描述

当我在我的 PC 上的 Docker 中托管一个 Flask 应用程序时,CSS 和 JS 可以从模板和 HTML 视图格式正确读取。当我在 Kubernetes POD 中托管相同的应用程序时,它不会读取 CSS 和 JS。

在下面的快照中,您可以看到结果。 CSS 和 JS 未加载的示例 请在下面找到应用程序的文件结构。视图 index.html 和 upload.html 扩展了 public/templates/public_template.html,其中包含 CSS 和 JS 库的导入。

你能告诉可能是什么原因吗?kubectl 和 Docker 所需的文件结构可能不同吗?

C:.
|   .gitignore
|   Dockerfile
|   upload_app-deploy.yaml
|   run.py
|
\---app
    |   app.py
    |   requirements.txt
    |   __init__.py
    |
    +---static
    |   +---css
    |   |       bootstrap.min.css
    |   |       style.css
    |   |
    |   +---img
    |   |       flask.png
    |   |
    |   \---js
    |           app.js
    |           bootstrap.bundle.min.js
    |           jquery.slim.min.js
    |
    \---templates
        +---admin
        |   \---templates
        \---public
            |   index.html
            |   upload.html
            |
            \---templates
                    public_template.html 

浏览器中的错误:

Failed to load resource: the server responded with a status of 403 ()
jquery.slim.min.js:1 Failed to load resource: the server responded with a status of 403 ()
(index):1 Refused to apply style from 'https://ais-bt-services.asml.com/static/css/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
app.js:1 Failed to load resource: the server responded with a status of 403 ()
(index):1 Refused to apply style from 'https://ais-bt-services.asml.com/static/css/bootstrap.min.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

Kubernetes 1 中的浏览器错误 Kubernetes 2 中的浏览器错误

Dockerfile:

# Python 3 running on Debian GNU/Linux 10 (buster)(ATM) (amd64)
FROM XXX/docker/python:3

# Fill directories
COPY . /app

# Switch default directory so f.e. you don't have to provide a full pwd of req.txt
WORKDIR /app/app

# Install python packages with fixed versions
RUN pip install --force-reinstall -r requirements.txt

# Execute an app: listener and handler
CMD ["python", "/app/app/app.py"]

请在下面找到目录的权限,它们对我来说似乎没问题,目录到 775,文件到 664 和 root 用户。会不会是用户在 Kubernetes 中运行后就不同了?或者也许还有其他想法? 权限

标签: pythoncsstemplatesflaskkubernetes

解决方案


推荐阅读