Topik ini menjelaskan cara membuat klaster khusus ACK menggunakan Terraform.
Anda dapat menjalankan kode contoh dalam tutorial ini dengan satu klik.Jalankan dengan satu klik
Pembuatan klaster khusus ACK baru dibatasi. Untuk mengaktifkan fitur ini, kirim tiket.
Prasyarat
Aktifkan Container Service for Kubernetes (ACK). Untuk informasi selengkapnya tentang cara mengaktifkan ACK menggunakan Terraform, lihat Aktifkan ACK dan berikan izin kepada peran menggunakan Terraform.
Akun Alibaba Cloud memiliki izin penuh atas semua resource dalam akun tersebut. Jika kredensial akun Alibaba Cloud bocor, resource Anda akan terpapar pada risiko keamanan yang besar. Kami menyarankan Anda menggunakan pengguna Resource Access Management (RAM) dan membuat AccessKey untuk pengguna tersebut. Untuk informasi selengkapnya, lihat Buat pengguna RAM dan Buat AccessKey.
Kebijakan berikut dilampirkan pada pengguna RAM yang Anda gunakan untuk menjalankan perintah di Terraform. Kebijakan ini mencakup izin minimum yang diperlukan untuk menjalankan perintah di Terraform. Untuk informasi selengkapnya, lihat Berikan izin kepada pengguna RAM.
Kebijakan akses ini memungkinkan pengguna RAM untuk membuat, melihat, dan menghapus VPC, vSwitch, serta klaster ACK.
{ "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": "*" } ] }Persiapkan lingkungan runtime Terraform. Anda dapat menjalankan Terraform dengan salah satu metode berikut.
Gunakan Terraform di Terraform Explorer: Alibaba Cloud menyediakan lingkungan runtime online untuk Terraform yang dapat Anda gunakan tanpa instalasi. Metode ini cocok untuk skenario di mana Anda ingin menguji dan men-debug Terraform secara cepat dan nyaman tanpa biaya.
Cloud Shell: Cloud Shell telah dipasang sebelumnya dengan Terraform dan dikonfigurasi dengan kredensial identitas Anda. Anda dapat langsung menjalankan perintah Terraform di Cloud Shell. Metode ini merupakan cara cepat, nyaman, dan hemat biaya untuk menggunakan Terraform.
Gunakan Terraform di Resource Orchestration Service (ROS): ROS menyediakan kemampuan terkelola untuk Terraform. Anda dapat membuat template Terraform, mendefinisikan resource Alibaba Cloud, AWS, atau Azure, serta mengonfigurasi parameter dan dependensi resource.
Instal dan konfigurasikan Terraform di komputer Anda: Metode ini cocok untuk skenario di mana koneksi jaringan Anda buruk atau Anda memerlukan lingkungan pengembangan kustom.
PentingPastikan versi Terraform Anda adalah 0.12.28 atau lebih baru. Untuk memeriksa versi, jalankan perintah
terraform --version.
Resource yang digunakan
Beberapa resource yang digunakan dalam topik ini menggunakan model bayar sesuai penggunaan. Pastikan Anda melepaskan resource tersebut ketika tidak lagi diperlukan untuk menghindari biaya tak terduga.
alicloud_instance_types: Menanyakan tipe instans ECS yang memenuhi kriteria tertentu.
alicloud_vpc: Membuat virtual private cloud (VPC).
alicloud_vswitch: Membuat virtual switch (vSwitch) untuk membagi VPC menjadi satu atau beberapa subnet.
alicloud_resource_manager_resource_groups: Menanyakan kelompok resource dari akun Alibaba Cloud saat ini.
alicloud_cs_kubernetes: Membuat klaster khusus ACK.
Hasilkan parameter permintaan Terraform dari konsol
Jika contoh yang tersedia tidak mencakup konfigurasi yang Anda butuhkan atau kombinasi parameter Anda salah, Anda dapat menghasilkan parameter yang diperlukan dari konsol. Untuk melakukannya, ikuti langkah-langkah berikut:
Masuk ke Konsol ACK. Di panel navigasi kiri, klik Clusters.
Pada halaman Clusters, klik Cluster Templates.
Pada kotak dialog yang muncul, pilih tipe klaster yang ingin Anda buat, klik Create, lalu konfigurasikan klaster di halaman Cluster Configurations.
Setelah konfigurasi selesai, klik Console-to-Code di pojok kanan atas halaman Confirm.
Di sidebar, klik tab Terraform. Parameter yang diperlukan untuk membuat klaster akan ditampilkan. Anda kemudian dapat menyalin dan menggunakan parameter tersebut.
Buat klaster khusus ACK menggunakan Terraform (Terway)
Bagian ini menjelaskan cara membuat klaster khusus ACK yang menggunakan komponen jaringan Terway.
Buat direktori kerja. Di direktori kerja tersebut, buat file konfigurasi bernama main.tf. Lalu, salin kode berikut ke file main.tf.
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"] } # Define the name or tag of the resource. variable "name" { default = "tf-example" } # Specify an existing vpc_id. If this is left empty, a new VPC is created. variable "vpc_id" { description = "Existing vpc id used to create several vswitches and other resources." default = "" } # When vpc_id is not specified, this defines the CIDR block for the new VPC. 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" } # Specify the IDs of existing vSwitches. variable "vswitch_ids" { description = "List of existing vswitch id." type = list(string) default = [] } # When vswitch_ids is not specified, this creates new vSwitches. You must specify three non-overlapping CIDR blocks. 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"] } # Specify existing vSwitch IDs for Terway. variable "terway_vswitch_ids" { description = "List of existing vswitch ids for terway." type = list(string) default = [] } # When terway_vswitch_ids is not specified, this specifies the CIDR blocks for the vSwitches used by Terway. 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"] } # Specify the add-ons to install in the ACK cluster. Declare the name and configuration for each add-on. 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)) # Get the instance types supported in each zone. instance_types_per_az = { for az, types in data.alicloud_instance_types.default : az => [for t in types.instance_types : t.id] } # Get the list of instance types in all listed zones. all_instance_types_in_zones = [for zone in local.common_zone_ids : local.instance_types_per_az[zone]] # Convert each list to a set. sets = [for s in local.all_instance_types_in_zones : toset(s)] # Calculate the instance types common to all zones. 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)] } # Query zones that support enhanced NAT gateways. data "alicloud_enhanced_nat_available_zones" "enhanced" { } # If the vpc_id variable is not provided, this resource creates a new VPC with the CIDR block specified by the vpc_cidr variable. resource "alicloud_vpc" "vpc" { count = var.vpc_id == "" ? 1 : 0 cidr_block = var.vpc_cidr } # If the vswitch_ids variable is not provided, new vSwitches are created based on the specified vswitch_cidrs by default. 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] } # If the terway_vswitch_ids variable is not provided, vSwitches for Terway are created based on the specified vswitch_cidrs by default. 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] } # Query the resource groups of the current Alibaba Cloud user. data "alicloud_resource_manager_resource_groups" "default" { status = "OK" } # Query Alibaba Cloud ECS instance types. 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" } # Create an ACK dedicated cluster. Configurations include control plane vSwitches, pod vSwitches, instance types, disks, password, and service CIDR block. 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)) # The vSwitch IDs for the control plane. 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)) # The vSwitch CIDR blocks for the pod network when using Terway. master_instance_types = [local.common_instance_types[0],local.common_instance_types[0],local.common_instance_types[0]] # The instance types for the control plane nodes. master_disk_category = "cloud_essd" # The system disk type for the control plane nodes. password = "Yourpassword1234" # The SSH logon password. service_cidr = "172.18.0.0/16" # The service CIDR block. load_balancer_spec = "slb.s1.small" # The Server Load Balancer specification. install_cloud_monitor = "true" # Install the CloudMonitor service. resource_group_id = data.alicloud_resource_manager_resource_groups.default.groups.0.id # The ID of the resource group to which the cluster belongs. This is used to isolate different resources. deletion_protection = "false" # Deletion protection for the cluster to prevent accidental deletion through the console or API. timezone = "Asia/Shanghai" # The time zone used by the cluster. os_type = "Linux" # The operating system platform type. platform = "AliyunLinux3" # The operating system distribution. cluster_domain = "cluster.local" # The local domain name of the cluster. proxy_mode = "ipvs" # The proxy mode of kube-proxy. custom_san = "www.terraform.io" # The custom certificate SAN. new_nat_gateway = "true" # Create a new NAT Gateway. dynamic "addons" { for_each = var.cluster_addons content { name = lookup(addons.value, "name", var.cluster_addons) config = lookup(addons.value, "config", var.cluster_addons) } } }Jalankan perintah berikut untuk menginisialisasi lingkungan runtime Terraform.
terraform initOutput berikut menunjukkan bahwa Terraform telah diinisialisasi.
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.Buat rencana eksekusi dan pratinjau perubahannya.
terraform planOutput berikut menunjukkan bahwa rencana eksekusi telah dibuat. Anda dapat melihat informasi resource.
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. ...Jalankan perintah berikut untuk membuat klaster khusus ACK.
terraform applyPada prompt, masukkan
yesdan tekan tombol Enter. Tunggu hingga perintah selesai. Output berikut menunjukkan bahwa klaster khusus 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 8m26s [id=************] Apply complete! Resources: 8 added, 0 changed, 0 destroyed.Hasil verifikasi
Jalankan perintah terraform show
Jalankan perintah berikut untuk menanyakan detail resource yang dibuat oleh Terraform.
terraform showMasuk ke Konsol ACK
Masuk ke Konsol Container Service for Kubernetes untuk melihat klaster yang telah dibuat.
Pembersihan resource
Saat Anda tidak lagi memerlukan resource yang dibuat atau dikelola oleh Terraform, jalankan perintah berikut untuk melepaskannya. Untuk informasi selengkapnya tentang terraform destroy, lihat Perintah umum.
Perintah terraform destroy akan menghapus semua resource yang dibuat. Lakukan dengan hati-hati.
terraform destroySelama proses pembersihan resource, masukkan yes pada prompt dan tekan tombol Enter. Tunggu hingga perintah selesai. Output berikut menunjukkan bahwa klaster khusus ACK telah dihapus.
...
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.Contoh lengkap
Referensi
Untuk menginstal komponen tambahan saat membuat klaster khusus ACK, lihat Kelola add-on menggunakan Terraform.
Untuk informasi selengkapnya tentang cara membuat kelompok node, lihat Buat kelompok node autoscaling menggunakan Terraform.
Terraform tersedia sebagai layanan terkelola di ROS. Anda dapat menerapkan template Terraform di Konsol ROS. Untuk informasi selengkapnya, lihat Buat tumpukan Terraform.