我非常接近使用 Docker Compose 和 ECS 找到一個不錯的設定,但還有一件小事。
場景是這樣的:
- 使用 Docker Compose 和 Docker Context 更新應用程式 (Django) 源代碼并部署到 ECS。
- ECS 為應用程式注冊一個新任務,并與舊任務一起啟動。
- 問題:Nginx 對舊容器進行健康檢查,當它被注銷時,nginx 開始拋出 502 錯誤并重新啟動任務,導致停機和不可用。
- Nginx 再次啟動并對新容器進行健康檢查,應用程式再次運行,但如上所述出現意外停機。
我需要在這里做一些配置嗎?我錯過了什么嗎?
docker-compose.yml 供參考:
services:
web:
image: # Image from ECR, built from GH-action.
command: gunicorn core.wsgi:application --bind 0.0.0.0:8000
environment:
# ...
volumes:
# ...
deploy:
replicas: 1
nginx:
image: # Image from ECR, kept static
ports:
- "80:80"
volumes:
# ...
depends_on:
- web
deploy:
replicas: 1
uj5u.com熱心網友回復:
所以事實證明這是一個相當大的挑戰。底線是 ECS 任務在運行時無法更新。所以我們需要重新啟動任務或使用execute-command手動更新它。
我嘗試了這種jwilder/nginx-proxy方法,但是 Fargate 無法做到這一點,因為該啟動型別的卷安裝方式。
我最終為我的 nginx 容器使用了 sidecar 模式,但是,目前沒有可用于帶有 Compose-CLI 的 sidecar 的解決方案(請參閱https://github.com/docker/compose-cli/issues/1566),所以我不得不以x-aws-cloudformation稍微凌亂的方式使用覆寫層。
所以首先我們只需洗掉 nginx 服務:
version: "3.9"
services:
web:
image: # django-app
command: gunicorn core.wsgi:application --bind 0.0.0.0:8000
environment:
# ...
volumes:
# ...
ports:
- "80:80" # Move ports into this service so we get the ALB
deploy:
replicas: 1
運行 convert 命令以獲取生成的 CloudFormation 模板:
docker compose covert > cfn.yml
然后添加x-aws-cloudformation 覆寫:
x-aws-cloudformation:
Resources:
WebTaskDefinition:
Properties:
ContainerDefinitions:
# Generated container definitions, copy/paste from cfn.yml
# Only change ContainerPort for web:
- # ...Web_ResolvConf_InitContainer
- # ...Web-container
PortMappings:
- ContainerPort: 8000
# The nginx sidecar:
- DependsOn:
- Condition: SUCCESS
ContainerName: Web_ResolvConf_InitContainer
- Condition: START
ContainerName: web
Essential: true
Image: # nginx
LogConfiguration:
# ...
MountPoints:
# ...
Name: nginx
PortMappings:
- ContainerPort: 80
# We also need to tell the load-balancer to reference the nginx container
WebService:
Properties:
LoadBalancers:
- ContainerName: nginx
ContainerPort: 80
TargetGroupArn:
Ref: WebTCP80TargetGroup
最后,我們需要稍微更改一下 nginx 配置
# BEFORE
upstream app_server {
server web:8000 fail_timeout=0;
}
# AFTER
upstream app_server {
server 0.0.0.0:8000 fail_timeout=0;
}
不漂亮,但它有效。滾動更新現在將按預期實作零停機。希望這種模式會隨著 Compose-CLI 的發展而得到改進!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/403240.html
標籤:
