使用 app.yaml 設定應用程式

App Engine 應用程式是使用 app.yaml 檔案進行設定,其中包含 CPU、記憶體、網路和磁碟資源、資源調度,以及包含環境變數在內的其他一般設定。

app.yaml 檔案簡介

您可以在 app.yaml 檔案中指定 Python 應用程式的執行階段設定,包括版本和網址。這個檔案可做為特定服務版本的部署描述元。

您必須先為應用程式的 default 服務建立 app.yaml 檔案,才能建立及部署其他服務的 app.yaml 檔案。

以下 app.yaml 示範如何設定應用程式,以便使用最新版的 Python。如要使用任何其他 支援的 Python 版本,您必須更新 app.yaml 檔案。詳情請參閱「Python 執行階段」。

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
  operating_system: ubuntu22

# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

視應用程式服務的複雜程度而定,您或許只需要在對應的 app.yaml 檔案中定義一些元素。下列範例示範簡易的 Python 應用程式在彈性環境中可能需要具備的條件:

您可以為 app.yaml 檔案指定不重複的名稱,但稍後必須一併使用部署指令指定檔案名稱。舉例來說,如果您將 app.yaml 檔案命名為 service-name-app.yamlapp.flexible.yaml,就必須使用下列其中一種方式來部署應用程式:

gcloud app deploy service-name-app.yaml
gcloud app deploy app.flexible.yaml
如要進一步瞭解如何在應用程式中建構多個服務和 app.yaml 檔案,請參閱「建構網路服務」一文。

所有設定元素

如需這個設定檔中所有支援元素的完整清單,請參閱 app.yaml 參考資料