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

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/

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

修改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

常見問題解答

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

若要使用 Terraform 在 Azure 中部署 Docker 容器,您需要複製 IronSecureDoc 的 GitHub 儲存庫模板,請修改 `variables.tf` 中的資源名稱,並建立一個包含敏感資料的 `secret.tfvars` 檔案。然後,使用terraform init初始化部署,並使用terraform planterraform apply建立並套用執行計劃。

在 Azure 上部署 Terraform 需要哪些步驟?

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

Azure 容器登錄檔在此部署中扮演什麼角色?

Azure 容器註冊表用於儲存 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 計畫和應用程式會引用此文件,以安全地管理敏感資訊。

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

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

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

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

準備好開始了嗎?
版本: 2024.10 剛剛發布