Celery 工人无法连接到 docker 实例上的 redis [英] Celery workers unable to connect to redis on docker instances
问题描述
我有一个运行 Django 应用程序的 dockerized 设置,我在其中使用 Celery 任务.Celery 使用 Redis 作为代理.
I have a dockerized setup running a Django app within which I use Celery tasks. Celery uses Redis as the broker.
版本控制:
- Docker 版本 17.09.0-ce,构建 afdb6d4
- docker-compose 1.15.0 版,构建 e12f3b9
- Django==1.9.6
- django-celery-beat==1.0.1
- 芹菜==4.1.0
- 芹菜[redis]
- redis==2.10.5
问题:
我的 celery 工人似乎无法连接到位于 localhost:6379 的 redis 容器.我可以通过 telnet 登录到指定端口上的 redis 服务器.我能够验证 redis-server 正在容器上运行.
My celery workers appear to be unable to connect to the redis container located at localhost:6379. I am able to telnet into the redis server on the specified port. I am able to verify redis-server is running on the container.
当我手动连接到 Celery docker 实例并尝试使用命令 celery -A backend worker -l info
创建一个 worker 时,我收到通知:
When I manually connect to the Celery docker instance and attempt to create a worker using the command celery -A backend worker -l info
I get the notice:
[2017-11-13 18:07:50,937: ERROR/MainProcess] 消费者:无法连接到 redis://localhost:6379/0:连接到 localhost:6379 时出现错误 99.无法分配请求的地址..4.00 秒后重试...
注意事项:
我可以通过 6379 端口 telnet 到 redis 容器.在 redis 容器上,redis-server 正在运行.
I am able to telnet in to the redis container on port 6379. On the redis container, redis-server is running.
还有什么我遗漏的吗?我已经在兔子洞里走了很远,但感觉我错过了一些非常简单的东西.
Is there anything else that I'm missing? I've gone pretty far down the rabbit hole, but feel like I'm missing something really simple.
Docker 配置文件:
docker-compose.common.yml 这里
docker-compose.dev.yml 这里
docker-compose.common.yml here
docker-compose.dev.yml here
推荐答案
当你使用 docker-compose 时,你不会使用 localhost
进行容器间通信,你会使用容器的组合分配主机名.在这种情况下,您的 redis 容器的主机名是 redis
.services:
下的顶级元素是您的默认主机名.
When you use docker-compose, you aren't going to be using localhost
for inter-container communication, you would be using the compose-assigned hostname of the container. In this case, the hostname of your redis container is redis
. The top level elements under services:
are your default host names.
所以要让celery连接redis,你应该试试redis://redis:6379/0
.由于协议和服务名称相同,我将详细说明:如果您在 docker-compose 中将您的 redis 服务命名为butter-pecan-redis",您将改为使用 redis://butter-pecan-redis:6379/0
.
So for celery to connect to redis, you should try redis://redis:6379/0
. Since the protocol and the service name are the same, I'll elaborate a little more: if you named your redis service "butter-pecan-redis" in your docker-compose, you would instead use redis://butter-pecan-redis:6379/0
.
此外,docker-compose.dev.yml 似乎没有 celery 和 redis 在公共网络上,这可能会导致它们无法相互看到.我相信他们至少需要共享一个网络才能解析各自的主机名.
Also, docker-compose.dev.yml doesn't appear to have celery and redis on a common network, which might cause them not to be able to see each other. I believe they need to share at least one network in common to be able to resolve their respective host names.
docker-compose 中的网络 在前几段中有一个例子,有一个docker-compose.yml 看看.
Networking in docker-compose has an example in the first handful of paragraphs, with a docker-compose.yml to look at.
这篇关于Celery 工人无法连接到 docker 实例上的 redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!