All Products
Search
Document Center

Container Service for Kubernetes:Buat kluster khusus ACK dengan Terraform

Last Updated:Jun 19, 2026

Deploy kluster khusus ACK berbasis Terway dengan VPC, vSwitch, dan add-on yang diprovisikan secara otomatis.

Jalankan kode ini di browser Anda di OpenAPI Portal.
Penting

Pembuatan kluster khusus ACK dibatasi. Untuk meminta akses, kirim tiket.

Prasyarat

Pastikan hal-hal berikut:

  • ACK telah diaktifkan (aktifkan ACK menggunakan Terraform).

  • Pasangan Kunci Akses (AccessKey pair) telah dibuat untuk RAM user yang menjalankan Terraform (Buat Pasangan Kunci Akses, RAM users).

    Gunakan RAM user, bukan akun root Alibaba Cloud Anda. Izin terbatas mengurangi risiko jika kredensial dikompromikan.
  • Kebijakan izin minimum berikut telah dilampirkan ke RAM user. Mencakup pembuatan, penampilan, dan penghapusan Virtual Private Cloud (VPC), vSwitch, dan kluster ACK (berikan izin).

    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "vpc:CreateVpc",
            "vpc:CreateVSwitch",
            "vpc:DescribeRouteTableList",
            "vpc:DescribeVpcAttribute",
            "vpc:ListEnhancedNatGatewayAvailableZones",
            "vpc:DescribeVSwitchAttributes",
            "vpc:DescribeNatGateways",
            "cs:CreateCluster",
            "cs:DescribeTaskInfo",
            "cs:DescribeClusterDetail",
            "cs:DescribeClusterCerts",
            "cs:CheckControlPlaneLogEnable",
            "vpc:DeleteVpc",
            "vpc:DeleteVSwitch",
            "cs:DeleteCluster"
          ],
          "Resource": "*"
        }
      ]
    }
  • Terraform v0.12.28 atau versi lebih baru. Verifikasi dengan terraform --version.

    Option

    Best for

    Terraform Explorer — online, tanpa perlu instalasi

    Pengujian dan debugging cepat tanpa biaya

    Cloud Shell — pra-instal, telah dikonfigurasi dengan kredensial Anda

    Eksekusi cepat dan nyaman tanpa pengaturan lokal

    Resource Orchestration Service (ROS) — Terraform terkelola

    Template yang mendefinisikan resource Alibaba Cloud, AWS, atau Azure dengan siklus hidup terkelola

    Instalasi lokal

    Lingkungan pengembangan kustom atau akses jaringan terbatas

Resource Terraform

Konfigurasi ini menggunakan resource berikut. Biaya berlaku—lepaskan resource tersebut bila tidak lagi diperlukan.

Resource

Purpose

alicloud_instance_types

Menanyakan tipe instans Elastic Compute Service (ECS) yang memenuhi kondisi tertentu

alicloud_vpc

Membuat VPC

alicloud_vswitch

Membuat vSwitch (subnet) di dalam VPC

<a data-init-id="b7615243ea1tz" href="https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/resource_manager_resource_groups" id="9b1e64de7381t">alicloud_resource_manager_resource_groups</a>

Menanyakan kelompok sumber daya yang dikelola oleh RAM user

<a data-init-id="1c1e24d415isr" href="https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/cs_kubernetes" id="af0cee8a93ui4">alicloud_cs_kubernetes</a>

Membuat kluster khusus ACK

Konfigurasi ini memilih tipe instans yang tersedia di ketiga zona, memastikan node bidang kontrol dapat dijadwalkan di zona mana pun.

Hasilkan parameter Terraform dari konsol

Jika contoh tidak sesuai kebutuhan Anda, hasilkan parameter dari konsol ACK:

  1. Login ke Konsol ACK. Di panel navigasi kiri, klik Clusters.

  2. Di halaman Clusters, klik Cluster Templates.

  3. Pilih tipe kluster, klik Create, lalu konfigurasikan kluster di halaman Cluster Configurations.

  4. Di halaman Confirm, klik Console-to-Code.

  5. Di sidebar, klik tab Terraform dan salin parameter yang dihasilkan.

Buat kluster khusus ACK dengan Terway

Langkah-langkah berikut membuat kluster khusus ACK dengan Terway. Konfigurasi ini menyediakan VPC, tiga vSwitch bidang kontrol, tiga vSwitch pod Terway, dan kluster—total 8 resource.

Langkah 1: Buat file konfigurasi

Buat direktori kerja dan file main.tf dengan konten berikut:

provider "alicloud" {
  region = var.region_id
}

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

variable "zone_ids" {
  type    = list(string)
  default = ["cn-hangzhou-i","cn-hangzhou-j","cn-hangzhou-k"]
}

# Tentukan nama atau label resource.
variable "name" {
  default = "tf-example"
}

# Tentukan ID VPC yang sudah ada. Jika Anda biarkan variabel ini kosong, VPC baru akan dibuat.
variable "vpc_id" {
  description = "Existing vpc id used to create several vswitches and other resources."
  default     = ""
}

# Tentukan blok CIDR VPC baru. Variabel ini berlaku jika Anda biarkan variabel vpc_id kosong.
variable "vpc_cidr" {
  description = "The cidr block used to launch a new vpc when 'vpc_id' is not specified."
  default     = "10.0.0.0/8"
}

# Tentukan ID vSwitch yang sudah ada.
variable "vswitch_ids" {
  description = "List of existing vswitch id."
  type        = list(string)
  default     = []
}

# Jika vswitch_ids tidak ditentukan, Anda harus menentukan tiga blok CIDR untuk membuat vSwitch. Blok CIDR tersebut tidak boleh tumpang tindih satu sama lain.
variable "vswitch_cidrs" {
  description = "List of cidr blocks used to create several new vswitches when 'vswitch_ids' is not specified."
  type        = list(string)
  default     = ["10.1.0.0/16", "10.2.0.0/16", "10.3.0.0/16"]
}

# Tentukan konfigurasi Terway.
variable "terway_vswitch_ids" {
  description = "List of existing vswitch ids for terway."
  type        = list(string)
  default     = []
}

# Variabel ini menentukan blok CIDR tempat vSwitch Terway dibuat jika Anda biarkan variabel terway_vswitch_ids kosong.
variable "terway_vswitch_cidrs" {
  description = "List of cidr blocks used to create several new vswitches when 'terway_vswitch_cidrs' is not specified."
  type        = list(string)
  default     = ["10.4.0.0/16", "10.5.0.0/16", "10.6.0.0/16"]
}

# Tentukan komponen yang ingin Anda instal di kluster ACK. Anda harus menentukan nama dan konfigurasi setiap komponen yang ingin Anda instal.
variable "cluster_addons" {
  type = list(object({
    name   = string
    config = string
  }))

  default = [
    {
      "name"   = "terway-eniip",
      "config" = "",
    },
    {
      "name"   = "csi-plugin",
      "config" = "",
    },
    {
      "name"   = "csi-provisioner",
      "config" = "",
    },
    {
      "name"   = "logtail-ds",
      "config" = "{\"IngressDashboardEnabled\":\"true\"}",
    },
    {
      "name"   = "nginx-ingress-controller",
      "config" = "{\"IngressSlbNetworkType\":\"internet\"}",
    },
    {
      "name"   = "arms-prometheus",
      "config" = "",
    },
    {
      "name"   = "ack-node-problem-detector",
      "config" = "{\"sls_project_name\":\"\"}",
    }
  ]
}

locals {
  all_zone_ids = [for zones in data.alicloud_enhanced_nat_available_zones.enhanced.zones : zones.zone_id]
  common_zone_ids = setintersection(toset(var.zone_ids),toset(local.all_zone_ids))

  # Dapatkan tipe instans yang tersedia di setiap zona.
  instance_types_per_az = { for az, types in data.alicloud_instance_types.default : az => [for t in types.instance_types : t.id] }

  # Dapatkan daftar tipe instans di semua zona yang tercantum
  all_instance_types_in_zones = [for zone in local.common_zone_ids : local.instance_types_per_az[zone]]

  # Ubah setiap daftar menjadi koleksi
  sets = [for s in local.all_instance_types_in_zones : toset(s)]

  # Hitung tipe instans yang umum di semua zona.
  common_instance_types = [for element in local.sets[0]: element if length([for set in local.sets: set if contains(set, element)]) == length(local.sets)]
}

# Tanyakan zona yang mendukung enhanced NAT gateway.
data "alicloud_enhanced_nat_available_zones" "enhanced" {
}

# Jika Anda tidak menentukan variabel vpc_id, resource Terraform ini membuat VPC. Blok CIDR VPC ditentukan oleh variabel vpc_cidr.
resource "alicloud_vpc" "vpc" {
  count      = var.vpc_id == "" ?  1 : 0
  cidr_block = var.vpc_cidr
}

# Jika Anda tidak menentukan variabel vswitch_ids, vSwitch baru dibuat berdasarkan vswitch_cidrs.
resource "alicloud_vswitch" "vswitches" {
  count      = length(var.vswitch_ids) > 0 ?  0 : length(var.vswitch_cidrs)
  vpc_id     = var.vpc_id == "" ?  join("", alicloud_vpc.vpc.*.id) : var.vpc_id
  cidr_block = element(var.vswitch_cidrs, count.index)
  zone_id    = tolist(local.common_zone_ids)[count.index]
}

# Jika Anda tidak menentukan variabel terway_vswitch_ids, vSwitch yang digunakan oleh Terway dibuat berdasarkan parameter vswitch_cidrs.
resource "alicloud_vswitch" "terway_vswitches" {
  count      = length(var.terway_vswitch_ids) > 0 ?  0 : length(var.terway_vswitch_cidrs)
  vpc_id     = var.vpc_id == "" ?  join("", alicloud_vpc.vpc.*.id) : var.vpc_id
  cidr_block = element(var.terway_vswitch_cidrs, count.index)
  zone_id    = tolist(local.common_zone_ids)[count.index]
}

# Tanyakan kelompok sumber daya yang dikelola oleh RAM user.
data "alicloud_resource_manager_resource_groups" "default" {
  status = "OK"
}

# Tanyakan tipe instans ECS.
data "alicloud_instance_types" "default" {
  for_each            = toset(local.common_zone_ids )
  availability_zone   = each.key
  cpu_core_count      = 8
  memory_size         = 16
  kubernetes_node_role = "Master"
  system_disk_category = "cloud_essd"
}

# Buat kluster khusus ACK dan konfigurasikan vSwitch bidang kontrol, vSwitch pod, tipe instans, disk, password untuk login ke node, dan blok CIDR Service.
resource "alicloud_cs_kubernetes" "default" {
  master_vswitch_ids    = length(var.vswitch_ids) > 0 ?  split(",", join(",", var.vswitch_ids)) : length(var.vswitch_cidrs) < 1 ?  [] : split(",", join(",", alicloud_vswitch.vswitches.*.id)) # Tanyakan zona yang mendukung enhanced NAT gateway.
  pod_vswitch_ids       = length(var.terway_vswitch_ids) > 0 ?  split(",", join(",", var.terway_vswitch_ids)) : length(var.terway_vswitch_cidrs) < 1 ?  [] : split(",", join(",", alicloud_vswitch.terway_vswitches.*.id)) # Blok CIDR vSwitch pod jika Anda menggunakan Terway sebagai komponen jaringan.
  master_instance_types = [local.common_instance_types[0],local.common_instance_types[0],local.common_instance_types[0]] # Tipe instans bidang kontrol.
  master_disk_category  = "cloud_essd"            # Jenis disk sistem bidang kontrol.
  password              = "Yourpassword1234"     # Password yang digunakan untuk login SSH.
  service_cidr          = "172.18.0.0/16"        # Blok CIDR Service.
  load_balancer_spec    = "slb.s1.small"         # Tipe instans Server Load Balancer (SLB).
  install_cloud_monitor = "true"                 # Aktifkan CloudMonitor.
  resource_group_id     = data.alicloud_resource_manager_resource_groups.default.groups.0.id # ID kelompok sumber daya tempat kluster berada. Kelompok sumber daya digunakan untuk mengisolasi resource.
  deletion_protection   = "false"                # Perlindungan penghapusan dapat mencegah kluster dihapus secara tidak sengaja di konsol atau melalui pemanggilan operasi API.
  timezone              = "Asia/Shanghai"        # Zona waktu kluster.
  os_type               = "Linux"                # Jenis OS node.
  platform              = "AliyunLinux3"         # Distribusi OS.
  cluster_domain        = "cluster.local"        # Nama domain kluster.
  proxy_mode            = "ipvs"                 # Mode kube-proxy.
  custom_san            = "www.terraform.io"     # SAN kustom untuk sertifikat server API kluster.
  new_nat_gateway       = "true"                 # Buat gateway NAT.
  dynamic "addons" {
    for_each = var.cluster_addons
    content {
      name   = lookup(addons.value, "name", var.cluster_addons)
      config = lookup(addons.value, "config", var.cluster_addons)
    }
  }
}

Langkah 2: Inisialisasi Terraform

terraform init

Inisialisasi yang berhasil mengembalikan:

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.

Langkah 3: Pratinjau rencana eksekusi

terraform plan

Menampilkan pratinjau perubahan resource yang direncanakan tanpa menerapkannya.

Rencana yang berhasil mengembalikan:

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: 8 to add, 0 to change, 0 to destroy.
...

Langkah 4: Buat kluster

terraform apply

Saat diminta, ketik yes lalu tekan Enter.

Penerapan yang berhasil mengembalikan:

...
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_managed_kubernetes.default: Creation complete after 8m26s [id=************]

Apply complete!  Resources: 8 added, 0 changed, 0 destroyed.

Verifikasi kluster

Setelah terraform apply selesai, verifikasi kluster menggunakan salah satu metode berikut:

Jalankan perintah terraform show

Opsi 1: Gunakan `terraform show`

terraform show

Menampilkan semua resource yang dibuat Terraform beserta status terkininya.

Login ke konsol ACK

Opsi 2: Periksa konsol ACK

Masuk ke Konsol ACK dan pastikan kluster baru muncul dalam daftar Clusters.

Bersihkan resource

Penting

Menjalankan terraform destroy akan menghapus permanen semua resource yang dibuat oleh konfigurasi ini. Tindakan ini tidak dapat dibatalkan.

Lepaskan resource tersebut. Lihat Perintah umum untuk opsi terraform destroy.

terraform destroy

Saat diminta, ketik yes lalu tekan Enter. Penghapusan yang berhasil mengembalikan:

...
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: yes
...
Destroy complete!  Resources: 7 destroyed.

Kode contoh

Jalankan kode ini di Terraform Explorer.
provider "alicloud" {
  region = var.region_id
}

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

variable "zone_ids" {
  type    = list(string)
  default = ["cn-hangzhou-i","cn-hangzhou-j","cn-hangzhou-k"]
}

# Tentukan nama atau label resource.
variable "name" {
  default = "tf-example"
}

# Tentukan ID VPC yang sudah ada. Jika Anda biarkan variabel ini kosong, VPC baru akan dibuat.
variable "vpc_id" {
  description = "Existing vpc id used to create several vswitches and other resources."
  default     = ""
}

# Tentukan blok CIDR VPC baru. Variabel ini berlaku jika Anda biarkan variabel vpc_id kosong.
variable "vpc_cidr" {
  description = "The cidr block used to launch a new vpc when 'vpc_id' is not specified."
  default     = "10.0.0.0/8"
}

# Tentukan ID vSwitch yang sudah ada.
variable "vswitch_ids" {
  description = "List of existing vswitch id."
  type        = list(string)
  default     = []
}

# Jika vswitch_ids tidak ditentukan, Anda harus menentukan tiga blok CIDR untuk membuat vSwitch. Blok CIDR tersebut tidak boleh tumpang tindih satu sama lain.
variable "vswitch_cidrs" {
  description = "List of cidr blocks used to create several new vswitches when 'vswitch_ids' is not specified."
  type        = list(string)
  default     = ["10.1.0.0/16", "10.2.0.0/16", "10.3.0.0/16"]
}

# Tentukan konfigurasi Terway.
variable "terway_vswitch_ids" {
  description = "List of existing vswitch ids for terway."
  type        = list(string)
  default     = []
}

# Variabel ini menentukan blok CIDR tempat vSwitch Terway dibuat jika Anda biarkan variabel terway_vswitch_ids kosong.
variable "terway_vswitch_cidrs" {
  description = "List of cidr blocks used to create several new vswitches when 'terway_vswitch_cidrs' is not specified."
  type        = list(string)
  default     = ["10.4.0.0/16", "10.5.0.0/16", "10.6.0.0/16"]
}

# Tentukan komponen yang ingin Anda instal di kluster ACK. Anda harus menentukan nama dan konfigurasi setiap komponen yang ingin Anda instal.
variable "cluster_addons" {
  type = list(object({
    name   = string
    config = string
  }))

  default = [
    {
      "name"   = "terway-eniip",
      "config" = "",
    },
    {
      "name"   = "csi-plugin",
      "config" = "",
    },
    {
      "name"   = "csi-provisioner",
      "config" = "",
    },
    {
      "name"   = "logtail-ds",
      "config" = "{\"IngressDashboardEnabled\":\"true\"}",
    },
    {
      "name"   = "nginx-ingress-controller",
      "config" = "{\"IngressSlbNetworkType\":\"internet\"}",
    },
    {
      "name"   = "arms-prometheus",
      "config" = "",
    },
    {
      "name"   = "ack-node-problem-detector",
      "config" = "{\"sls_project_name\":\"\"}",
    }
  ]
}

locals {
  all_zone_ids = [for zones in data.alicloud_enhanced_nat_available_zones.enhanced.zones : zones.zone_id]
  common_zone_ids = setintersection(toset(var.zone_ids),toset(local.all_zone_ids))

  # Dapatkan tipe instans yang tersedia di setiap zona.
  instance_types_per_az = { for az, types in data.alicloud_instance_types.default : az => [for t in types.instance_types : t.id] }

  # Dapatkan daftar tipe instans di semua zona yang tercantum
  all_instance_types_in_zones = [for zone in local.common_zone_ids : local.instance_types_per_az[zone]]

  # Ubah setiap daftar menjadi koleksi
  sets = [for s in local.all_instance_types_in_zones : toset(s)]

  # Hitung tipe instans yang umum di semua zona.
  common_instance_types = [for element in local.sets[0]: element if length([for set in local.sets: set if contains(set, element)]) == length(local.sets)]
}

# Tanyakan zona yang mendukung enhanced NAT gateway.
data "alicloud_enhanced_nat_available_zones" "enhanced" {
}

# Jika Anda tidak menentukan variabel vpc_id, resource Terraform ini membuat VPC. Blok CIDR VPC ditentukan oleh variabel vpc_cidr.
resource "alicloud_vpc" "vpc" {
  count      = var.vpc_id == "" ?  1 : 0
  cidr_block = var.vpc_cidr
}

# Jika Anda tidak menentukan variabel vswitch_ids, vSwitch baru dibuat berdasarkan vswitch_cidrs.
resource "alicloud_vswitch" "vswitches" {
  count      = length(var.vswitch_ids) > 0 ?  0 : length(var.vswitch_cidrs)
  vpc_id     = var.vpc_id == "" ?  join("", alicloud_vpc.vpc.*.id) : var.vpc_id
  cidr_block = element(var.vswitch_cidrs, count.index)
  zone_id    = tolist(local.common_zone_ids)[count.index]
}

# Jika Anda tidak menentukan variabel terway_vswitch_ids, vSwitch yang digunakan oleh Terway dibuat berdasarkan parameter vswitch_cidrs.
resource "alicloud_vswitch" "terway_vswitches" {
  count      = length(var.terway_vswitch_ids) > 0 ?  0 : length(var.terway_vswitch_cidrs)
  vpc_id     = var.vpc_id == "" ?  join("", alicloud_vpc.vpc.*.id) : var.vpc_id
  cidr_block = element(var.terway_vswitch_cidrs, count.index)
  zone_id    = tolist(local.common_zone_ids)[count.index]
}

# Tanyakan kelompok sumber daya yang dikelola oleh RAM user.
data "alicloud_resource_manager_resource_groups" "default" {
  status = "OK"
}

# Tanyakan tipe instans ECS.
data "alicloud_instance_types" "default" {
  for_each            = toset(local.common_zone_ids )
  availability_zone   = each.key
  cpu_core_count      = 8
  memory_size         = 16
  kubernetes_node_role = "Master"
  system_disk_category = "cloud_essd"
}

# Buat kluster khusus ACK dan konfigurasikan vSwitch bidang kontrol, vSwitch pod, tipe instans, disk, password untuk login ke node, dan blok CIDR Service.
resource "alicloud_cs_kubernetes" "default" {
  master_vswitch_ids    = length(var.vswitch_ids) > 0 ?  split(",", join(",", var.vswitch_ids)) : length(var.vswitch_cidrs) < 1 ?  [] : split(",", join(",", alicloud_vswitch.vswitches.*.id)) # Tanyakan zona yang mendukung enhanced NAT gateway.
  pod_vswitch_ids       = length(var.terway_vswitch_ids) > 0 ?  split(",", join(",", var.terway_vswitch_ids)) : length(var.terway_vswitch_cidrs) < 1 ?  [] : split(",", join(",", alicloud_vswitch.terway_vswitches.*.id)) # Blok CIDR vSwitch pod jika Anda menggunakan Terway sebagai komponen jaringan.
  master_instance_types = [local.common_instance_types[0],local.common_instance_types[0],local.common_instance_types[0]] # Tipe instans bidang kontrol.
  master_disk_category  = "cloud_essd"            # Jenis disk sistem bidang kontrol.
  password              = "Yourpassword1234"     # Password yang digunakan untuk login SSH.
  service_cidr          = "172.18.0.0/16"        # Blok CIDR Service.
  load_balancer_spec    = "slb.s1.small"         # Tipe instans SLB.
  install_cloud_monitor = "true"                 # Aktifkan CloudMonitor.
  resource_group_id     = data.alicloud_resource_manager_resource_groups.default.groups.0.id # ID kelompok sumber daya tempat kluster berada. Kelompok sumber daya digunakan untuk mengisolasi resource.
  deletion_protection   = "false"                # Perlindungan penghapusan dapat mencegah kluster dihapus secara tidak sengaja di konsol atau melalui pemanggilan operasi API.
  timezone              = "Asia/Shanghai"        # Zona waktu kluster.
  os_type               = "Linux"                # Jenis OS node.
  platform              = "AliyunLinux3"         # Distribusi OS.
  cluster_domain        = "cluster.local"        # Nama domain kluster.
  proxy_mode            = "ipvs"                 # Mode kube-proxy.
  custom_san            = "www.terraform.io"     # SAN kustom untuk sertifikat server API kluster.
  new_nat_gateway       = "true"                 # Buat gateway NAT.
  dynamic "addons" {
    for_each = var.cluster_addons
    content {
      name   = lookup(addons.value, "name", var.cluster_addons)
      config = lookup(addons.value, "config", var.cluster_addons)
    }
  }
}

Langkah selanjutnya