開始在 Compute Engine 上使用 .NET

這個教學課程說明如何開始使用 Compute Engine。請按照這個教學課程中的指示,將 Hello World .NET 網頁應用程式部署至 Compute Engine。如需開始使用 App Engine 的相關說明,請參閱 App Engine 標準環境

目標

  • 將 Hello World 範例應用程式部署至單一 Compute Engine 執行個體。

費用

在本文件中,您會使用下列 Google Cloud的計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 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.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Compute Engine API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

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

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Compute Engine API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. 在本機上安裝 .NET Core SDK 2.1 以上版本,並執行 Microsoft Windows
  9. 在本機上安裝 Web Deploy 3.6 以上版本,本機必須執行 Microsoft Windows

在本機執行應用程式

  1. 下載並解壓縮或從 GitHub 複製範例存放區:

    git clone https://github.com/GoogleCloudPlatform/getting-started-dotnet.git
    
  2. 在 PowerShell 視窗中,前往 HelloWorld 目錄:

    cd getting-started-dotnet\HelloWorld
    
  3. 啟動本機網路伺服器:

    dotnet run
    
  4. 開啟網路瀏覽器並前往 localhost:8080

    在網頁瀏覽器中,您會看到一些 Hello World 文字,這些文字是從本機電腦提供。

  5. 準備好繼續時,請按下 Ctrl+C 停止本機網路伺服器。

部署至單一執行個體

這個部分的內容會逐步引導您在 Compute Engine 中執行單一應用程式執行個體。

單一執行個體的部署作業。

建立及設定 Compute Engine 執行個體

您可以透過 Google Cloud Marketplace,在 Compute Engine 上啟動執行 Microsoft IIS 的 Windows 執行個體。

  1. 前往 Google Cloud 控制台的「ASP.NET Framework」Cloud Marketplace 頁面。

    前往 Cloud Marketplace

  2. 按一下「啟動」

  3. 將設定保留為預設值,然後點按「部署」

    等待 Compute Engine 執行個體部署完成。部署作業通常需要約 5 分鐘。

  4. 如要顯示資源資訊,請按一下「VM 執行個體」

  5. 如要編輯 VM 執行個體,請按一下「管理資源」

  6. 按一下「設定 Windows 密碼」,然後記下使用者名稱。

  7. 複製新的 Windows 密碼,然後按一下「Close」

  8. 在「VM instance details」(VM 執行個體詳細資料) 頁面中,記下外部 IP 位址。

  9. 在本機 Windows 電腦上,編輯 HelloWorld/Properties/PublishProfiles/ComputeEngine.pubxml 檔案。

  10. <MSDeployServiceURL></MSDeployServiceURL> 之間輸入 VM 執行個體的外部 IP 位址。例如:<MSDeployServiceURL>203.0.113.22</MSDeployServiceURL>

  11. 輸入先前記下的執行個體使用者名稱,長度必須介於 <UserName></UserName> 個字元之間。

  12. 將變更儲存至 ComputeEngine.pubxml 檔案。

  13. 在 PowerShell 視窗中,將應用程式發布至 Compute Engine 執行個體:

    dotnet publish -c Release `
      /p:PublishProfile=Properties\PublishProfiles\ComputeEngine.pubxml `
      "/p:Password=YOUR-PASSWORD"
    

    YOUR-PASSWORD 換成您先前複製的密碼。

  14. 在網路瀏覽器中開啟執行個體的外部 IP 位址。

    在網路瀏覽器中,您會看到一些 Hello World 文字,這些文字是從 Compute Engine 執行個體提供。

管理及監控執行個體

您可以使用 Google Cloud 主控台來監控及管理執行個體。

  1. 如要查看 Compute Engine 資源產生的所有記錄,請前往「Logs Explorer」頁面。

    前往記錄檔探索工具

    系統會自動將 Cloud Logging 設為從多項常見服務收集記錄檔,包括 syslog

清除所用資源

為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。

刪除專案

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。

刪除個別資源

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

後續步驟