开始在 Compute Engine 上使用 Ruby

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

目标

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

费用

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

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

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  4. 启用 Compute Engine API。

    启用 API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  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 中,使用 bundle 安装应用依赖项:

    bundle install
    
  2. 运行 Sinatra 应用:

    bundle exec ruby app.rb -p 8080
    
  3. 在 Cloud Shell 中,点击 Web 预览,然后选择在端口 8080 上预览。此时系统会打开一个新窗口,其中显示正在运行的应用。

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

部署到单个实例

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

单实例部署。

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

使用启动脚本初始化实例

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

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

# Install dependencies
apt-get update && apt-get -y upgrade && apt-get install -y autoconf bison \
    build-essential git libssl-dev libyaml-dev libreadline6-dev zlib1g-dev \
    libncurses5-dev libffi-dev libgdbm3 libgdbm-dev nginx supervisor

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

# Install Ruby and Bundler
mkdir /home/rubyapp/.ruby
git clone https://github.com/rbenv/ruby-build.git /home/rubyapp/.ruby-build
/home/rubyapp/.ruby-build/bin/ruby-build 2.6.5 /home/rubyapp/.ruby

chown -R rubyapp:rubyapp /home/rubyapp

cat >/home/rubyapp/.profile << EOF
export PATH="/home/rubyapp/.ruby/bin:$PATH"
EOF

su -l rubyapp -c "gem install bundler"

# Fetch source code
git clone https://github.com/GoogleCloudPlatform/getting-started-ruby.git /opt/app

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

# Install ruby dependencies
su -l rubyapp -c "cd /opt/app/gce && bundle install"

# Disable the default NGINX configuration
rm /etc/nginx/sites-enabled/default

# Enable our NGINX configuration
cp /opt/app/gce/rubyapp.conf /etc/nginx/sites-available/rubyapp.conf
ln -s /etc/nginx/sites-available/rubyapp.conf /etc/nginx/sites-enabled/rubyapp.conf

# Start NGINX
systemctl restart nginx.service

# Configure supervisor to run the ruby app
cat >/etc/supervisor/conf.d/rubyapp.conf << EOF
[program:rubyapp]
directory=/opt/app/gce
command=bash -lc "bundle exec ruby app.rb"
autostart=true
autorestart=true
user=rubyapp
environment=HOME="/home/rubyapp",USER="rubyapp"
stdout_logfile=syslog
stderr_logfile=syslog
EOF

supervisorctl reread
supervisorctl update

# Application should now be running under supervisor

启动脚本执行以下任务:

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

  • 安装 Ruby 2.6.5、Bundler 及其系统依赖项。

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

  • 安装并配置 Nginx

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

  • 配置 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-9 \
        --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-a ^
        --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-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 地址。

管理和监控实例

您可以使用 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-80

后续步骤