全部產品
Search
文件中心

Container Service for Kubernetes:使用Terraform建立ACK Edge叢集

更新時間:Dec 04, 2024

Terraform是一種開源工具,用於安全高效地預覽、配置和管理雲基礎架構和資源,協助開發人員自動化地建立、更新阿里雲基礎設施資源,並進行版本管理。本文介紹如何使用Terraform建立ACK Edge叢集

說明

本教程所含範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行

前提條件

  • 已開通Container Service Edge 版

  • 由於阿里雲帳號(主帳號)具有資源的所有許可權,一旦發生泄露將面臨重大風險。建議您使用RAM使用者,並為該RAM使用者建立AccessKey,具體操作方式請參見建立RAM使用者建立AccessKey

  • 為運行Terraform命令的RAM使用者綁定以下最小權限原則,以擷取管理本樣本所涉及資源的許可權。更多資訊,請參見為RAM使用者授權

    該權限原則允許RAM使用者進行VPC、交換器及ACK的建立、查看與刪除操作。

    {
        "Version": "1",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "vpc:CreateVpc",
                    "vpc:CreateVSwitch",
                    "cs:CreateCluster",
                    "vpc:DescribeVpcAttribute",
                    "vpc:DescribeVSwitchAttributes",
                    "vpc:DescribeRouteTableList",
                    "vpc:DescribeNatGateways",
                    "cs:DescribeTaskInfo",
                    "cs:DescribeClusterDetail",
                    "cs:GetClusterCerts",
                    "cs:CheckControlPlaneLogEnable",
                    "cs:CreateClusterNodePool",
                    "cs:DescribeClusterNodePoolDetail",
                    "cs:DescribeClusterNodePools",
                    "cs:ScaleOutCluster",
                    "cs:DescribeClusterNodes",
                    "vpc:DeleteVpc",
                    "vpc:DeleteVSwitch",
                    "cs:DeleteCluster",
                    "cs:DeleteClusterNodepool"
                ],
                "Resource": "*"
            }
        ]
    }
  • 準備Terraform運行環境,您可以選擇以下任一方式來使用Terraform。

    • Cloud Shell:阿里雲Cloud Shell中預裝了Terraform的組件,並已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用於低成本、快速、便捷地訪問和使用Terraform的情境。

    • 在本地安裝和配置Terraform:適用於網路連接較差或需要自訂開發環境的情境。

    重要

    請確認Terraform版本不低於v0.12.28,可通過terraform --version命令查看Terraform版本。

使用的資源

說明

本教程樣本包含的部分資源會產生一定費用,請在不需要時及時進行釋放或退訂。

使用Terraform建立ACK Edge叢集

  1. 建立一個工作目錄,並在工作目錄下建立以下名為main.tf的設定檔。

    main.tf設定檔中描述了如下Terraform配置:

    • 建立一個新的VPC,並在該VPC下建立一個vSwitch。

    • 建立一個ACK Edge叢集

    • 建立一個包含兩個節點的節點池。

    provider "alicloud" {
      region = var.region_id
    }
    
    variable "region_id" {
      default = "cn-hangzhou"
    }
    
    variable "k8s_name_edge" {
      type        = string
      description = "The name used to create edge kubernetes cluster."
      default     = "edge-example"
    }
    
    variable "new_vpc_name" {
      type        = string
      description = "The name used to create vpc."
      default     = "tf-vpc-172-16"
    }
    
    variable "new_vsw_name" {
      type        = string
      description = "The name used to create vSwitch."
      default     = "tf-vswitch-172-16-0"
    }
    
    variable "nodepool_name" {
      type        = string
      description = "The name used to create node pool."
      default     = "edge-nodepool-1"
    }
    
    variable "k8s_login_password" {
      type    = string
      default = "Test123456"
    }
    
    variable "k8s_version" {
      type        = string
      description = "Kubernetes version"
      default     = "1.28.9-aliyun.1"
    }
    
    variable "containerd_runtime_version" {
      type    = string
      default = "1.6.34"
    }
    
    variable "cluster_spec" {
      type        = string
      description = "The cluster specifications of kubernetes cluster,which can be empty. Valid values:ack.standard : Standard managed clusters; ack.pro.small : Professional managed clusters."
      default     = "ack.pro.small"
    }
    
    data "alicloud_zones" "default" {
      available_resource_creation = "VSwitch"
      available_disk_category     = "cloud_efficiency"
    }
    
    data "alicloud_instance_types" "default" {
      availability_zone    = data.alicloud_zones.default.zones.0.id
      cpu_core_count       = 4
      memory_size          = 8
      kubernetes_node_role = "Worker"
    }
    
    resource "alicloud_vpc" "vpc" {
      vpc_name   = var.new_vpc_name
      cidr_block = "172.16.0.0/12"
    }
    
    resource "alicloud_vswitch" "vsw" {
      vswitch_name = var.new_vsw_name
      vpc_id       = alicloud_vpc.vpc.id
      cidr_block   = cidrsubnet(alicloud_vpc.vpc.cidr_block, 8, 8)
      zone_id      = data.alicloud_zones.default.zones.0.id
    }
    
    
    resource "alicloud_cs_edge_kubernetes" "edge" {
      name                  = var.k8s_name_edge
      version               = var.k8s_version
      cluster_spec          = var.cluster_spec
      worker_vswitch_ids    = split(",", join(",", alicloud_vswitch.vsw.*.id))
      worker_instance_types = [data.alicloud_instance_types.default.instance_types.0.id]
      password              = var.k8s_login_password
      new_nat_gateway       = true
      pod_cidr              = "10.10.0.0/16"
      service_cidr          = "10.12.0.0/16"
      load_balancer_spec    = "slb.s2.small"
      worker_number         = 1
      node_cidr_mask        = 24
    
      # 運行時。
      runtime = {
        name    = "containerd"
        version = var.containerd_runtime_version
      }
    }
    # 節點池。
    resource "alicloud_cs_kubernetes_node_pool" "nodepool" {
      # Kubernetes叢集名稱。
      cluster_id = alicloud_cs_edge_kubernetes.edge.id
      # 節點池名稱。
      node_pool_name = var.nodepool_name
      # 新的Kubernetes叢集將位於的vSwitch。指定一個或多個vSwitch的ID。它必須在availability_zone指定的地區中。
      vswitch_ids = split(",", join(",", alicloud_vswitch.vsw.*.id))
    
      # ECS執行個體類型和收費方式。
      instance_types       = [data.alicloud_instance_types.default.instance_types.0.id]
      instance_charge_type = "PostPaid"
    
      # 可選,自訂執行個體名稱。
      # node_name_mode      = "customized,edge-shenzhen,ip,default"
    
      #容器運行時。
      runtime_name    = "containerd"
      runtime_version = var.containerd_runtime_version
    
      # 叢集節點池的期望節點數。
      desired_size = 2
      # SSH登入叢集節點的密碼。
      password = var.k8s_login_password
    
      # 是否為Kubernetes的節點安裝CloudMonitor。
      install_cloud_monitor = true
    
      # 節點的系統磁碟類別。其有效值為cloud_ssd和cloud_efficiency。預設為cloud_efficiency。
      system_disk_category = "cloud_efficiency"
      system_disk_size     = 100
    
      # 作業系統類型。
      image_type = "AliyunLinux"
    
      # 節點資料盤配置。
      data_disks {
        # 節點資料盤種類。
        category = "cloud_efficiency"
        # 節點資料盤大小。
        size = 120
      }
      lifecycle {
        ignore_changes = [
          labels
        ]
      }
    }
  2. 執行以下命令,初始化Terraform運行環境。

    terraform init

    返回資訊如下,Terraform初始化成功。

    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 plan

    返回以下資訊,表示資源執行計畫已成功產生,您可以查看相關資源資訊。

    Refreshing Terraform state in-memory prior to plan...
    The refreshed state will be used to calculate this plan, but will not be
    persisted to local or remote state storage.
    ...
    Plan: 4 to add, 0 to change, 0 to destroy.
    ...
  4. 執行以下命令,建立ACK Edge叢集。

    terraform apply

    在執行過程中,根據提示輸入yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示叢集建立成功。

    ...
    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_cs_edge_kubernetes.edge: Creation complete after 8m26s [id=************]
    
    Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
  5. 驗證結果

    執行terraform show命令

    您可以使用以下命令查詢Terraform已建立的資來源詳細資料。

    terraform show

    image

    登入ACK控制台

    登入Container Service管理主控台,查看已建立的叢集。image

清理資源

當您不再需要上述通過Terraform建立或管理的資源時,請運行terraform destroy命令以釋放資源。關於terraform destroy的更多資訊,請參見常用命令

terraform destroy

完整樣本

說明

當前範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行

範例程式碼

provider "alicloud" {
  region = var.region_id
}

variable "region_id" {
  default = "cn-hangzhou"
}

variable "k8s_name_edge" {
  type        = string
  description = "The name used to create edge kubernetes cluster."
  default     = "edge-example"
}

variable "new_vpc_name" {
  type        = string
  description = "The name used to create vpc."
  default     = "tf-vpc-172-16"
}

variable "new_vsw_name" {
  type        = string
  description = "The name used to create vSwitch."
  default     = "tf-vswitch-172-16-0"
}

variable "nodepool_name" {
  type        = string
  description = "The name used to create node pool."
  default     = "edge-nodepool-1"
}

variable "k8s_login_password" {
  type    = string
  default = "Test123456"
}

variable "k8s_version" {
  type        = string
  description = "Kubernetes version"
  default     = "1.28.9-aliyun.1"
}

variable "containerd_runtime_version" {
  type    = string
  default = "1.6.34"
}

variable "cluster_spec" {
  type        = string
  description = "The cluster specifications of kubernetes cluster,which can be empty. Valid values:ack.standard : Standard managed clusters; ack.pro.small : Professional managed clusters."
  default     = "ack.pro.small"
}

data "alicloud_zones" "default" {
  available_resource_creation = "VSwitch"
  available_disk_category     = "cloud_efficiency"
}

data "alicloud_instance_types" "default" {
  availability_zone    = data.alicloud_zones.default.zones.0.id
  cpu_core_count       = 4
  memory_size          = 8
  kubernetes_node_role = "Worker"
}

resource "alicloud_vpc" "vpc" {
  vpc_name   = var.new_vpc_name
  cidr_block = "172.16.0.0/12"
}

resource "alicloud_vswitch" "vsw" {
  vswitch_name = var.new_vsw_name
  vpc_id       = alicloud_vpc.vpc.id
  cidr_block   = cidrsubnet(alicloud_vpc.vpc.cidr_block, 8, 8)
  zone_id      = data.alicloud_zones.default.zones.0.id
}


resource "alicloud_cs_edge_kubernetes" "edge" {
  name                  = var.k8s_name_edge
  version               = var.k8s_version
  cluster_spec          = var.cluster_spec
  worker_vswitch_ids    = split(",", join(",", alicloud_vswitch.vsw.*.id))
  worker_instance_types = [data.alicloud_instance_types.default.instance_types.0.id]
  password              = var.k8s_login_password
  new_nat_gateway       = true
  pod_cidr              = "10.10.0.0/16"
  service_cidr          = "10.12.0.0/16"
  load_balancer_spec    = "slb.s2.small"
  worker_number         = 1
  node_cidr_mask        = 24

  # 運行時。
  runtime = {
    name    = "containerd"
    version = var.containerd_runtime_version
  }
}
# 節點池。
resource "alicloud_cs_kubernetes_node_pool" "nodepool" {
  # Kubernetes叢集名稱。
  cluster_id = alicloud_cs_edge_kubernetes.edge.id
  # 節點池名稱。
  node_pool_name = var.nodepool_name
  # 新的Kubernetes叢集將位於的vSwitch。指定一個或多個vSwitch的ID。它必須在availability_zone指定的地區中。
  vswitch_ids = split(",", join(",", alicloud_vswitch.vsw.*.id))

  # ECS執行個體類型和收費方式。
  instance_types       = [data.alicloud_instance_types.default.instance_types.0.id]
  instance_charge_type = "PostPaid"

  # 可選,自訂執行個體名稱。
  # node_name_mode      = "customized,edge-shenzhen,ip,default"

  #容器運行時。
  runtime_name    = "containerd"
  runtime_version = var.containerd_runtime_version

  # 叢集節點池的期望節點數。
  desired_size = 2
  # SSH登入叢集節點的密碼。
  password = var.k8s_login_password

  # 是否為Kubernetes的節點安裝CloudMonitor。
  install_cloud_monitor = true

  # 節點的系統磁碟類別。其有效值為cloud_ssd和cloud_efficiency。預設為cloud_efficiency。
  system_disk_category = "cloud_efficiency"
  system_disk_size     = 100

  # 作業系統類型。
  image_type = "AliyunLinux"

  # 節點資料盤配置。
  data_disks {
    # 節點資料盤種類。
    category = "cloud_efficiency"
    # 節點資料盤大小。
    size = 120
  }
  lifecycle {
    ignore_changes = [
      labels
    ]
  }
}