将 Elastic Beanstalk 环境从 AWS Linux 1 升级到 AWS Linux 2

Upgrading Elastic Beanstalk environment from AWS Linux 1 to AWS Linux 2

我在 AWS Linux 1 上有一个 Elastic Beanstalk 环境 运行 Python 3.6,我想将它切换到 Amazon Python 3.8 Linux 2.

我知道我可以使用 aws CLI update-environment 命令升级环境:

aws elasticbeanstalk update-environment --environment-name <ENV_NAME> --solution-stack-name "64bit Amazon Linux 2 v3.3.7 running Python 3.8"

但是,AWS Linux 2 使用不同的配置参数。我无法部署 AWS Linux 2 配置,因为它在 AWS Linux 1 上无效,我无法升级到 AWS Linux 2,因为我的配置无效。

我该如何升级,有没有办法就地升级?

配置差异

AWS Linux 2 对 Elastic Beanstalk 的工作方式和配置方式进行了大量更改。无论您是进行就地升级还是启动新环境,这里都列出了与升级前 运行 不同的内容。此处的大部分项目与 .ebextensions.

中的 Elastic Beanstalk 配置不同
  1. Python3.6 和 3.8 之间的子包依赖性存在差异。您应该在 Python 3.8 上测试您的需求文件并确保它兼容,尤其是如果您使用生成的 requirements.txt.

  2. AWS Linux 2 不再允许您使用 .ebextensions 中的 file 指令编写 Apache 配置。这些修改现在需要存在于 .platform/httpd/conf.

  3. 虚拟环境在 运行宁 container_commands 期间不再活动。任何使用您的代码的容器命令都需要先 source $PYTHONPATH/activate 运行。

  4. 生成的文件现在会在配置更改时被擦除,因此像 django 的 collectstatic .

    这样的命令
  5. 虽然 yum,但 Postgres 客户端不再正常可用。要安装它,您需要执行以下操作:

    packages:
        yum:
            amazon-linux-extras: []
    
    commands:
        01_postgres_activate:
            command: sudo amazon-linux-extras enable postgresql10
        02_postgres_install:
            command: sudo yum install -y postgresql-devel
    
  6. Apache 不再是默认的 Web 服务器(它是 Nginx)。要继续使用它,您需要在您的环境中将其指定为一个选项,例如:

    option_settings:
      aws:elasticbeanstalk:environment:proxy:
        ProxyServer: apache
    
  7. Modwsgi 已被 Gunicorn 取代。您拥有的任何 modwsgi 定制将不再有效,并且 WSGI 路径具有不同的格式:

    option_settings:
      aws:elasticbeanstalk:container:python:
        WSGIPath: config.wsgi:application
    
  8. 静态文件配置有不同的格式:

    option_settings:
      aws:elasticbeanstalk:environment:proxy:staticfiles:
        /static: staticfiles
    
  9. 您将选择加入高级健康报告。强烈建议添加 Elastic Beanstalk 运行状况检查:

    option_settings:
      aws:elasticbeanstalk:application:
        Application Healthcheck URL: /health-check/
    
  10. 应用程序现在通过 Gunicorn 运行 服务器上的端口 8000 并且 Apache/Nginx 只是代理对 Gunicorn 的请求。如果您正在进行 .

    等 apache 自定义,这很重要
  11. Apache 现在 运行 通过 systemctl 而不是 supervisord。如果您尝试重新启动 Apache,命令现在是 sudo systemctl restart httpd

  12. 如果你想在 sshed 到服务器时加载你的环境变量,你需要以不同的方式解析它们:

  13. 环境变量位于不同的位置并且具有不同的格式。要在通过 SSH 登录时访问它们,您需要将 jq: [] 添加到您的 yum 安装中。然后,运行以下命令或将它们添加到服务器的 bashrc(使用 .ebextensions 中的 file 指令)以加载环境变量并激活 python 虚拟环境:

    source <(/opt/elasticbeanstalk/bin/get-config environment | jq -r 'to_entries | .[] | "export \(.key)=\"\(.value)\""')
    source $PYTHONPATH/activate
    cd /var/app/current
    

通过启动新环境进行升级

要采用此升级路径,您不需要使用 Elastic Beanstalk 数据层(即您自己启动了 RDS 实例,而不是通过 Elastic Beanstalk)。

  1. 使用您的 AWS Linux 2 配置创建一个代码分支

  2. 在 AWS 上启动一个新的 Elastic Beanstalk 环境 Linux 2.

  3. 从之前的环境中复制环境变量。

  4. 允许从新环境访问您的数据库(添加新环境的服务器安全组作为数据库安全组的入口规则的目标)

  5. 在新环境中设置 SSL。

  6. 将 AWS Linux 2 代码分支部署到新环境。

  7. 测试这个新环境,忽略浏览器证书警告(或设置一个临时 DNS 条目来测试它)。

  8. 切换 DNS 条目以指向您的新环境或使用 AWS 的 CNAME swap feature on the two environemnts.

  9. 在您的新环境 运行 足够长的时间没有问题后,终止您的旧环境。

就地升级

有一种方法可以就地进行升级,但您的网站会在几分钟内显示“502 Bad Gateway”。为此,您需要与 AWS Linux 1 和 AWS Linux 2 环境兼容的 EB 配置。

对于 Python,您可以使用小型 Flask 应用程序和四部分部署来完成此操作。

第 1 部分:部署与两个平台兼容的占位符应用程序

  1. flask 添加到您的 requirements.txt(如果它还没有)。

  2. 删除.ebextensions

    中的所有文件
  3. 制作.ebextensions/01.config:

    option_settings:
      aws:elasticbeanstalk:container:python:
        WSGIPath: wsgi_shim.py
    
  4. 使wsgi_shim.py:

    from flask import Flask
    
    application = Flask(__name__)
    
    @application.route("/")
    @application.route("/<path:path>/")
    def hello_world(path=None):
        return "This site is currently down for maintenance"
    
  5. [如果使用 ,更改负载平衡器以通过 HTTP 将所有流量发送到服务器。]

  6. eb deploy

第 2 部分:将平台升级到 AWS Linux2

  1. [如果您在 elastic beanstalk 中配置了任何静态路由,请将其删除。]

  2. 升级你的eb环境

    # Get list of solution stacks
    aws elasticbeanstalk list-available-solution-stacks --output=json --query 'SolutionStacks' --region us-east-1
    
    # Use one of the above options here
    aws elasticbeanstalk update-environment --environment-name <ENV_NAME> --solution-stack-name "64bit Amazon Linux 2 v3.3.7 running Python 3.8"
    

第 3 部分:将您的主要应用程序部署到 AWS Linux2

  1. .ebextensions/01.config 替换为您的新 AWS Linux 2 配置。

  2. :

    重写引擎开启 重写规则 (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

  3. eb deploy

第 4 部分:清理

  1. [如果使用 ,请将负载均衡器更改回通过 HTTPS 向服务器发送流量。]

  2. 删除 wsgi_shim.py 并从 requirements.txt 中移除烧瓶(除非是烧瓶项目)。

  3. eb deploy