简体   繁体   English

K8s 集群上的 Django + Celery + Redis

[英]Django + Celery + Redis on a K8s cluster

I have a Django application deployed on a K8s cluster.我在 K8s 集群上部署了一个 Django 应用程序。 I need to send some emails (some are scheduled, others should be sent asynchronously), and the idea was to delegate those emails to Celery.我需要发送一些电子邮件(一些是预定的,另一些应该异步发送),我的想法是将这些电子邮件委托给 Celery。

So I set up a Redis server (with Sentinel) on the cluster, and deployed an instance for a Celery worker and another for Celery beat.因此,我在集群上设置了一个 Redis 服务器(带有 Sentinel),并为 Celery worker 部署了一个实例,为 Celery beat 部署了另一个实例。

The k8s object used to deploy the Celery worker is pretty similar to the one used for the Django application.用于部署 Celery worker 的 k8s 对象与用于 Django 应用程序的对象非常相似。 The main difference is the command introduced on the celery worker: ['celery', '-A', 'saleor', 'worker', '-l', 'INFO']主要区别在于 celery worker 上引入的命令: ['celery', '-A', 'saleor', 'worker', '-l', 'INFO']

Scheduled emails are sent with no problem (celery worker and celery beat don't have any problems connecting to the Redis server).发送预定的电子邮件没有问题(celery worker 和 celery beat 连接到 Redis 服务器没有任何问题)。 However, the asynchronous emails - "delegated" by the Django application - are not sent because it is not possible to connect to the Redis server ( ERROR celery.backends.redis Connection to Redis lost: Retry (1/20) in 1.00 second. [PID:7:uWSGIWorker1Core0] )但是,由于无法连接到 Redis 服务器( ERROR celery.backends.redis Connection to Redis lost: Retry (1/20) in 1.00 second. [PID:7:uWSGIWorker1Core0]因此不会发送异步电子邮件 - 由 Django 应用程序“委托” ERROR celery.backends.redis Connection to Redis lost: Retry (1/20) in 1.00 second. [PID:7:uWSGIWorker1Core0] )

Error 1:错误 1:

socket.gaierror: [Errno -5] No address associated with hostname 

Error 2:错误 2:

redis.exceptions.ConnectionError: Error -5 connecting to redis:6379. No address associated with hostname. 

The Redis server, Celery worker, and Celery beat are in a "redis" namespace, while the other things, including the Django app, are in the "development" namespace. Redis 服务器、Celery worker 和 Celery beat 位于“redis”命名空间中,而其他事物,包括 Django 应用程序,位于“development”命名空间中。

Here are the variables that I define:以下是我定义的变量:

- name: CELERY_PASSWORD
  valueFrom:
    secretKeyRef:
      name: redis-password
      key: redis_password
- name: CELERY_BROKER_URL
  value: redis://:$(CELERY_PASSWORD)@redis:6379/1
- name: CELERY_RESULT_BACKEND
  value: redis://:$(CELERY_PASSWORD)@redis:6379/1

I also tried to define CELERY_BACKEND_URL (with the same value as CELERY_RESULT_BACKEND ), but it made no difference.我也试图定义CELERY_BACKEND_URL (有相同的值CELERY_RESULT_BACKEND ),但它并没有区别。

What could be the cause for not connecting to the Redis server?无法连接到 Redis 服务器的原因可能是什么? Am I missing any variables?我是否缺少任何变量? Could it be because pods are in a different namespace?可能是因为 pod 位于不同的命名空间中吗?

Thanks!谢谢!

This is a community wiki answer posted for better visibility.这是为了更好的可见性而发布的社区 wiki 答案。 Feel free to expand it.随意扩展它。

Solution from @sofia that helped to fix this issue:来自@sofia 的解决方案有助于解决此问题:

need to use the same namespace for the Redis server and for the Django application.需要为 Redis 服务器和 Django 应用程序使用相同的命名空间。 In this particular case, change the namespace "redis" to "development" where the application is deployed.在这种特殊情况下,将命名空间“redis”更改为部署应用程序的“development”。

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

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