[英]How to deploy Django Application with Celery + Reddis on Google Cloud Run (docker-compose)
我有一個docker-compose.prod.yaml
包含我想部署到 Google Cloud Run的 7 項服務。 我喜歡無服務器解決方案的好處,據我了解,另外兩個選項是:
在 Google Cloud Run 上部署此類應用程序是否可能,甚至是可取的? 將不勝感激有關如何實現這一目標的一些指導。 我贊成簡單性和成本效益。
我預見到的一些潛在問題:
我確實承認 Google 提供了Cloud Tasks ,但是,這會將我們與 GCP 聯系起來,我們會錯過 Celery 的廣泛功能。
對docker-compose.prod.yaml
所做的更改之一將切換雲 SQL托管實例的db
容器。
version: '3.8'
services:
nginx:
build: ./compose/production/nginx
volumes:
- staticfiles:/app/staticfiles
- mediafiles:/app/mediafiles
ports:
- 80:80
- 5555:5555
- 15672:15672
depends_on:
- web
- flower
web:
build:
context: .
dockerfile: ./compose/production/django/Dockerfile
command: /start
volumes:
- staticfiles:/app/staticfiles
- mediafiles:/app/mediafiles
env_file:
- ./.env/.prod-sample
depends_on:
- redis
- db
db:
image: postgres:14-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_DB=hello_django
- POSTGRES_USER=hello_django
- POSTGRES_PASSWORD=hello_django
redis:
image: redis:6-alpine
celery_worker:
build:
context: .
dockerfile: ./compose/production/django/Dockerfile
image: django_celery_example_celery_worker
command: /start-celeryworker
volumes:
- staticfiles:/app/staticfiles
- mediafiles:/app/mediafiles
env_file:
- ./.env/.prod-sample
depends_on:
- redis
- db
celery_beat:
build:
context: .
dockerfile: ./compose/production/django/Dockerfile
image: django_celery_example_celery_beat
command: /start-celerybeat
volumes:
- staticfiles:/app/staticfiles
- mediafiles:/app/mediafiles
env_file:
- ./.env/.prod-sample
depends_on:
- redis
- db
flower:
build:
context: .
dockerfile: ./compose/production/django/Dockerfile
image: django_celery_example_celery_flower
command: /start-flower
volumes:
- staticfiles:/app/staticfiles
- mediafiles:/app/mediafiles
env_file:
- ./.env/.prod-sample
depends_on:
- redis
- db
volumes:
postgres_data:
staticfiles:
mediafiles:
這個 docker setup 大部分取自這個課程,非常方便開發。 作為一家以數據為中心的公司,我們對 GCP 最為熟悉,所以現在寧願堅持使用它。
這是配置文件結構的樣子:
├── compose
│ ├── local
│ │ └── django
│ │ ├── Dockerfile
│ │ ├── celery
│ │ │ ├── beat
│ │ │ │ └── start
│ │ │ ├── flower
│ │ │ │ └── start
│ │ │ └── worker
│ │ │ └── start
│ │ ├── entrypoint
│ │ └── start
│ └── production
│ ├── django
│ │ ├── Dockerfile
│ │ ├── celery
│ │ │ ├── beat
│ │ │ │ └── start
│ │ │ ├── flower
│ │ │ │ └── start
│ │ │ └── worker
│ │ │ └── start
│ │ ├── entrypoint
│ │ └── start
│ └── nginx
│ ├── Dockerfile
│ └── nginx.conf
├── django_celery_example
│ # files omitted for brevity
├── docker-compose.prod.yml
├── docker-compose.yml
├── manage.py
├── polls
│ # files omitted for brevity
└── requirements.txt
抱歉,但是當您說Redis和DB Container時,您無法在 Cloud Run 上運行它,但是我在某處讀到在Cloud Run上運行DB時總有一個解決方法。
Cloud Run 非常適合無狀態容器,而DB和Redis將狀態存儲在文件系統中,而 memory 則可能需要為Redis做一些計划,就像那里的 DB cloudSQL 一樣。
我預見到的一些潛在問題:
考慮到要運行的 Redis 和 DB 容器, GKE可能更合適,您可以根據需要部署和創建網絡並提供靈活性。
您可以使用Cloud bucket fuse或使用共享文件系統,或者使用SDK將文件上傳到 bucket。 但是,如果需要跨 POD 共享卷, ReadWrieMany與NFS將是最佳選擇。
您還可以使用Kompose ,它將您Docker compose轉換為Kube.netes YAML - Ref: Docker-compose 以部署到 Kube.netes
額外(腳手架和舵)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.