使用 app.yaml 配置应用

App Engine 应用是使用 app.yaml 文件进行配置的,该文件包含 CPU、内存、网络和磁盘资源设置、扩缩设置以及环境变量等其他常规设置。

app.yaml 文件简介

您可以在 app.yaml 文件中指定 Go 应用的运行时配置,包括版本和网址。该文件充当特定服务版本的部署描述符。

您必须先为应用的 default 服务创建 app.yaml 文件,然后才能为其他服务创建和部署 app.yaml 文件。

以下 app.yaml 演示了如何为 Go 1.15 及更早版本和 1.18 及更高版本配置应用。请注意,您必须更新 app.yaml 文件才能使用新版本。如需详细了解如何使用新版本,请参阅 Go 运行时

v1.18 及更高版本

# Copyright 2019 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
#
#     https://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: go
env: flex

runtime_config:
  operating_system: 'ubuntu22'
  runtime_version: '1.18'

# 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

v1.15 及更低版本

# Copyright 2023 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
#
#     https://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: go
env: flex

# 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 文件中定义几个元素,具体取决于应用服务的复杂程度。以下示例演示了简单的 Go 应用可能需要在柔性环境中定义的元素:

您可以为 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 文件的结构,请参阅设计 Web 服务的结构

所有配置元素

如需查看此配置文件中所有受支持的元素的完整列表,请参阅 app.yaml 参考文档