すべてのプロダクト
Search
ドキュメントセンター

Container Service for Kubernetes:Terraformを使用したACK Edgeクラスターの作成

最終更新日:Jan 07, 2025

Terraformは、クラウドインフラストラクチャとリソースを安全かつ効率的にプレビュー、設定、管理できるオープンソースツールです。 Terraformを使用して、Alibaba Cloudインフラストラクチャとリソースを自動的に作成および更新し、要件に基づいてバージョンを管理できます。 このトピックでは、Terraformを使用してACK Edgeクラスターを作成する方法について説明します。

説明

このトピックのサンプルコードは、数回クリックするだけで実行できます。 詳細については、「Terraform Explorer」をご参照ください。

前提条件

  • ACK Edgeが有効になります。

  • デフォルトでは、Alibaba Cloudアカウントには、このアカウントに属するすべてのリソースに対する完全な権限があります。 Alibaba Cloudアカウントの資格情報が漏洩すると、セキュリティリスクが発生する可能性があります。 Resource Access Management (RAM) ユーザーを使用してリソースを管理することを推奨します。 RAMユーザーを作成するときは、RAMユーザーのAccessKeyペアを作成する必要があります。 詳細については、「RAMユーザーの作成」および「AccessKeyペアの作成」をご参照ください。

  • Terraformでコマンドを実行するために使用するRAMユーザーには、次のポリシーが添付されています。 ポリシーには、Terraformでコマンドを実行するために必要な最小限の権限が含まれます。 詳細については、「RAM ユーザーへの権限の付与」をご参照ください。

    このポリシーにより、Resource Access Management (RAM) ユーザーは、仮想プライベートクラウド (VPC) 、vSwitch、およびContainer Service for Kubernetes (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 Explorer: Alibaba Cloudは、Terraformのオンラインランタイム環境を提供します。 Terraformをインストールしなくても、环境にログオンしてTerraformを使用できます。 この方法は、低コストで効率的かつ便利な方法でTerraformを使用およびデバッグする必要があるシナリオに適しています。

    • Cloud ShellでTerraformを使用: Cloud ShellはTerraformとともにプリインストールされ、ID認証情報で構成されています。 Cloud ShellでTerraformコマンドを実行できます。 この方法は、低コストで効率的かつ便利な方法でTerraformを使用してアクセスする必要があるシナリオに適しています。

    • オンプレミスマシンにTerraformをインストールして構成する: この方法は、ネットワーク接続が不安定な場合やカスタム開発環境が必要な場合に適しています。

    重要

    Terraform 0.12.28以降をインストールする必要があります。 terraform -- versionコマンドを実行して、Terraformバージョンを照会できます。

Resources

説明

特定のリソースに対して課金されます。 リソースが不要になった場合は、できるだけ早い機会にリソースを解放または購読解除する必要があります。

Terraformを使用したACK Edgeクラスターの作成

  1. 作業ディレクトリとmain.tfという名前のファイルをディレクトリに作成します。

    main.tfファイルは、Terraformの次の設定を構成するために使用されます。

    • VPCを作成し、VPCにvSwitchを作成します。

    • ACK Edgeクラスターを作成します。

    • 2つのノードを含むノードプールを作成します。

    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
    
      # The container runtime. 
      runtime = {
        name    = "containerd"
        version = var.containerd_runtime_version
      }
    }
    # The node pool. 
    resource "alicloud_cs_kubernetes_node_pool" "nodepool" {
      # The name of the cluster. 
      cluster_id = alicloud_cs_edge_kubernetes.edge.id
      # The name of the node pool. 
      node_pool_name = var.nodepool_name
      # The vSwitches of the new Kubernetes cluster. Specify the IDs of one or more vSwitches. The vSwitches must be in the zone specified by availability_zone. 
      vswitch_ids = split(",", join(",", alicloud_vswitch.vsw.*.id))
    
      # The Elastic Compute Service (ECS) instance types and billing method. 
      instance_types       = [data.alicloud_instance_types.default.instance_types.0.id]
      instance_charge_type = "PostPaid"
    
      # Specify custom node names. This parameter is optional. 
      # node_name_mode      = "customized,edge-shenzhen,ip,default"
    
      # The container runtime. 
      runtime_name    = "containerd"
      runtime_version = var.containerd_runtime_version
    
      # The expected number of nodes in the node pool. 
      desired_size = 2
      # The password that is used to log on to the cluster by using SSH. 
      password = var.k8s_login_password
    
      # Specify whether to install the CloudMonitor agent on nodes. 
      install_cloud_monitor = true
    
      # The type of the system disks of the nodes. Valid values: cloud_ssd and cloud_efficiency. Default value: cloud_efficiency. 
      system_disk_category = "cloud_efficiency"
      system_disk_size     = 100
    
      # The OS type. 
      image_type = "AliyunLinux"
    
      # The configurations of the data disks of the nodes. 
      data_disks {
        # The type of data disks. 
        category = "cloud_efficiency"
        # The disk size. 
        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コンソールへのログイン

    ACKコンソールにログインし、作成したクラスターを表示します。image

リソースのクリア

Terraformによって作成または管理された前述のリソースが不要になった場合は、terraform destroyコマンドを実行してリソースを解放します。 terraform destroyコマンドの詳細については、「一般的なコマンド」をご参照ください。

terraform destroy

サンプルコード

説明

このトピックのサンプルコードは、数回クリックするだけで実行できます。 詳細については、「Terraform Explorer」をご参照ください。

サンプルコード

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

  # The container runtime. 
  runtime = {
    name    = "containerd"
    version = var.containerd_runtime_version
  }
}
# The node pool. 
resource "alicloud_cs_kubernetes_node_pool" "nodepool" {
  # The name of the cluster. 
  cluster_id = alicloud_cs_edge_kubernetes.edge.id
  # The name of the node pool. 
  node_pool_name = var.nodepool_name
  # The vSwitches of the new Kubernetes cluster. Specify the IDs of one or more vSwitches. The vSwitches must be in the zone specified by availability_zone. 
  vswitch_ids = split(",", join(",", alicloud_vswitch.vsw.*.id))

  # The ECS instance types and billing method. 
  instance_types       = [data.alicloud_instance_types.default.instance_types.0.id]
  instance_charge_type = "PostPaid"

  # Specify custom node names. This parameter is optional. 
  # node_name_mode      = "customized,edge-shenzhen,ip,default"

  # The container runtime. 
  runtime_name    = "containerd"
  runtime_version = var.containerd_runtime_version

  # The expected number of nodes in the node pool. 
  desired_size = 2
  # The password that is used to log on to the cluster by using SSH. 
  password = var.k8s_login_password

  # Specify whether to install the CloudMonitor agent on nodes. 
  install_cloud_monitor = true

  # The type of the system disks of the nodes. Valid values: cloud_ssd and cloud_efficiency. Default value: cloud_efficiency. 
  system_disk_category = "cloud_efficiency"
  system_disk_size     = 100

  # The OS type. 
  image_type = "AliyunLinux"

  # The configurations of the data disks of the nodes. 
  data_disks {
    # The type of data disks. 
    category = "cloud_efficiency"
    # The disk size. 
    size = 120
  }
  lifecycle {
    ignore_changes = [
      labels
    ]
  }
}