使用 Azure 容器執行個體,以簡單且快速的方式在 Azure 中執行無伺服器 Docker 容器。 當您不需要 Azure Kubernetes Service 等完整容器協調流程平台時,請隨選將應用程式部署至容器執行個體。 在本文中,您會使用 Terraform 來部署隔離的 Docker 容器,並使其 Web 應用程式可以使用公用 IP 位址。
Terraform 允許對雲端基礎結構進行定義、預覽和部署。 使用 Terraform,您可以使用 HCL 語法建立組態檔。 HCL 語法可讓您指定雲端提供者,例如 Azure,以及構成雲端基礎結構的專案。 建立組態檔之後,您會建立一個 執行計劃 ,讓您在部署基礎結構變更之前先預覽這些變更。 驗證變更之後,您會套用執行計劃來部署基礎結構。
在本文中,您將學會如何:
- 使用 random_pet 建立 Azure 資源群組名稱的隨機值
- 使用 azurerm_resource_group 建立 Azure 資源群組
- 使用 random_string 為容器名稱建立隨機值
- 使用 azurerm_container_group 建立 Azure 容器群組
先決條件
實作 Terraform 程式碼
備註
本文的範例程式代碼位於 Azure Terraform GitHub 存放庫中。 您可以檢視包含 目前和舊版 Terraform 測試結果的記錄檔。
建立目錄,在其中測試並執行範例 Terraform 程式代碼,並將其設為目前目錄。
建立名為
main.tf的檔案,並插入下列程序代碼:resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } resource "azurerm_resource_group" "rg" { name = random_pet.rg_name.id location = var.resource_group_location } resource "random_string" "container_name" { length = 25 lower = true upper = false special = false } resource "azurerm_container_group" "container" { name = "${var.container_group_name_prefix}-${random_string.container_name.result}" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name ip_address_type = "Public" os_type = "Linux" restart_policy = var.restart_policy zones = var.zone != "" ? [ var.zone ] : null container { name = "${var.container_name_prefix}-${random_string.container_name.result}" image = var.image cpu = var.cpu_cores memory = var.memory_in_gb ports { port = var.port protocol = "TCP" } } }建立名為
outputs.tf的檔案,並插入下列程序代碼:output "container_ipv4_address" { value = azurerm_container_group.container.ip_address }建立名為
providers.tf的檔案,並插入下列程序代碼:terraform { required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }建立名為
variables.tf的檔案,並插入下列程序代碼:variable "resource_group_location" { type = string default = "eastus" description = "Location for all resources." } variable "resource_group_name_prefix" { type = string default = "rg" description = "Prefix of the resource group name that's combined with a random value so name is unique in your Azure subscription." } variable "container_group_name_prefix" { type = string description = "Prefix of the container group name that's combined with a random value so name is unique in your Azure subscription." default = "acigroup" } variable "container_name_prefix" { type = string description = "Prefix of the container name that's combined with a random value so name is unique in your Azure subscription." default = "aci" } variable "image" { type = string description = "Container image to deploy. Should be of the form repoName/imagename:tag for images stored in public Docker Hub, or a fully qualified URI for other registries. Images from private registries require additional registry credentials." default = "mcr.microsoft.com/azuredocs/aci-helloworld" } variable "port" { type = number description = "Port to open on the container and the public IP address." default = 80 } variable "cpu_cores" { type = number description = "The number of CPU cores to allocate to the container." default = 1 } variable "memory_in_gb" { type = number description = "The amount of memory to allocate to the container in gigabytes." default = 2 } variable "restart_policy" { type = string description = "The behavior of Azure runtime if container has stopped." default = "Always" validation { condition = contains(["Always", "Never", "OnFailure"], var.restart_policy) error_message = "The restart_policy must be one of the following: Always, Never, OnFailure." } } variable "zone" { type = string description = "The availability zone to deploy the container group into. If not specified, the container group is nonzonal and might be deployed into any zone." default = "" }若要使 容器群組成為區域,請將變數的
zone值設定為您要部署的邏輯可用性區域。這很重要
區域容器群組僅適用於支援可用性區域的區域。 若要查看您的區域是否支援可用性區域,請參閱 Azure 區域清單。
初始化 Terraform
執行 terraform init 來初始化 Terraform 部署。 此命令會下載管理 Azure 資源所需的 Azure 提供者。
terraform init -upgrade
關鍵點:
-
-upgrade參數會將必要的提供者外掛程式升級至符合組態版本條件約束的最新版本。
建立 Terraform 執行計劃
執行 terraform 計劃 以建立執行計劃。
terraform plan -out main.tfplan
關鍵點:
-
terraform plan命令會建立執行計劃,但不會執行它。 然而,它會決定哪些動作是必要的,以建立您組態檔中所指定的設定。 此模式可讓您在對實際資源進行任何變更之前,先確認執行計劃是否符合您的預期。 - 選擇性
-out參數可讓您指定計劃的輸出檔。 使用-out參數可確保您查看的計劃確切地被套用。
套用 Terraform 執行計畫
執行terraform apply指令將執行計劃套用至您的雲端基礎設施。
terraform apply main.tfplan
關鍵點:
- 範例
terraform apply命令假設您先前已執行terraform plan -out main.tfplan。 - 如果您為
-out參數指定了不同的檔案名,請在呼叫terraform apply時使用相同的檔案名。 - 如果您未使用
-out參數,請呼叫沒有任何參數的terraform apply。
確認結果
當您套用執行計劃時,Terraform 會輸出公用 IP 位址。 若要再次顯示 IP 位址,請執行 terraform 輸出。
terraform output -raw container_ipv4_address在瀏覽器的網址列中輸入範例的公用 IP 位址。
清理資源
當您不再需要透過 Terraform 建立的資源時,請執行下列步驟:
執行 terraform 計劃 並指定
destroy旗標。terraform plan -destroy -out main.destroy.tfplan關鍵點:
-
terraform plan命令會建立執行計劃,但不會執行它。 然而,它會決定哪些動作是必要的,以建立您組態檔中所指定的設定。 此模式可讓您在對實際資源進行任何變更之前,先確認執行計劃是否符合您的預期。 - 選擇性
-out參數可讓您指定計劃的輸出檔。 使用-out參數可確保您查看的計劃確切地被套用。
-
執行 terraform apply 來應用執行計劃。
terraform apply main.destroy.tfplan
排除 Azure 上 Terraform 的故障
針對在 Azure 上使用 Terraform 時的常見問題進行疑難解答