Deploy a C# Container Instance in Azure

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

在 Azure 使用 Terraform 部署 IronSecureDoc Docker 容器,並透過公網 IP 位址和完全合格網域名稱 (FQDN) 使其可供存取。

先決條件

步驟 1:複製 GitHub 儲存庫範本

我們已準備好 GitHub 儲存庫範本,您可在此處克隆並立即開始:

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

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

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

  • resource_group_name:資源群組的名稱。
  • resource_group_location:資源群組的位置。您可以從 REGIONS.md 取得區域資訊,並使用第二欄的內容填入此變數。
  • container_group_name:容器群組的名稱。
  • container_name:容器的名稱。
  • image_tag:待部署圖片的標籤; 它可以是 latest 或任何特定版本號。
  • 其他變數無需更改。

請使用 secret.tfvars 來封裝所有敏感資料

請在 secret.tfvars 中填入以下變數。

Azure 容器登錄檔

dns_name_label = var.dns_name_label 之後追加至 main.tf

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

步驟 6:驗證結果

  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

常見問題

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

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

設定 Terraform 進行 Azure 部署需要哪些步驟?

首先,安裝並設定 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'。

用於初始化 Azure 部署的 Terraform 指令為何?

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

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

敏感資料(例如登錄檔憑證和授權金鑰)應儲存於 `secret.tfvars` 檔案中。此檔案會在 Terraform 的 plan 和 apply 過程中被引用,以安全地管理敏感資訊。

如何使用 Terraform 在 Azure 中銷毀已部署的資源?

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

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
版本: 2024.10 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎?
執行範例 觀看您的 PDF 被加密。