使用Terraform创建ACK Edge集群

Terraform是一种开源工具,用于安全高效地预览、配置和管理云基础架构和资源,帮助开发者自动化地创建、更新阿里云基础设施资源,并进行版本管理。本文介绍如何使用Terraform创建ACK Edge集群

说明

本教程所含示例代码支持一键运行,您可以直接运行代码。一键运行

前提条件

  • 已开通容器服务 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。

    • 在Terraform Explorer中使用Terraform:阿里云提供了Terraform的在线运行环境,您无需安装Terraform,登录后即可在线使用和体验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的节点安装云监控。
      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控制台

    登录容器服务管理控制台,查看已创建的集群。image

清理资源

当您不再需要上述通过Terraform创建或管理的资源时,请运行terraform destroy命令以释放资源。关于terraform destroy的更多信息,请参见Terraform常用命令

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的节点安装云监控。
  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
    ]
  }
}