這個教學課程說明如何開始使用 Compute Engine。請按照這個教學課程中的指示,將 Hello World .NET 網頁應用程式部署至 Compute Engine。如需開始使用 App Engine 的相關說明,請參閱 App Engine 標準環境。
目標
- 將 Hello World 範例應用程式部署至單一 Compute Engine 執行個體。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.- 在本機上安裝 .NET Core SDK 2.1 以上版本,並執行 Microsoft Windows。
- 在本機上安裝 Web Deploy 3.6 以上版本,本機必須執行 Microsoft Windows。
在本機執行應用程式
下載並解壓縮或從 GitHub 複製範例存放區:
git clone https://github.com/GoogleCloudPlatform/getting-started-dotnet.git在 PowerShell 視窗中,前往
HelloWorld目錄:cd getting-started-dotnet\HelloWorld啟動本機網路伺服器:
dotnet run開啟網路瀏覽器並前往
localhost:8080。在網頁瀏覽器中,您會看到一些 Hello World 文字,這些文字是從本機電腦提供。
準備好繼續時,請按下 Ctrl+C 停止本機網路伺服器。
部署至單一執行個體
這個部分的內容會逐步引導您在 Compute Engine 中執行單一應用程式執行個體。
建立及設定 Compute Engine 執行個體
您可以透過 Google Cloud Marketplace,在 Compute Engine 上啟動執行 Microsoft IIS 的 Windows 執行個體。
前往 Google Cloud 控制台的「ASP.NET Framework」Cloud Marketplace 頁面。
按一下「啟動」。
將設定保留為預設值,然後點按「部署」。
等待 Compute Engine 執行個體部署完成。部署作業通常需要約 5 分鐘。
如要顯示資源資訊,請按一下「VM 執行個體」。
如要編輯 VM 執行個體,請按一下「管理資源」。
按一下「設定 Windows 密碼」,然後記下使用者名稱。
複製新的 Windows 密碼,然後按一下「Close」。
在「VM instance details」(VM 執行個體詳細資料) 頁面中,記下外部 IP 位址。
在本機 Windows 電腦上,編輯
HelloWorld/Properties/PublishProfiles/ComputeEngine.pubxml檔案。在
<MSDeployServiceURL>和</MSDeployServiceURL>之間輸入 VM 執行個體的外部 IP 位址。例如:<MSDeployServiceURL>203.0.113.22</MSDeployServiceURL>輸入先前記下的執行個體使用者名稱,長度必須介於
<UserName>到</UserName>個字元之間。將變更儲存至
ComputeEngine.pubxml檔案。在 PowerShell 視窗中,將應用程式發布至 Compute Engine 執行個體:
dotnet publish -c Release ` /p:PublishProfile=Properties\PublishProfiles\ComputeEngine.pubxml ` "/p:Password=YOUR-PASSWORD"
將
YOUR-PASSWORD換成您先前複製的密碼。在網路瀏覽器中開啟執行個體的外部 IP 位址。
在網路瀏覽器中,您會看到一些 Hello World 文字,這些文字是從 Compute Engine 執行個體提供。
管理及監控執行個體
您可以使用 Google Cloud 主控台來監控及管理執行個體。
-
如要查看 Compute Engine 資源產生的所有記錄,請前往「Logs Explorer」頁面。
前往記錄檔探索工具系統會自動將 Cloud Logging 設為從多項常見服務收集記錄檔,包括
syslog。
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 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
後續步驟
查看 Google Cloud 的參考架構、圖表和最佳做法。歡迎瀏覽我們的 Cloud Architecture Center。
如需建構應用程式的其他 .NET 資源,請參考下列項目:
- 透過「Compute Engine」>「Instance groups」(執行個體群組) 管理及監控執行個體群組的部署作業。
- 透過「Network services」(網路服務) >「Load balancing」(負載平衡) 管理負載平衡設定,當中包含網址對應關係與後端服務。
- 將應用程式部署至 Google Kubernetes Engine
- 探索其他Google Cloud 服務。