全部產品
Search
文件中心

Alibaba Cloud Service Mesh:使用Terraform管理ASM執行個體

更新時間:Jul 06, 2024

Terraform是HashiCorp公司提供的一種開源的雲上Resource Orchestration Service工具,用於安全高效地預覽,配置和管理雲基礎架構和資源,協助開發人員自動化地建立、更新阿里雲基礎設施資源。本文介紹如何使用Terraform建立和刪除ASM執行個體。

前提條件

  • 已在本地安裝和配置Terraform。具體操作,請參見在本地安裝和配置Terraform

  • 已配置阿里雲帳號資訊。建立環境變數,用於存放身份認證和地區資訊。

    export ALICLOUD_ACCESS_KEY="************" #替換為您的AccessKey ID資訊。
    export ALICLOUD_SECRET_KEY="************" #替換為您的AccessKey Secret資訊。
    export ALICLOUD_REGION="cn-beijing"    #替換為您的地區ID。
    說明

    為提高許可權管理的靈活性和安全性,建議您建立名為Terraform的RAM使用者,並為該RAM使用者建立AccessKey和授權。具體操作,請參見建立RAM使用者為RAM使用者授權

背景資訊

關於Terraform的詳細介紹,請參見terraform

建立ASM執行個體

  1. 在本地建立名為main.tf的設定檔。

    • 若您沒有專用網路和虛擬交換器,您需要使用以下內容建立main.tf檔案。

      terraform {
        required_providers {
          alicloud = {
            source = "aliyun/alicloud"
          }
        }
      }
      
      variable "k8s_name_prefix" {
        description = "The name prefix used to create Service Mesh (ASM)."
        default     = "tf-asm"
      }
      
      resource "random_uuid" "this" {}
      
      # 預設的資源名稱及配置。
      locals {
        # 服務網格名稱。
        mesh_name = substr(join("-", [var.k8s_name_prefix, random_uuid.this.result]), 0, 63)
        # 服務網格的規格,可以選擇三種規格:standard: 標準版(免費),enterprise:企業版,ultimate:旗艦版。
        mesh_spec = "enterprise"
        # 專用網路名稱。
        new_vpc_name = "vpc-for-${local.mesh_name}"
        # 虛擬交換器名稱。
        new_vsw_name = "vsw-for-${local.mesh_name}"
      }
      
      # 可以建立虛擬交換器的AZ。
      data "alicloud_zones" "default" {
        available_resource_creation = "VSwitch"
      }
      # 專用網路。
      resource "alicloud_vpc" "default" {
        vpc_name = local.new_vpc_name
      }
      # 虛擬交換器。
      resource "alicloud_vswitch" "default" {
        vpc_id       = alicloud_vpc.default.id
        cidr_block   = cidrsubnet(alicloud_vpc.default.cidr_block, 8, 2)
        zone_id      = data.alicloud_zones.default.zones.0.id
        vswitch_name = local.new_vsw_name
      }
      # 查詢可以建立的服務網格版本。
      data "alicloud_service_mesh_versions" "default" {
        edition = local.mesh_spec == "standard" ? "Default" : "Pro"
      }
      # 選擇可建立的第一個版本作為建立新服務網格使用的版本。
      locals {
        mesh_version = split(":", data.alicloud_service_mesh_versions.default.ids[0])[1]
      }
      # 服務網格ASM執行個體。
      resource "alicloud_service_mesh_service_mesh" "default" {
        # 服務網格名稱。
        service_mesh_name = local.mesh_name
        # 服務網格的網路設定。
        network {
          # 專用網路ID。
          vpc_id        = alicloud_vpc.default.id
          # 虛擬交換器ID。
          vswitche_list = [alicloud_vswitch.default.id]
        }
        # 服務網格的版本。
        version = local.mesh_version
        # 服務網格的API Server和Pilot負載平衡配置。
        load_balancer {
          # 是否使用eip暴露服務網格API Server負載平衡。
          api_server_public_eip = true
        }
      
        # 服務網格的執行個體配置Mesh Config。
        mesh_config {
          # 將訪問日誌採集到阿里雲Log Service。
          access_log {
            enabled = true
          }
      
          # 啟用控制面日誌採集,需開通阿里雲Log Service。
          control_plane_log {
            enabled = true
          }
      
          # 啟用ARMS鏈路追蹤。
          tracing = true
      
          # 如果啟用鏈路追蹤,設定採樣百分比。
          pilot {
            trace_sampling = 100
          }
      
          # 開啟採集Prometheus監控指標。
          telemetry = true
      
          # 啟用網格拓撲(需要開啟Prometheus)。
          kiali {
            enabled = true
          }
      
          # 啟用網格審計,需要開通阿里雲Log Service。
          audit {
            enabled = true
          }
        }
        # 服務網格的規格,可以選擇三種規格:standard: 標準版(免費),enterprise:企業版,ultimate:旗艦版。
        cluster_spec = local.mesh_spec
      }

      在main.tf檔案中根據實際情況自訂以下參數,其他參數值會使用OpenAPI自動拉取擷取。

      參數

      描述

      mesh_name

      自訂Service Mesh名稱。

      mesh_spec

      設定Service Mesh規格,可選:

      • standard:標準版(免費)。

      • enterprise:企業版。

      • ultimate:旗艦版。

      new_vpc_name

      自訂專用網路名稱。

      new_vsw_name

      自訂虛擬交換器名稱。

      api_server_public_eip

      是否使用EIP暴露Service MeshAPI Server負載平衡,可選:

      • true:使用EIP暴露Service MeshAPI Server負載平衡。

      • false:不使用EIP暴露Service MeshAPI Server負載平衡。

    • 若您已有專用網路和虛擬交換器,您需要使用以下內容建立main.tf檔案。

      重要

      專用網路、虛擬交換器需要和Terraform的身份認證資訊處於同一地區,否則將識別不到專用網路和虛擬交換器。

      terraform {
        required_providers {
          alicloud = {
            source = "aliyun/alicloud"
          }
        }
      }
      
      variable "asm_name_prefix" {
        description = "The name prefix used to create Service Mesh (ASM)."
        default     = "tf-asm"
      }
      
      resource "random_uuid" "this" {}
      
      # 預設的資源名稱及配置。
      locals {
        # 服務網格名稱。
        mesh_name = substr(join("-", [var.asm_name_prefix, random_uuid.this.result]), 0, 63)
        # 服務網格的規格,可以選擇三種規格:standard: 標準版(免費),enterprise:企業版,ultimate:旗艦版。
        mesh_spec = "enterprise"
        # 已有專用網路名稱。
        vpc_name = "vpc-luying-hangzhou1"
        # 已有虛擬交換器名稱。
        vsw_name = "vsw-luying-hangzhou1"
      }
      
      # 專用網路。
      data "alicloud_vpcs" "default" {
        name_regex = local.vpc_name # 已經存在的vpc名稱。
      }
      # 虛擬交換器。
      data "alicloud_vswitches" "default" {
        vpc_id = data.alicloud_vpcs.default.ids[0]
      }
      locals {
        exist_vswitch_ids = [for vsw in data.alicloud_vswitches.default.vswitches : vsw.id if vsw.name == local.vsw_name]
      }
      # 查詢可以建立的服務網格版本。
      data "alicloud_service_mesh_versions" "default" {
        edition = local.mesh_spec == "standard" ? "Default" : "Pro"
      }
      # 選擇可建立的第一個版本作為建立新服務網格使用的版本。
      locals {
        mesh_version = split(":", data.alicloud_service_mesh_versions.default.ids[0])[1]
      }
      # 服務網格ASM執行個體。
      resource "alicloud_service_mesh_service_mesh" "default" {
        # 服務網格名稱。
        service_mesh_name = local.mesh_name
        # 服務網格的網路設定。
        network {
          # 專用網路ID。
          vpc_id        =  data.alicloud_vpcs.default.ids[0]
          # 虛擬交換器ID。
          vswitche_list = [local.exist_vswitch_ids[0]]
        }
        # 服務網格的版本。
        version = local.mesh_version
        # 服務網格的API Server和Pilot負載平衡配置。
        load_balancer {
          # 是否使用eip暴露服務網格API Server負載平衡。
          api_server_public_eip = true
        }
      
        # 服務網格的執行個體配置Mesh Config。
        mesh_config {
          # 將訪問日誌採集到阿里雲Log Service。
          access_log {
            enabled = true
          }
      
          # 啟用控制面日誌採集,需開通阿里雲Log Service。
          control_plane_log {
            enabled = true
          }
      
          # 啟用ARMS鏈路追蹤。
          tracing = true
      
          # 如果啟用鏈路追蹤,設定採樣百分比。
          pilot {
            trace_sampling = 100
          }
      
          # 開啟採集Prometheus監控指標。
          telemetry = true
      
          # 啟用網格拓撲(需要開啟Prometheus)。
          kiali {
            enabled = true
          }
      
          # 啟用網格審計,需要開通阿里雲Log Service。
          audit {
            enabled = true
          }
        }
        # 服務網格的規格,可以選擇三種規格:standard: 標準版(免費),enterprise:企業版,ultimate:旗艦版。
        cluster_spec = local.mesh_spec
      }

      在main.tf檔案中根據實際情況自訂以下參數,其他參數值會使用OpenAPI自動拉取擷取。

      參數

      描述

      mesh_name

      自訂Service Mesh名稱。

      mesh_spec

      設定Service Mesh規格,可選:

      • standard:標準版(免費)。

      • enterprise:企業版。

      • ultimate:旗艦版。

      vpc_name

      輸入已有專用網路名稱。

      vsw_name

      輸入已有虛擬交換器名稱。

      api_server_public_eip

      是否使用EIP暴露Service MeshAPI Server負載平衡,可選:

      • true:使用EIP暴露Service MeshAPI Server負載平衡。

      • false:不使用EIP暴露Service MeshAPI Server負載平衡。

  2. 執行以下命令,初始化Terraform運行環境。

    terraform init

    預期輸出:

    Initializing the backend...
    
    Initializing provider plugins...
    - Finding aliyun/alicloud versions matching "1.166.0"...
    - Finding latest version of hashicorp/random...
    ...
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. 執行以下命令,產生Terraform資源規劃。

    terraform plan

    預期輸出:

    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    ...
    Plan: 2 to add, 0 to change, 0 to destroy.
  4. 執行以下命令,使用main.tf檔案建立ASM執行個體。

    terraform apply

    預期輸出:

    alicloud_service_mesh_service_mesh.example: Refreshing state...
    ...
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value:                    

    Enter a value參數右側輸入yes,預期返回以下內容:

    ...
    alicloud_service_mesh_service_mesh.default: Creating...
    alicloud_service_mesh_service_mesh.default: Still creating... [10s elapsed]
    ...
    alicloud_service_mesh_service_mesh.example: Creation complete after 2m42s [id=**********]
    
    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

刪除ASM執行個體

使用Terraform刪除指定ASM執行個體時,您必須進入到與main.tf檔案相同的目錄下後,才能執行命令刪除該執行個體。

進入與main.tf檔案相同的目錄下,執行以下命令,刪除ASM執行個體。

terraform destroy

預期輸出:

...
Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: 

Enter a value參數右側輸入yes,預期返回以下內容:

...
Destroy complete! Resources: 2 destroyed.

修改ASM執行個體屬性

您可以通過修改tf檔案中的屬性定義,並執行terraform apply將變更應用到ASM執行個體,本文以修改http10_enabled屬性為例,示範如何通過terraform修改ASM執行個體屬性。

  1. 此處以“已有專用網路和虛擬交換器”情境的tf檔案為例,修改服務網格資源的mesh_config.pilot.http10_enabledtrue

    terraform {
      required_providers {
        alicloud = {
          source = "aliyun/alicloud"
        }
      }
    }
    
    variable "asm_name_prefix" {
      description = "The name prefix used to create Service Mesh (ASM)."
      default     = "tf-asm"
    }
    
    resource "random_uuid" "this" {}
    
    # 預設的資源名稱及配置。
    locals {
      # 服務網格名稱。
      mesh_name = substr(join("-", [var.asm_name_prefix, random_uuid.this.result]), 0, 63)
      # 服務網格的規格,可以選擇三種規格:standard: 標準版(免費),enterprise:企業版,ultimate:旗艦版。
      mesh_spec = "enterprise"
      # 已有專用網路名稱。
      vpc_name = "prod-hz-vpc"
      # 已有虛擬交換器名稱。
      vsw_name = "prod-hz-vpc-default"
    }
    
    # 專用網路。
    data "alicloud_vpcs" "default" {
      name_regex = local.vpc_name # 已經存在的vpc名稱。
    }
    # 虛擬交換器。
    data "alicloud_vswitches" "default" {
      vpc_id = data.alicloud_vpcs.default.ids[0]
    }
    locals {
      exist_vswitch_ids = [for vsw in data.alicloud_vswitches.default.vswitches : vsw.id if vsw.name == local.vsw_name]
    }
    # 查詢可以建立的服務網格版本。
    data "alicloud_service_mesh_versions" "default" {
      edition = local.mesh_spec == "standard" ? "Default" : "Pro"
    }
    # 選擇可建立的第一個版本作為建立新服務網格使用的版本。
    locals {
      mesh_version = split(":", data.alicloud_service_mesh_versions.default.ids[0])[1]
    }
    # 服務網格ASM執行個體。
    resource "alicloud_service_mesh_service_mesh" "default" {
      # 服務網格名稱。
      service_mesh_name = local.mesh_name
      # 服務網格的網路設定。
      network {
        # 專用網路ID。
        vpc_id        =  data.alicloud_vpcs.default.ids[0]
        # 虛擬交換器ID。
        vswitche_list = [local.exist_vswitch_ids[0]]
      }
      # 服務網格的版本。
      version = local.mesh_version
      # 服務網格的API Server和Pilot負載平衡配置。
      load_balancer {
        # 是否使用eip暴露服務網格API Server負載平衡。
        api_server_public_eip = true
      }
    
      # 服務網格的執行個體配置Mesh Config。
      mesh_config {
        # 將訪問日誌採集到阿里雲Log Service。
        access_log {
          enabled = true
        }
    
        # 啟用控制面日誌採集,需開通阿里雲Log Service。
        control_plane_log {
          enabled = true
          project = "mesh-log-cab09b566d4a64c1fa05271d5365495f1"
        }
    
        # 啟用ARMS鏈路追蹤。
        tracing = true
    
        # 如果啟用鏈路追蹤,設定採樣百分比。
        pilot {
          trace_sampling = 100
          http10_enabled = true 
        }
    
        # 開啟採集Prometheus監控指標。
        telemetry = true
    
        # 啟用網格拓撲(需要開啟Prometheus)。
        kiali {
          enabled = true
        }
    
        # 啟用網格審計,需要開通阿里雲Log Service。
        audit {
          enabled = true
        }
      }
      # 服務網格的規格,可以選擇三種規格:standard: 標準版(免費),enterprise:企業版,ultimate:旗艦版。
      cluster_spec = local.mesh_spec
    }
    
  2. 執行terraform apply,觀察到該欄位發生變更,符合預期。

    terraform apply
    random_uuid.this: Refreshing state... [id=6ab24265-2381-dad9-3be5-351329c5665a]
    data.alicloud_vpcs.default: Reading...
    data.alicloud_service_mesh_versions.default: Reading...
    data.alicloud_service_mesh_versions.default: Read complete after 1s [id=605899410]
    data.alicloud_vpcs.default: Read complete after 1s [id=2909606812]
    data.alicloud_vswitches.default: Reading...
    data.alicloud_vswitches.default: Read complete after 0s [id=866499268]
    alicloud_service_mesh_service_mesh.default: Refreshing state... [id=cab09b566d4a64c1fa05271d5365495f1]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      ~ update in-place
    
    Terraform will perform the following actions:
    
      # alicloud_service_mesh_service_mesh.default will be updated in-place
      ~ resource "alicloud_service_mesh_service_mesh" "default" {
            id                = "cab09b566d4a64c1fa05271d5365495f1"
            # (6 unchanged attributes hidden)
    
          ~ mesh_config {
                # (5 unchanged attributes hidden)
    
              ~ pilot {
                  ~ http10_enabled = false -> true
                    # (1 unchanged attribute hidden)
                }
    
                # (7 unchanged blocks hidden)
            }
    
            # (2 unchanged blocks hidden)
        }
    
    Plan: 0 to add, 1 to change, 0 to destroy.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value:
  3. 輸入yes應用變更

    ......省略無關內容......
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    alicloud_service_mesh_service_mesh.default: Modifying... [id=cab09b566d4a64c1fa05271d5365495f1]
    alicloud_service_mesh_service_mesh.default: Still modifying... [id=cab09b566d4a64c1fa05271d5365495f1, 10s elapsed]
    alicloud_service_mesh_service_mesh.default: Still modifying... [id=cab09b566d4a64c1fa05271d5365495f1, 20s elapsed]
    alicloud_service_mesh_service_mesh.default: Still modifying... [id=cab09b566d4a64c1fa05271d5365495f1, 30s elapsed]
    alicloud_service_mesh_service_mesh.default: Modifications complete after 37s [id=cab09b566d4a64c1fa05271d5365495f1]

添加或移除Kubernetes叢集

您可以通過修改tf檔案中的cluster_ids數組,將希望加入ASM管理的叢集ID追加至數組,將希望從ASM移除的叢集ID從數組中移除,並執行terraform apply將變更應用到ASM執行個體。

  1. 此處以添加叢集一個叢集到asm為例,修改服務網格資源的cluster_ids,在數組種追加叢集ID:

    ......省略無關內容......
    # 服務網格ASM執行個體。
    resource "alicloud_service_mesh_service_mesh" "default" {
      # 服務網格名稱。
      service_mesh_name = local.mesh_name
      # 服務網格的網路設定。
      network {
        # 專用網路ID。
        vpc_id        =  data.alicloud_vpcs.default.ids[0]
        # 虛擬交換器ID。
        vswitche_list = [local.exist_vswitch_ids[0]]
      }
      # 服務網格的版本。
      version = local.mesh_version
      # 服務網格的API Server和Pilot負載平衡配置。
      load_balancer {
        # 是否使用eip暴露服務網格API Server負載平衡。
        api_server_public_eip = true
      }
      cluster_ids = [
        "c94a1a1d968e04c55861b8747********" # 新增叢集ID到數組
      ]
      ......省略無關內容......
    }
    ......省略無關內容......
  2. 執行terraform apply,觀察到資料面叢集ID數組發生變更,符合預期

    random_uuid.this: Refreshing state... [id=6ab24265-2381-dad9-3be5-351329c5665a]
    data.alicloud_service_mesh_versions.default: Reading...
    data.alicloud_vpcs.default: Reading...
    data.alicloud_vpcs.default: Read complete after 1s [id=2909606812]
    data.alicloud_vswitches.default: Reading...
    data.alicloud_vswitches.default: Read complete after 0s [id=866499268]
    data.alicloud_service_mesh_versions.default: Read complete after 1s [id=3077056360]
    alicloud_service_mesh_service_mesh.default: Refreshing state... [id=c71fe2f2301234701b2e4116397426342]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      ~ update in-place
    
    Terraform will perform the following actions:
    
      # alicloud_service_mesh_service_mesh.default will be updated in-place
      ~ resource "alicloud_service_mesh_service_mesh" "default" {
          ~ cluster_ids       = [
              + "c94a1a1d968e04c55861b8747********",
            ]
            id                = "c71fe2f2301234701b2e4116397426342"
            tags              = {}
            # (6 unchanged attributes hidden)
        }
    
    Plan: 0 to add, 1 to change, 0 to destroy.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: 
  3. 輸入yes應用變更

    ......省略無關內容......
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    alicloud_service_mesh_service_mesh.default: Modifying... [id=c71fe2f2301234701b2e4116397426342]
    alicloud_service_mesh_service_mesh.default: Still modifying... [id=c71fe2f2301234701b2e4116397426342, 10s elapsed]
    alicloud_service_mesh_service_mesh.default: Still modifying... [id=c71fe2f2301234701b2e4116397426342, 20s elapsed]
    alicloud_service_mesh_service_mesh.default: Still modifying... [id=c71fe2f2301234701b2e4116397426342, 30s elapsed]
    alicloud_service_mesh_service_mesh.default: Still modifying... [id=c71fe2f2301234701b2e4116397426342, 40s elapsed]
    alicloud_service_mesh_service_mesh.default: Still modifying... [id=c71fe2f2301234701b2e4116397426342, 50s elapsed]
    alicloud_service_mesh_service_mesh.default: Still modifying... [id=c71fe2f2301234701b2e4116397426342, 1m0s elapsed]
    alicloud_service_mesh_service_mesh.default: Still modifying... [id=c71fe2f2301234701b2e4116397426342, 1m10s elapsed]
    alicloud_service_mesh_service_mesh.default: Still modifying... [id=c71fe2f2301234701b2e4116397426342, 1m20s elapsed]
    alicloud_service_mesh_service_mesh.default: Still modifying... [id=c71fe2f2301234701b2e4116397426342, 1m30s elapsed]
    alicloud_service_mesh_service_mesh.default: Still modifying... [id=c71fe2f2301234701b2e4116397426342, 1m40s elapsed]
    alicloud_service_mesh_service_mesh.default: Modifications complete after 1m44s [id=c71fe2f2301234701b2e4116397426342]
    
    Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

Terraform管理的ASM資源

ASM支援通過Terraform管理以下Resource和Data Source:

類型

名稱

描述

Resources

alicloud_service_mesh_service_mesh

管理ASM執行個體。

alicloud_service_mesh_user_permission

配置ASM執行個體許可權。

Data Sources

alicloud_service_mesh_service_meshes

列舉所有的ASM執行個體。

alicloud_service_mesh_versions

列舉所有可用的Service Mesh版本。

terraform apply看到欄位被刪除時的處理方式

為了簡化操作,ASM的部分屬性即使建立時不指定,服務端也會分配預設值,這個屬性類別似於terraform的Computed屬性標籤,但是若將這些屬性設定為Computed,則會使得這些值無法被改為空白(字串類型無法改為空白字串,數實值型別無法改為0,布爾類型無法改為false)。為了允許這些屬性被改為空白值,ASM Terraform Registry不能將這些屬性設定為Computed。在您執行terraform apply時,由於服務端返回了這些屬性,而tf檔案中未顯式聲明,terraform會認為您希望刪除這些值,此時,若您不希望置空這些屬性,則應當根據提示手動將它們補充到tf檔案中,再執行terraform apply