理解失败的部署

0

【以下的问题经过翻译处理】 我是 Lightsail 的新手,我正在尝试调试失败的部署,而且我正感觉一头雾水。如果有任何想法,将不胜感激!

我有两个镜像:一个基于Python-alpine构建的Flask/Gunicorn镜像和一个 Nginx 镜像。在本地,我可以使用 docker-compose 启动它们并且它们工作得很好。

但在 Lightsail 中,我所知道的是我的 Flask 镜像“耗时太长”:

[17/Mar/2023:24:11:33] [deployment:14] Creating your deployment
[17/Mar/2023:24:13:05] [deployment:14] Started 1 new node
[17/Mar/2023:24:14:39] [deployment:14] Started 1 new node
[17/Mar/2023:24:15:54] [deployment:14] Started 1 new node
[17/Mar/2023:24:16:14] [deployment:14] Took too long

我尝试过但没有成功的方法如下:

参考 https://repost.aws/questions/QUrqo_fzNTQ5i1E08tT1uM7g/lightsail-container-took-too-long-to-deploy-all-of-a-sudden-nothing-in-logs

  • 将Gunicorn的日志级别设置为DEBUG。有时我可以看到Gunicorn进程被SIGTERM信号终止,但上面的“耗时太长”部分没有额外的信息。
  • 将健康检查设置为 300 秒,以防这是 ​​SIGTERM 的来源。没有效果。
  • 将容量从“nano”提升到“micro”再到“”。没有效果。

参考 https://repost.aws/questions/QU8i3bF2BZQZiwKfxGw5CfgQ/how-to-deploy-amazon-linux-on-a-lightsail-container-service

  • 确保将启动命令粘贴到正确的“启动命令”表单输入中。但没有效果。

也许我漏掉了一些明显的东西。

更新: 我已经配置了Nginx代理将请求转发到gunicorn,并提供静态内容。以下是Dockerfiles和docker-compose文件的内容:

Flask/Gunicorn Dockerfile:

FROM python:3.10-alpine

ENV POETRY_VERSION=1.2.2 \
    POETRY_VIRTUALENVS_IN_PROJECT=true \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

RUN apk add --no-cache curl \
    && curl -sSL https://install.python-poetry.org | POETRY_VERSION=$POETRY_VERSION python3 -

WORKDIR /src

# TODO: build wheel for pipeline
COPY . .

RUN /root/.local/bin/poetry install --only main

CMD . /src/.venv/bin/activate && gunicorn -w 2 --log-level debug --bind=0.0.0.0:8080 'app:app'

Nginx Dockerfile:

FROM nginx:alpine

COPY ./nginx.conf /etc/nginx/nginx.conf

docker-compose.yml

version: "3.3"
services:
  web:
    image: myDockerHub/myImage
    restart: always
    volumes:
      - static_volume:/src/my_project/static
    ports:
      - "8080:80"

  nginx:
    image: myDockerHub/nginx
    restart: always
    volumes:
      - static_volume:/src/my_project/static
    depends_on:
      - web
    ports:
      - "80:80"

volumes:
  static_volume:

profile picture
专家
已提问 5 个月前16 查看次数
1 回答
0

【以下的回答经过翻译处理】 从 Gunicorn 或 Flask 或 nginx 没有任何有意义的日志输出来看,我认为可能存在一些入口点或启动命令配置错误。看起来,在尝试启动运行容器的计算节点 3 次之后,系统认为此次部署失败。

我尝试搭建一个环境,使用类似于您在帖子中描述的配置

这是我创建的目录和文件的列表,以及每个文件的内容:

static/index.html
static/readme.txt
hello.py
Dockerfile-flask
nginx.conf
Dockerfile-nginx

static/index.html:

<!DOCTYPE html>
<html>
<head><title>Static Content Root</title></head>
<body><p>Your static content is here.</p></body>
</html>

static/readme.txt:

Did you read me??

hello.py:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World!"

Dockerfile-flask:

FROM python:alpine
RUN pip3 install Flask gunicorn
RUN mkdir /app
ADD hello.py /app

# nginx container will be sending traffic here via localhost:5555,
# see nginx.conf
EXPOSE 5555

WORKDIR /app
ENTRYPOINT ["gunicorn", "--bind", "localhost:5555", "--access-logfile", "-", "hello:app"]

nginx.conf:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /static;
        index  index.html;
    }

    location /flask/ {
        # forward /flask/... requests to the gunicorn+flask container running on
        # the same compute node, listening on port 5555.
        proxy_pass http://localhost:5555/;
    }
}

Dockerfile-nginx:

FROM nginx:alpine
COPY static /static/
COPY nginx.conf /etc/nginx/conf.d/default.conf

接下来,我在本地构建了Docker镜像。标记为"gunicorn-flask-app"的是Flask应用程序镜像。标记为"nginx-ingress"的是Nginx镜像,其中包含一些静态内容,并配置为将一部分流量发送到gunicorn/flask容器:

docker build . -t gunicorn-flask-app --platform=linux/amd64 -f Dockerfile-flask
docker build . -t nginx-ingress --platform=linux/amd64 -f Dockerfile-nginx

现在让我们创建容器服务:

aws lightsail create-container-service --service-name example --power nano --scale 1

它需要一些时间才能部署完成。我使用命令 aws lightsail get-container-services --service-name example 来查看它是否已经完成,或在Lightsail控制台上查看。

让我们将本地构建的镜像推送到Lightsail。

推送Flask镜像:

aws lightsail push-container-image --service-name example --label flask --image gunicorn-flask-app

输出(请注意,镜像引用中的序列号48对您而言可能完全不同):

... 
Digest: sha256:d46b4439329e8de4c608cf4f61865beea9b988fe732309c8a1ff0a83cd16b82c
Image "gunicorn-flask-app" registered.
Refer to this image as ":example.flask.48" in deployments.

推送 nginx 镜像:

aws lightsail push-container-image --service-name example --label nginx --image nginx-ingress

输出:

... 
Digest: sha256:bd008e587fcc59cc71f4421af0b0397a7a04d56e2ac2f4d0fc6e52a1adcc6ec6
Image "nginx-ingress" registered.
Refer to this image as ":example.nginx.47" in deployments.

完成所有这些后,我们现在可以创建部署:

aws lightsail create-container-service-deployment --service-name example \
--containers '{
    "app": {
        "image": ":example.flask.latest"
    },
    "ingress": {
        "image": ":example.nginx.latest",
        "ports": {"80": "HTTP"}
    }
}' \
--public-endpoint '{"containerName": "ingress", "containerPort": 80}'

随着部署的进行,我看到运行 gunicorn+flask 应用程序的"app"容器的日志如下:

[deployment:3] Creating your deployment
... [1] [INFO] Starting gunicorn 20.1.0
... [1] [INFO] Listening at: http://127.0.0.1:5555 (1)
... [1] [INFO] Using worker: sync
... [7] [INFO] Booting worker with pid: 7
[deployment:3] Reached a steady state

...以及运行我们的 nginx 服务器的“ingress”容器日志:

[deployment:3] Creating your deployment
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
...
/docker-entrypoint.sh: Configuration complete; ready for start up
... [notice] 1#1: using the "epoll" event method
... [notice] 1#1: nginx/1.23.3
... [notice] 1#1: built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r4)
... [notice] 1#1: OS: Linux 5.10.167-147.601.amzn2.x86_64
... [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1024:4096
... [notice] 1#1: start worker processes
... [notice] 1#1: start worker process 29
... [notice] 1#1: start worker process 30
[deployment:3] Reached a steady state

当我通过公共终端节点访问我的服务 https://example.qwerty1234567.us-west-2.cs.amazonlightsail.com/时,我可以看到它按预期返回静态内容。当我访问 https://example.qwerty1234567.us-west-2.cs.amazonlightsail.com/flask/时,我可以看到来自Flask应用程序的 "Hello, World!" 消息。

注意:URL中的 "qwerty1234567.us-west-2" 部分会因个人账户和部署服务的区域而异。您可以在Lightsail控制台的容器服务详细信息中找到此URL,或通过运行命令 aws lightsail get-container-services --service-name example 来获取。

先决条件

我在安装了 Docker Desktop & Homebrew 的 Mac 上执行此操作。

为了将本地构建的镜像推送到Lightsail容器服务部署中使用,您需要安装以下:

AWS CLI: brew install awscli lightsailctl: brew install aws/tap/lightsailctl

profile picture
专家
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则