本文介绍如何将Flask应用部署到函数计算。与传统的部署方法相比,您可将传统的Flask应用一键部署至远端直接用于生产。跳过购买机器等步骤的同时,还拥有了弹性伸缩、按量付费和免运维等特性。

前提条件

您已完成以下操作:

操作步骤

  1. 在目标目录中执行以下命名初始化项目。
    s init devsapp/start-flask -d start-flask
    -d用于指定项目名称,本文以项目名称start-flask为例,介绍如何将Flask迁移到函数计算。
    说明 若已存在该项目则跳过此步骤。
    输出示例:
     Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
    
     devsapp!start-flask@0.0.8.zip file decompression completed
     please select credential alias default
    
        ______ _           _
        |  ___| |         | |
        | |_  | | __ _ ___| | __
        |  _| | |/ _` / __| |/ /
        | |   | | (_| \__ \   <
        \_|   |_|\__,_|___/_|\_\
    
    
        Welcome to the start-flask application
         This application requires to open these services:
             FC : https://fc.console.aliyun.com/
             ACR: https://cr.console.aliyun.com/
         This application can help you quickly deploy the Flask project:
             Full yaml configuration: https://github.com/devsapp/flask#%E5%AE%8C%E6%95%B4yaml
             Flask development docs : https://dormousehole.readthedocs.io/en/latest/
         This application homepage: https://github.com/devsapp/start-flask
    
    
    ‍ Thanks for using Serverless-Devs
     You could [cd /test/start-flask] and enjoy your serverless journey!
    ️ If you need help for this example, you can use [s -h] after you enter folder.
     Document Star:https://github.com/Serverless-Devs/Serverless-Devs
  2. 执行以下命令,进入项目目录。
    cd start-flask
  3. 执行以下命令,部署项目。
    s deploy
    输出示例:
    [2021-08-30T17:20:12.059] [INFO ] [S-CLI] - Start ...
    [2021-08-30T17:20:12.070] [INFO ] [S-CLI] - Start the pre-action
    [2021-08-30T17:20:12.070] [INFO ] [S-CLI] - Action: pip3 install -t . -r requirements.txt
    WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
    Collecting Flask==1.1.2 (from -r requirements.txt (line 1))
      Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/f2/28/2a03252dfb9ebf377f40fba6a7841b47083260bf8bd8e737b0c6952df83f/Flask-1.1.2-py2.py3-none-any.whl (94kB)
        100% |████████████████████████████████| 102kB 12.3MB/s
    Collecting dataclasses==0.6 (from -r requirements.txt (line 2))
      Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/26/2f/1095cdc2868052dd1e64520f7c0d5c8c550ad297e944e641dbf1ffbb9a5d/dataclasses-0.6-py3-none-any.whl
    Collecting itsdangerous>=0.24 (from Flask==1.1.2->-r requirements.txt (line 1))
      Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/9c/96/26f935afba9cd6140216da5add223a0c465b99d0f112b68a4ca426441019/itsdangerous-2.0.1-py3-none-any.whl
    Collecting Jinja2>=2.10.1 (from Flask==1.1.2->-r requirements.txt (line 1))
      Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/80/21/ae597efc7ed8caaa43fb35062288baaf99a7d43ff0cf66452ddf47604ee6/Jinja2-3.0.1-py3-none-any.whl (133kB)
        100% |████████████████████████████████| 143kB 11.7MB/s
    Collecting Werkzeug>=0.15 (from Flask==1.1.2->-r requirements.txt (line 1))
      Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/bd/24/11c3ea5a7e866bf2d97f0501d0b4b1c9bbeade102bb4b588f0d2919a5212/Werkzeug-2.0.1-py3-none-any.whl (288kB)
        100% |████████████████████████████████| 296kB 16.6MB/s
    Collecting click>=5.1 (from Flask==1.1.2->-r requirements.txt (line 1))
      Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/76/0a/b6c5f311e32aeb3b406e03c079ade51e905ea630fc19d1262a46249c1c86/click-8.0.1-py3-none-any.whl (97kB)
        100% |████████████████████████████████| 102kB 34.5MB/s
    Collecting MarkupSafe>=2.0 (from Jinja2>=2.10.1->Flask==1.1.2->-r requirements.txt (line 1))
      Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/fc/d6/57f9a97e56447a1e340f8574836d3b636e2c14de304943836bd645fa9c7e/MarkupSafe-2.0.1-cp36-cp36m-manylinux1_x86_64.whl
    Collecting importlib-metadata; python_version < "3.8" (from click>=5.1->Flask==1.1.2->-r requirements.txt (line 1))
      Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/c8/4e/8d042c16307f3ae727419c96e14cdc02064a0a06d5e716d02060c860fcf4/importlib_metadata-4.8.0-py3-none-any.whl
    Collecting zipp>=0.5 (from importlib-metadata; python_version < "3.8"->click>=5.1->Flask==1.1.2->-r requirements.txt (line 1))
      Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/92/d9/89f433969fb8dc5b9cbdd4b4deb587720ec1aeb59a020cf15002b9593eef/zipp-3.5.0-py3-none-any.whl
    Collecting typing-extensions>=3.6.4; python_version < "3.8" (from importlib-metadata; python_version < "3.8"->click>=5.1->Flask==1.1.2->-r requirements.txt (line 1))
      Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/2e/35/6c4fff5ab443b57116cb1aad46421fb719bed2825664e8fe77d66d99bcbc/typing_extensions-3.10.0.0-py3-none-any.whl
    Installing collected packages: itsdangerous, MarkupSafe, Jinja2, dataclasses, Werkzeug, zipp, typing-extensions, importlib-metadata, click, Flask
    Successfully installed Flask-1.1.2 Jinja2-3.0.1 MarkupSafe-2.0.1 Werkzeug-2.0.1 click-8.0.1 dataclasses-0.6 importlib-metadata-4.8.0 itsdangerous-2.0.1 typing-extensions-3.10.0.0 zipp-3.5.0
    [2021-08-30T17:20:14.760] [INFO ] [S-CLI] - End the pre-action
      devsapp!flask@0.0.8.zip file decompression completed
     Using web framework type: nas, If you want to deploy with container, you can [s cli fc-default set web-framework container] to switch.
    (node:12474) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
      End of request
      Deployed.
      End of request
    [2021-08-30T17:20:37.829] [INFO ] [FC-DEPLOY] - Using region: cn-shenzhen
    [2021-08-30T17:20:37.831] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-08-30T17:20:37.831] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-08-30T17:20:37.831] [INFO ] [FC-DEPLOY] - Using accessKeySecret: ldHUiJLoGto****
     Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-08-30T17:20:38.461] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-flask exists
    [2021-08-30T17:20:38.715] [INFO ] [FC-DEPLOY] - Setting role: AliyunFCDefaultRole
    [2021-08-30T17:20:39.138] [INFO ] [RAM] - Checking Role AliyunFCDefaultRole exists
    [2021-08-30T17:20:39.306] [INFO ] [RAM] - Updating role: AliyunFCDefaultRole
    [2021-08-30T17:20:39.398] [INFO ] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists
    [2021-08-30T17:20:39.530] [INFO ] [FC-DEPLOY] - Using vpcConfig: auto: fc will try to generate related vpc resources automatically
    [2021-08-30T17:20:40.327] [INFO ] [VPC] - Getting vpc: fc-deploy-component-generated-vpc-cn-shenzhen
    [2021-08-30T17:20:40.448] [INFO ] [VPC] - Getting vswitch: fc-deploy-component-generated-vswitch-cn-shenzhen
    [2021-08-30T17:20:40.572] [INFO ] [VPC] - Getting securityGroup: fc-deploy-component-generated-securityGroup-cn-shenzhen
    [2021-08-30T17:20:40.768] [INFO ] [FC-DEPLOY] - Generated vpcConfig:
    securityGroupId: sg-wz92ctigpi221smc****
    vSwitchId: vsw-wz9qnuult4q5g4f7n****
    vpcId: vpc-wz9x9bzs0wtvjgt6n****
    
    [2021-08-30T17:20:40.769] [INFO ] [FC-DEPLOY] - Using nasConfig: auto: fc will try to generate related nas file system automatically
    [2021-08-30T17:21:06.776] [INFO ] [FC-DEPLOY] - Generated nasConfig:
    groupId: 10003
    mountPoints:
      - fcDir: /mnt/auto
        nasDir: /serverless-devs-flask
        serverAddr: example.com
    userId: 10003
    
    [2021-08-30T17:21:06.778] [INFO ] [FC-DEPLOY] - Checking Function flask exists
    [2021-08-30T17:21:06.903] [WARN ] [FC-DEPLOY] - Image registry.cn-shenzhen.aliyuncs.com/web-framework/python3:0.0.1 dose not exist locally.
    Maybe you need to run 's build' first if it dose not exist remotely.
    [2021-08-30T17:21:06.907] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-flask exists
    [2021-08-30T17:21:06.992] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-flask exists
    [2021-08-30T17:21:07.099] [INFO ] [FC-DEPLOY] - Creating service: serverless-devs-flask
    [2021-08-30T17:21:07.100] [INFO ] [FC-DEPLOY] - Creating function: flask
    [2021-08-30T17:21:07.100] [INFO ] [FC-DEPLOY] - Creating triggers: ["serverless-devs-flask"]
      Make service serverless-devs-flask success.
      Make function serverless-devs-flask/flask success.
      Make trigger serverless-devs-flask/flask/serverless-devs-flask success.
    [2021-08-30T17:21:09.939] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-flask exists
    [2021-08-30T17:21:10.009] [INFO ] [FC-DEPLOY] - Checking Function flask exists
    [2021-08-30T17:21:10.129] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-flask exists
    [2021-08-30T17:21:10.295] [INFO ] [FC-DEPLOY] - Creating custom domain: flask.serverless-devs-flask.188077086902****.cn-shenzhen.fc.devsapp.net
    [2021-08-30T17:21:11.136] [INFO ] [FC-DOMAIN] - Creating custom domain: flask.serverless-devs-flask.188077086902****.cn-shenzhen.fc.devsapp.net
    
    There is auto config in the service: serverless-devs-flask
    [2021-08-30T17:21:11.714] [INFO ] [WEB-FRAMEWORK] - nas component get src is: ./src
    Packing ...
    Package complete.
     Start uploading file: /mnt/2bfb748534-tpm20-cn-shenzhen/serverless-devs-flask/flask/src.zip
      File uploaded successfully: /mnt/2bfb748534-tpm20-cn-shenzhen/serverless-devs-flask/flask/src.zip
      upload done
    [2021-08-30T17:21:48.265] [INFO ] [FC-NAS] - unzipping file
    [2021-08-30T17:21:50.474] [INFO ] [FC-NAS] - unzip done
      Try container acceleration
    framework-test:
      region: cn-shenzhen
      serviceName: serverless-devs-flask
      functionName: flask
      customDomains:
        - >-
          http://flask.serverless-devs-flask.188077086902****.cn-shenzhen.fc.devsapp.net

执行结果

成功部署该项目后,您可以在执行输出中查看到函数计算生成的临时域名,通过该临时域名可以访问刚部署的应用,例如执行以下命令访问应用:

curl http://flask.serverless-devs-flask.188077086902****.cn-shenzhen.fc.devsapp.net
说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见绑定自定义域名