Compute Engine 上的 Python 使用入门

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

目标

  • 使用 Cloud Shell 下载并部署 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 API。

    启用 API

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

    转到“项目选择器”

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

  7. 启用 Compute Engine API。

    启用 API

  8. 在 Google Cloud Console 中,通过 Cloud Shell 打开此应用。

    转到 Cloud Shell

    利用 Cloud Shell,您可以直接在浏览器中通过命令行访问云端资源。在浏览器中打开 Cloud Shell,然后点击继续下载示例代码并切换到应用目录。

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

在 Cloud Shell 中运行应用

  1. 在 Cloud Shell 中,使用 pip 安装应用依赖项:

    pip3 install -r requirements.txt --user
    
  2. 安装 Gunicorn HTTP 服务器

    pip3 install gunicorn --user
    
  3. 运行 Gunicorn HTTP 服务器:

    ~/.local/bin/gunicorn -b :8080 main:app
    
  4. 在 Cloud Shell 中,点击 Web 预览,然后选择在端口 8080 上预览。此时系统会打开一个新窗口,其中显示正在运行的应用。

  5. 要停止本地网络服务器,请按 Control+C

部署到单个实例

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

单实例部署。

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

使用启动脚本初始化实例

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

# Install Stackdriver logging agent
curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh
sudo bash install-logging-agent.sh

# Install or update needed software
apt-get update
apt-get install -yq git supervisor python python-pip python3-distutils
pip install --upgrade pip virtualenv

# Account to own server process
useradd -m -d /home/pythonapp pythonapp

# Fetch source code
export HOME=/root
git clone https://github.com/GoogleCloudPlatform/getting-started-python.git /opt/app

# Python environment setup
virtualenv -p python3 /opt/app/gce/env
/bin/bash -c "source /opt/app/gce/env/bin/activate"
/opt/app/gce/env/bin/pip install -r /opt/app/gce/requirements.txt

# Set ownership to newly created account
chown -R pythonapp:pythonapp /opt/app

# Put supervisor configuration in proper place
cp /opt/app/gce/python-app.conf /etc/supervisor/conf.d/python-app.conf

# Start service via supervisorctl
supervisorctl reread
supervisorctl update

启动脚本执行以下任务:

  • 安装 Python 3 和 PIP。

  • 安装 Cloud Logging 代理。该代理会自动从系统日志收集日志。

  • 安装 Supervisor 将应用作为守护进程运行。

  • 从 Cloud Source Repositories 代码库克隆应用的源代码并安装依赖项。

  • 配置 Supervisor 以运行该应用。如果应用意外退出或被管理员或其他进程终止,Supervisor 将确保应用重启。此外,它还会将应用的 stdoutstderr 发送到 syslog,以便由 Logging 代理收集。

创建和配置 Compute Engine 实例

  1. 创建 Compute Engine 实例:

    Linux/macOS

    MY_INSTANCE_NAME="my-app-instance"
    ZONE=us-central1-a
    
    gcloud compute instances create $MY_INSTANCE_NAME \
        --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 \
        --zone $ZONE \
        --tags http-server

    Windows

    gcloud compute instances create my-app-instance ^
        --image-family=debian-9 ^
        --image-project=debian-cloud ^
        --machine-type=g1-small ^
        --scopes userinfo-email,cloud-platform ^
        --metadata-from-file startup-script=gce/startup-script.sh ^
        --zone us-central1-f ^
        --tags http-server
    

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

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

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

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

    YOUR_ZONE 替换为您部署实例的目标地区。

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

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

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

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

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

    http://YOUR_INSTANCE_IP:8080
    

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

管理和监控实例

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

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

    转到“虚拟机实例”

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

    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-8080

后续步骤