在 Azure 中部署 C# 容器執行個體

This article was translated from English: Does it need improvement?
Translated
View the article in English

使用Azure 上的 Terraform部署 IronSecureDoc Docker 容器,並使其具有公用 IP 位址和 FQDN。

先決條件

安裝和配置 Terraform

步驟 1:複製 GitHub 倉庫模板

我們已為您準備好 GitHub 倉庫模板,您可以複製並立即開始使用,連結如下

https://github.com/iron-software/IronSecureDoc-Terraform/

步驟二:修改所有資源名稱

修改variables.tf中的所有資源名稱

  • resource_group_name : 資源組的名稱。
  • resource_group_location :資源組的位置。您可以從 REGIONS.md 檔案中找到區域,並使用第二列來填入此變數。
  • container_group_name :容器群組的名稱。
  • container_name : 容器的名稱。
  • image_tag : 要部署的鏡像的標籤; 可以是latest ,也可以是任何特定版本號。
  • 其他變數無需更改。

建立secret.tfvars以包含所有敏感數據

secret.tfvars中填入以下變數。

Azure容器註冊表

main.tfdns_name_label = var.dns_name_label之後新增:

image_registry_credential {
  server   = var.registry_server
  username = var.registry_username
  password = var.registry_password
}

加到variables.tf

variable "registry_server" {
  type        = string
  sensitive   = false
  description = "The server for the container registry. Required if the image is stored in a private registry."
}

variable "registry_username" {
  type        = string
  sensitive   = true
  description = "Username for the container registry. Required if the image is stored in a private registry."
}

variable "registry_password" {
  type        = string
  sensitive   = true
  description = "Password for the container registry. Required if the image is stored in a private registry."
}

加到secret.tfvars

registry_server   = "<registry-name>.azurecr.io"
registry_username = "YOUR-REGISTRY-USERNAME"
registry_password = "YOUR-REGISTRY-PASSWORD"
license_key       = "YOUR-LICENSE-KEY"

Docker Hub

加到secret.tfvars

license_key = "YOUR-LICENSE-KEY"

步驟 3:初始化 Terraform

執行terraform init來初始化 Terraform 部署。 此指令會下載管理 Azure 資源所需的 Azure 提供者。

terraform init -upgrade
terraform init -upgrade
SHELL

步驟 4:建立 Terraform 執行計劃

運行terraform plan以建立執行計劃。

terraform plan -var-file="secret.tfvars" -out main.tfplan
terraform plan -var-file="secret.tfvars" -out main.tfplan
SHELL

步驟 5:應用 Terraform 執行計劃

執行terraform apply將執行計畫套用到您的雲端基礎架構。

terraform apply main.tfplan
terraform apply main.tfplan
SHELL

步驟六:驗證結果

  1. 應用執行計劃時,Terraform 會輸出公網 IP 位址。 若要再次顯示 IP 位址,請執行terraform output

    terraform output -raw container_ipv4_address
    terraform output -raw container_ipv4_address
    SHELL

    或顯示完全限定網域名稱 (FQDN)。

    terraform output -raw container_fqdn
    terraform output -raw container_fqdn
    SHELL
  2. 使用 Postman 或 curl 進行驗證。 預期結果應回傳pong

    curl http://<container_ipv4_address>:8080/v1/document-services/ping
    curl http://<container_ipv4_address>:8080/v1/document-services/ping
    SHELL

    或使用完全限定域名 (FQDN)。

    curl http://<container_fqdn>:8080/v1/document-services/ping
    curl http://<container_fqdn>:8080/v1/document-services/ping
    SHELL

步驟 7:銷毀資源

  • 制定銷毀資源的執行計畫。

    terraform plan -destroy -var-file="secret.tfvars" -out main.tfplan
    terraform plan -destroy -var-file="secret.tfvars" -out main.tfplan
    SHELL
  • 應用銷毀計劃。

    terraform apply main.tfplan
    terraform apply main.tfplan
    SHELL

常見問題解答

如何在 Azure 中使用 Terraform 部署 Docker 容器?

若要在 Azure 中使用 Terraform 部署 Docker 容器,您需要克隆 IronSecureDoc 的 GitHub 儲存庫範本,修改 `variables.tf` 中的資源名稱,並建立包含敏感資料的 `secret.tfvars` 檔案。然後,使用 terraform init 來初始化部署,並使用 terraform planterraform apply 來建立和套用執行計畫。

為 Azure 部署設定 Terraform 的必要步驟是什麼?

首先,安裝並設定 Terraform,然後將其驗證至 Azure。克隆 IronSecureDoc 的特定 GitHub 儲存庫,修改 `variables.tf` 檔案中的資源名稱,並管理 `secret.tfvars` 中的敏感資料。最後,初始化 Terraform 並建立部署的執行計畫。

Azure Container Registry 在此部署中扮演什麼角色?

Azure Container Registry 用於儲存 Docker 映像。您需要在 `main.tf` 檔案中附加註冊憑證,並在 `variables.tf` 和 `secret.tfvars` 中指定註冊資訊,以便部署存取和部署必要的 Docker 影像。

如何驗證我在 Azure 中的 Terraform 部署是否成功?

套用 Terraform 執行計畫後,執行 terraform output 以取得公共 IP 位址或 FQDN。使用 Postman 或 curl 等工具向服務發送 ping 請求。成功的部署將會傳回 'pong'。

使用什麼指令來初始化 Terraform for Azure 部署?

要初始化適用於 Azure 部署的 Terraform,請使用 terraform init -upgrade 指令。這會下載管理 Azure 資源所需的必要提供者外掛程式。

如何處理 Terraform 配置中的敏感資料?

註冊表憑證和授權金鑰等敏感資料應儲存在`secret.tfvars`檔中。在 Terraform 計劃和套用過程中會參考此檔案,以安全管理敏感資訊。

使用 Terraform 銷毀 Azure 中已部署資源的流程是什麼?

若要使用 Terraform 銷毀 Azure 中已部署的資源,請使用 terraform plan -destroy -var-file='secret.tfvars' -out main.tfplan 建立銷毀執行計畫,並使用 terraform apply main.tfplan 套用。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Version: 2024.10 剛發表