Docker 中的 Celery kombu.exceptions.ContentDisallowed

Nit*_*ese 5 python celery celery-task docker kubernetes

我正在使用 celery 和 fastAPI。

获取无法解码消息正文:ContentDisallowed('拒绝反序列化 json 类型的不受信任内容 (application/json)')在 docker 中运行时,在没有 docker 的本地计算机上运行相同的内容时,没有问题。

其配置如下。

celery_app = Celery('cda-celery-tasks',
                    broker=CFG.BROKER_URL,
                    backend=CFG.BACKEND_URL,
                    include=['src.tasks.tasks']
                    )

celery_app.conf.task_serializer = 'pickle'
celery_app.conf.result_serializer = 'pickle'
celery_app.conf.accept_content = ['pickle']
celery_app.conf.enable_utc = True

Run Code Online (Sandbox Code Playgroud)

在 docker 中运行时,我不断收到错误

FROM python:3.8
WORKDIR /app

COPY . .

RUN pip3 install poetry
ENV PATH="/root/.poetry/bin:$PATH"

RUN poetry install
Run Code Online (Sandbox Code Playgroud)

使用 kubernetes 中的以下命令启动 celery。

poetry run celery -A src.infrastructure.celery_application worker --loglevel=INFO --concurrency 2

运行时我不断收到错误

无法解码消息正文:ContentDisallowed('拒绝反序列化 json 类型的不受信任内容 (application/json)')

body: '{"method": "enable_events", "arguments": {}, "destination": null, "pattern": null, "matcher": null}' (99b)
Traceback (most recent call last):
  File "/root/.cache/pypoetry/virtualenvs/cda-9TtSrW0h-py3.8/lib/python3.8/site-packages/kombu/messaging.py", line 620, in _receive_callback
    decoded = None if on_m else message.decode()
  File "/root/.cache/pypoetry/virtualenvs/cda-9TtSrW0h-py3.8/lib/python3.8/site-packages/kombu/message.py", line 194, in decode
    self._decoded_cache = self._decode()
  File "/root/.cache/pypoetry/virtualenvs/cda-9TtSrW0h-py3.8/lib/python3.8/site-packages/kombu/message.py", line 198, in _decode
    return loads(self.body, self.content_type,
  File "/root/.cache/pypoetry/virtualenvs/cda-9TtSrW0h-py3.8/lib/python3.8/site-packages/kombu/serialization.py", line 242, in loads
    raise self._for_untrusted_content(content_type, 'untrusted')
kombu.exceptions.ContentDisallowed: Refusing to deserialize untrusted content of type json (application/json)
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我可能的原因和解决方案吗?如果我遗漏了任何内容,过度或低估了某个特定点,请在评论中告诉我。非常感谢您抽出时间。

Nit*_*ese 4

使用accept_content类型配置celery_app似乎可以解决这个问题:

celery_app.conf.accept_content = ['application/json', 'application/x-python-serialize', 'pickle']

Run Code Online (Sandbox Code Playgroud)

  • 伟大的!对于 Django 3.2,我使用了变量 CELERY_ACCEPT_CONTENT (2认同)