All Products
Search
Document Center

Container Service for Kubernetes:Buat kluster ACK yang dikelola dengan Terraform

Last Updated:Sep 18, 2026

Gunakan Terraform untuk menyediakan kluster ACK yang dikelola lengkap dengan beberapa kelompok node dan add-on penting.

Catatan

Anda dapat menjalankan kode contoh dalam tutorial ini langsung dari OpenAPI Portal.

Prasyarat

  • ACK telah diaktifkan (aktifkan ACK menggunakan Terraform).

  • Akun Alibaba Cloud Anda harus memiliki izin penuh atas semua resource. Jika kredensial akun Alibaba Cloud Anda bocor, Anda mungkin menghadapi risiko keamanan yang signifikan. Kami menyarankan agar Anda menggunakan pengguna Resource Access Management (RAM) dan membuat AccessKey untuk pengguna RAM tersebut. Untuk informasi selengkapnya, lihat Buat pengguna RAM dan Buat AccessKey.

  • Lampirkan kebijakan hak istimewa minimal berikut ke pengguna RAM yang Anda gunakan untuk menjalankan perintah Terraform. Kebijakan ini memberikan izin kepada pengguna RAM untuk mengelola resource dalam contoh ini. Untuk informasi selengkapnya, lihat Kelola izin pengguna RAM.

    Kebijakan izin ini memungkinkan pengguna RAM untuk membuat, melihat, dan menghapus VPC, vSwitch, serta kluster 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:ModifyClusterNodePool",
            "vpc:DeleteVpc",
            "vpc:DeleteVSwitch",
            "cs:DeleteCluster",
            "cs:DeleteClusterNodepool"
          ],
          "Resource": "*"
        }
      ]
    }
  • Persiapkan lingkungan runtime Terraform. Anda dapat menggunakan Terraform dengan salah satu cara berikut:

    Penting

    Pastikan versi Terraform Anda adalah 0.12.28 atau lebih baru. Untuk memeriksa versi, jalankan perintah terraform --version.

Sumber Daya

Catatan

Beberapa resource yang digunakan dalam contoh ini dikenai biaya. Untuk menghindari tagihan yang tidak diinginkan, hapus resource ini saat Anda tidak lagi membutuhkannya.

Hasilkan konfigurasi Terraform dari konsol

Untuk menyesuaikan konfigurasi di luar contoh yang disediakan, ekspor konfigurasi Terraform dari konsol ACK.

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

  2. Pada halaman Clusters, klik Cluster Templates.

  3. Dalam kotak dialog, pilih jenis kluster, klik Create, lalu konfigurasikan detail kluster pada halaman Cluster Configurations.

  4. Setelah selesai mengonfigurasi, buka langkah Confirm dan klik Console-to-Code di pojok kanan atas.

  5. Di sidebar, klik tab Terraform untuk melihat dan menyalin kode HCL yang dihasilkan secara otomatis.

Buat kluster ACK yang dikelola dengan Terway

Contoh ini membuat kluster ACK yang dikelola dengan kelompok node reguler, kelompok node terkelola, dan kelompok node autoscaling. Kluster mencakup beberapa add-on bawaan: Terway (jaringan), csi-plugin dan csi-provisioner (penyimpanan), loongcollector (logging), Nginx Ingress Controller, ack-arms-prometheus (pemantauan), serta ack-node-problem-detector (diagnostik node).

  1. Buat direktori kerja, buat file konfigurasi bernama main.tf di direktori kerja, lalu salin kode berikut ke dalam main.tf.

    provider "alicloud" {
      region = var.region_id
    }
    
    variable "region_id" {
      type    = string
      default = "cn-shenzhen"
    }
    
    variable "cluster_spec" {
      type        = string
      description = "Spesifikasi untuk kluster Kubernetes. Nilai ini bisa kosong. Nilai yang valid: ack.standard: kluster terkelola Standar; ack.pro.small: kluster terkelola Profesional."
      default     = "ack.pro.small"
    }
    
    # Zona ketersediaan untuk vSwitch.
    variable "availability_zone" {
      description = "Zona ketersediaan untuk vSwitch."
      default     = ["cn-shenzhen-c", "cn-shenzhen-e", "cn-shenzhen-f"]
    }
    
    # Daftar ID vSwitch yang sudah ada.
    variable "node_vswitch_ids" {
      description = "Daftar ID vSwitch yang sudah ada untuk node Terway."
      type        = list(string)
      default     = []
    }
    
    # Daftar blok CIDR untuk membuat vSwitch baru.
    variable "node_vswitch_cidrs" {
      description = "Daftar blok CIDR untuk membuat vSwitch baru ketika 'node_vswitch_ids' tidak ditentukan."
      type        = list(string)
      default     = ["172.16.0.0/23", "172.16.2.0/23", "172.16.4.0/23"]
    }
    
    # Konfigurasi add-on jaringan Terway. Jika kosong, vSwitch Terway baru akan dibuat berdasarkan terway_vswitch_cidrs secara default.
    variable "terway_vswitch_ids" {
      description = "Daftar ID vswitch pod yang sudah ada untuk terway."
      type        = list(string)
      default     = []
    }
    
    # Blok CIDR untuk membuat vSwitch untuk Terway ketika terway_vswitch_ids tidak ditentukan.
    variable "terway_vswitch_cidrs" {
      description = "Daftar blok cidr yang digunakan untuk membuat beberapa vswitch baru ketika 'terway_vswitch_ids' tidak ditentukan."
      type        = list(string)
      default     = ["172.16.208.0/20", "172.16.224.0/20", "172.16.240.0/20"]
    }
    
    # Tipe instans ECS untuk node pekerja.
    variable "worker_instance_types" {
      description = "Tipe instans ECS untuk node pekerja."
      default     = ["ecs.g6.2xlarge", "ecs.g6.xlarge"]
    }
    
    # Add-on yang akan diinstal pada kluster ACK. Termasuk Terway (jaringan), csi-plugin (penyimpanan), csi-provisioner (penyimpanan), loongcollector (logging), Nginx Ingress Controller, ack-arms-prometheus (pemantauan), dan ack-node-problem-detector (diagnostik node).
    variable "cluster_addons" {
      type = list(object({
        name   = string
        config = string
      }))
      default = [
        {
          "name"   = "terway-eniip",
          "config" = "",
        },
        {
          "name"   = "loongcollector",
          "config" = "{\"IngressDashboardEnabled\":\"true\"}",
        },
        {
          "name"   = "nginx-ingress-controller",
          "config" = "{\"IngressSlbNetworkType\":\"internet\"}",
        },
        {
          "name"   = "arms-prometheus",
          "config" = "",
        },
        {
          "name"   = "ack-node-problem-detector",
          "config" = "{\"sls_project_name\":\"\"}",
        },
        {
          "name"   = "csi-plugin",
          "config" = "",
        },
        {
          "name"   = "csi-provisioner",
          "config" = "",
        }
      ]
    }
    
    # Awalan nama untuk kluster ACK yang dikelola.
    variable "k8s_name_prefix" {
      description = "Awalan nama untuk kluster Kubernetes terkelola."
      default     = "tf-ack-shenzhen"
    }
    
    # Nama resource default.
    locals {
      k8s_name_terway         = substr(join("-", [var.k8s_name_prefix, "terway"]), 0, 63)
      k8s_name_flannel        = substr(join("-", [var.k8s_name_prefix, "flannel"]), 0, 63)
      k8s_name_ask            = substr(join("-", [var.k8s_name_prefix, "ask"]), 0, 63)
      new_vpc_name            = "tf-vpc-172-16"
      new_vsw_name_azD        = "tf-vswitch-azD-172-16-0"
      new_vsw_name_azE        = "tf-vswitch-azE-172-16-2"
      new_vsw_name_azF        = "tf-vswitch-azF-172-16-4"
      nodepool_name           = "default-nodepool"
      managed_nodepool_name   = "managed-node-pool"
      autoscale_nodepool_name = "autoscale-node-pool"
      log_project_name        = "log-for-${local.k8s_name_terway}"
    }
    
    # Konfigurasi instans ECS untuk node. Menanyakan tipe instans ECS yang memenuhi persyaratan CPU dan memori.
    data "alicloud_instance_types" "default" {
      cpu_core_count       = 8
      memory_size          = 32
      availability_zone    = var.availability_zone[0]
      kubernetes_node_role = "Worker"
    }
    
    # VPC.
    resource "alicloud_vpc" "default" {
      vpc_name   = local.new_vpc_name
      cidr_block = "172.16.0.0/12"
    }
    
    # vSwitch node.
    resource "alicloud_vswitch" "vswitches" {
      count      = length(var.node_vswitch_ids) > 0 ? 0 : length(var.node_vswitch_cidrs)
      vpc_id     = alicloud_vpc.default.id
      cidr_block = element(var.node_vswitch_cidrs, count.index)
      zone_id    = element(var.availability_zone, count.index)
    }
    
    # vSwitch untuk pod.
    resource "alicloud_vswitch" "terway_vswitches" {
      count      = length(var.terway_vswitch_ids) > 0 ? 0 : length(var.terway_vswitch_cidrs)
      vpc_id     = alicloud_vpc.default.id
      cidr_block = element(var.terway_vswitch_cidrs, count.index)
      zone_id    = element(var.availability_zone, count.index)
    }
    
    # Kluster ACK yang dikelola.
    resource "alicloud_cs_managed_kubernetes" "default" {
      name                         = local.k8s_name_terway                                         # Nama kluster Kubernetes.
      cluster_spec                 = var.cluster_spec                                              # Membuat kluster terkelola profesional.
      vswitch_ids                  = split(",", join(",", alicloud_vswitch.vswitches.*.id))        # vSwitch untuk kelompok node. Tentukan satu atau beberapa ID vSwitch. vSwitch harus berada di zona ketersediaan yang ditentukan.
      pod_vswitch_ids              = split(",", join(",", alicloud_vswitch.terway_vswitches.*.id)) # vSwitch Pod.
      new_nat_gateway              = true                                                          # Menentukan apakah akan membuat gerbang nat baru saat membuat kluster Kubernetes. Nilai default: true.
      service_cidr                 = "10.11.0.0/16"                                                # Blok CIDR Layanan, diperlukan saat menggunakan jaringan Flannel. CIDR ini tidak boleh tumpang tindih dengan blok CIDR VPC atau kluster Kubernetes lain di VPC yang sama. Parameter ini tidak dapat diubah setelah kluster dibuat. Kluster mendukung maksimal 256 node.
      slb_internet_enabled         = true                                                          # Menentukan apakah akan membuat load balancer yang menghadap Internet untuk server API. Nilai default: false.
      enable_rrsa                  = true
      control_plane_log_components = ["apiserver", "kcm", "scheduler", "ccm"] # Log control plane.
      dynamic "addons" {                                                      # Konfigurasi add-on.
        for_each = var.cluster_addons
        content {
          name   = lookup(addons.value, "name", var.cluster_addons)
          config = lookup(addons.value, "config", var.cluster_addons)
        }
      }
    }
    
    # Kelompok node reguler.
    resource "alicloud_cs_kubernetes_node_pool" "default" {
      cluster_id            = alicloud_cs_managed_kubernetes.default.id              # ID kluster Kubernetes.
      node_pool_name        = local.nodepool_name                                    # Nama kelompok node.
      vswitch_ids           = split(",", join(",", alicloud_vswitch.vswitches.*.id)) # vSwitch untuk kelompok node. Tentukan satu atau beberapa ID vSwitch. vSwitch harus berada di zona ketersediaan yang ditentukan.
      instance_types        = var.worker_instance_types
      instance_charge_type  = "PostPaid"
      desired_size          = 2            # Jumlah node yang diinginkan dalam kelompok node.
      install_cloud_monitor = true         # Menentukan apakah akan menginstal CloudMonitor pada node.
      system_disk_category  = "cloud_efficiency"
      system_disk_size      = 100
      image_type            = "AliyunLinux"
      data_disks {              # Konfigurasi disk data untuk node.
        category = "cloud_essd" # Kategori disk data.
        size     = 120          # Ukuran disk data.
      }
    }
    
    # Buat kelompok node terkelola.
    resource "alicloud_cs_kubernetes_node_pool" "managed_node_pool" {
      cluster_id     = alicloud_cs_managed_kubernetes.default.id              # ID kluster Kubernetes.
      node_pool_name = local.managed_nodepool_name                            # Nama kelompok node.
      vswitch_ids    = split(",", join(",", alicloud_vswitch.vswitches.*.id)) # vSwitch untuk kelompok node. Tentukan satu atau beberapa ID vSwitch. vSwitch harus berada di zona ketersediaan yang ditentukan.
      desired_size   = 0                                                      # Jumlah node yang diinginkan dalam kelompok node.
      management {
        auto_repair     = true
        auto_upgrade    = true
        max_unavailable = 1
      }
      instance_types        = var.worker_instance_types
      instance_charge_type  = "PostPaid"
      install_cloud_monitor = true
      system_disk_category  = "cloud_efficiency"
      system_disk_size      = 100
      image_type            = "AliyunLinux"
      data_disks {
        category = "cloud_essd"
        size     = 120
      }
    }
    
    # Buat kelompok node autoscaling. Kelompok node ini dapat diskalakan hingga maksimal 10 node dan harus memiliki minimal 1 node.
    resource "alicloud_cs_kubernetes_node_pool" "autoscale_node_pool" {
      cluster_id     = alicloud_cs_managed_kubernetes.default.id
      node_pool_name = local.autoscale_nodepool_name
      vswitch_ids    = split(",", join(",", alicloud_vswitch.vswitches.*.id))
      scaling_config {
        min_size = 1
        max_size = 10
      }
      instance_types        = var.worker_instance_types
      install_cloud_monitor = true         # Menentukan apakah akan menginstal CloudMonitor pada node.
      system_disk_category  = "cloud_efficiency"
      system_disk_size      = 100
      image_type            = "AliyunLinux3"
      data_disks {              # Konfigurasi disk data untuk node.
        category = "cloud_essd" # Kategori disk data.
        size     = 120          # Ukuran disk data.
      }
    }
  2. Jalankan perintah berikut untuk menginisialisasi lingkungan runtime Terraform.

    terraform init

    Output yang diharapkan:

    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. Buat rencana eksekusi dan pratinjau perubahan.

    terraform plan
  4. Jalankan perintah berikut untuk membuat kluster.

    terraform apply

    Ketik yes saat diminta dan tekan Enter. Output berikut menunjukkan bahwa kluster ACK telah dibuat:

    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 5m48s [id=ccb53e72ec6c447c990762800********]
    ...
    
    Apply complete! Resources: 11 added, 0 changed, 0 destroyed.
  5. Verifikasi hasil

    Terraform show

    Jalankan perintah berikut untuk melihat detail resource yang dibuat oleh Terraform.

    terraform show

    Konsol ACK

    Masuk ke Konsol ACK untuk melihat kluster yang telah dibuat.

Pembersihan resource

Untuk melepaskan resource dan menghentikan biaya, jalankan perintah terraform destroy. Untuk informasi selengkapnya tentang perintah terraform destroy, lihat Perintah umum.

terraform destroy