Compute Engine 上的 Go 使用入门


本教程介绍如何开始使用 Compute Engine。 请按照本教程向 Compute Engine 部署一个 Hello World Go Web 应用。如需获取 App Engine 入门方面的帮助,请参阅 App Engine 标准环境

目标

  • 使用 Cloud Shell 下载并部署 Hello World 示例应用。
  • 使用 Cloud Build 构建 Hello World 示例应用。
  • 将 Hello World 示例应用部署到单个 Compute Engine 实例。

费用

在本文档中,您将使用 Google Cloud 的以下收费组件:

您可使用价格计算器根据您的预计使用情况来估算费用。 Google Cloud 新用户可能有资格申请免费试用

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 启用 Compute Engine and Cloud Build API。

    启用 API

  5. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  6. 确保您的 Google Cloud 项目已启用结算功能

  7. 启用 Compute Engine and Cloud Build API。

    启用 API

  8. 在 Google Cloud 控制台中,在 Cloud Shell 中打开该应用。

    转到 Cloud Shell

    Cloud Shell 支持直接从浏览器通过命令行访问云端资源。

  9. 如果您同意克隆代码库,请点击确认以下载示例代码并切换到应用目录。

  10. 在 Cloud Shell 中,配置 gcloud CLI 以使用新的 Google Cloud 项目:
    # Configure gcloud for your project
    gcloud config set project YOUR_PROJECT_ID
    

在 Cloud Shell 中运行应用

  1. 在 Cloud Shell 中,启动本地网络服务器:

    go build -o app
    ./app
    
  2. 在 Cloud Shell 中,点击 Web 预览,然后选择在端口 8080 上预览。此时系统会打开一个新窗口,其中显示正在运行的应用。

    在网络浏览器中,您会看到 Hello, World!

  3. 准备好继续操作后,在 Cloud Shell 中按 Control + C 停止本地网络服务器。

部署到单个实例

此部分逐步介绍了如何在 Compute Engine 上运行应用的单个实例。

单实例部署。

您可以通过 Cloud Shell 将应用部署到单个 Compute Engine 实例虚拟机 (VM),以运行您的应用。

使用 Cloud Build 构建应用

您可以使用 Cloud Build 构建应用,将其压缩为 tar 文件,然后将该文件上传到 Cloud Storage 存储桶。存储分区是 Cloud Storage 中用于存放数据的基本容器。

  1. 在终端窗口中,创建一个 Cloud Storage 存储桶,其中 YOUR_BUCKET_NAME 表示存储桶的名称:

    gsutil mb gs://YOUR_BUCKET_NAME
    

    您可以为 Cloud Storage 存储桶选择任何名称。推荐使用项目 ID 作为存储桶名称。存储桶名称在整个 Google Cloud 中必须是唯一的,因此您可能无法将项目 ID 用作存储桶名称。

  2. 启动 Cloud Build 流程:

    gcloud builds submit --substitutions=_DEPLOY_DIR=gs://YOUR_BUCKET_NAME,_DEPLOY_FILENAME=app.tar.gz
    

    gcloud builds submit 命令使用 --substitutions 来配置生成的 tar 文件的上传目标位置。之后,该 tar 文件会下载到 Compute Engine 实例。

    Cloud Build 使用 YAML 配置文件来定义构建所需的步骤。

    steps:
      # Print the Go version being used.
      - name: 'mirror.gcr.io/library/golang'
        args: ['go', 'version']
      # Make a deploy directory we'll tar after building the app.
      - name: 'debian'
        args: ['mkdir', '-p', 'deploy/etc/systemd/system/', 'deploy/usr/bin']
      # Build the app.
      - name: 'mirror.gcr.io/library/golang'
        env: [
          'GO111MODULE=on',
          'GOPROXY=https://proxy.golang.org,direct',
          'GOOS=linux',
          'GOARCH=amd64'
        ]
        args: ['go', 'build', '-o', 'deploy/usr/bin/app', '.']
      # Copy the systemd service file into the deploy directory.
      - name: 'debian'
        args: ['cp', 'my-app.service', 'deploy/etc/systemd/system/']
      # Compress the deploy directory.
      - name: 'debian'
        args: ['tar', '-czf', '${_DEPLOY_FILENAME}', '-C', './deploy', '.']
    # Upload the tarball to Cloud Storage.
    artifacts:
      objects:
        location: '${_DEPLOY_DIR}'
        paths: ['${_DEPLOY_FILENAME}']

使用启动脚本初始化实例

您需要一种方法来指示实例下载并运行这些代码。 实例可能拥有对其执行启动或重启操作时会运行的启动脚本。

启动脚本会在实例首次启动时运行。

set -ex

# Install logging monitor. The monitor will automatically pickup logs sent to syslog.
curl "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" --output google-fluentd-install.sh
checksum=$(sha256sum google-fluentd-install.sh | awk '{print $1;}')
if [ "$checksum" != "ec78e9067f45f6653a6749cf922dbc9d79f80027d098c90da02f71532b5cc967" ]; then
    echo "Checksum does not match"
    exit 1
fi
chmod +x google-fluentd-install.sh && ./google-fluentd-install.sh
service google-fluentd restart &

APP_LOCATION=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/app-location" -H "Metadata-Flavor: Google")
gsutil cp "$APP_LOCATION" app.tar.gz
tar -xzf app.tar.gz

# Start the service included in app.tar.gz.
service my-app start

启动脚本执行以下任务:

  • 安装 Cloud Logging 代理,并将其配置为监控应用日志。

  • 下载并提取部署 tar 文件。

  • 启动 systemd 服务以运行该应用。

创建和配置 Compute Engine 实例

  1. 创建 Compute Engine 实例:

    Linux/macOS

    gcloud compute instances create my-app-instance \
    --image-family=debian-10 \
    --image-project=debian-cloud \
    --machine-type=g1-small \
    --scopes userinfo-email,cloud-platform \
    --metadata-from-file startup-script=startup-script.sh \
    --metadata app-location="gs://YOUR_BUCKET_NAME/app.tar.gz" \
    --zone YOUR_ZONE \
    --tags http-server
    

    YOUR_ZONE 替换为开发区域,如 us-central1-a。如需详细了解地区和区域,请参阅地理位置和地区

    --metadata app-location 标志会告知启动脚本在哪里下载应用 tar 文件。

    Windows

    gcloud compute instances create my-app-instance ^
    --image-family=debian-10 ^
    --image-project=debian-cloud ^
    --machine-type=g1-small ^
    --scopes userinfo-email,cloud-platform ^
    --metadata-from-file startup-script=startup-script.sh ^
    --metadata app-location="gs://YOUR_BUCKET_NAME/app.tar.gz" ^
    --zone YOUR_ZONE ^
    --tags http-server
    

    YOUR_ZONE 替换为开发区域,如 us-central1-a。如需详细了解地区和区域,请参阅地理位置和地区

    --metadata app-location 标志会告知启动脚本在哪里下载应用 tar 文件。

    这样会创建一个新实例,允许其访问 Google Cloud 服务,同时会运行您的启动脚本。实例名称为 my-app-instance

  2. 检查实例创建的进度:

    gcloud compute instances get-serial-port-output my-app-instance --zone YOUR_ZONE
    

    启动脚本完成后,您会看到以下消息:

    startup-script: INFO Finished running startup scripts.
    
  3. 创建一条防火墙规则以允许流量进入您的实例:

    gcloud compute firewall-rules create default-allow-http-80 \
        --allow tcp:80 \
        --source-ranges 0.0.0.0/0 \
        --target-tags http-server \
        --description "Allow port 80 access to http-server"
    

  4. 获取您的实例的外部 IP 地址:

    gcloud compute instances list
    
  5. 要查看应用的运行情况,请在浏览器中输入以下网址:

    http://YOUR_INSTANCE_IP
    

    YOUR_INSTANCE_IP 替换为实例的外部 IP 地址。

管理和监控实例

您可以使用 Google Cloud 控制台来监控和管理您的实例。

  1. 在 Google Cloud 控制台中,转到虚拟机实例页面。

    转到“虚拟机实例”

  2. 在虚拟机实例列表中,点击要连接的实例所在行中的 SSH
  3. 如需查看 Compute Engine 资源生成的所有日志,请转到日志浏览器页面。转到 Logs Explorer

    Cloud Logging 会自动配置为从各种常用服务(包括 syslog)收集日志。

清理

为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。

删除项目

  1. 在 Google Cloud 控制台中,进入管理资源页面。

    转到“管理资源”

  2. 在项目列表中,选择要删除的项目,然后点击删除
  3. 在对话框中输入项目 ID,然后点击关闭以删除项目。

逐个删除资源

gcloud compute instances delete my-app-instance --zone=YOUR_ZONE --delete-disks=all
gcloud compute firewall-rules delete default-allow-http-80

后续步骤