Terraform を使用して、複数のノードプールと必須アドオンを備えた ACK マネージドクラスター をプロビジョニングします。
このチュートリアルのサンプルコードは、OpenAPI Portal から直接実行できます。
前提条件
-
ACK が有効になっていること (Terraform を使用した ACK の有効化)。
-
ご利用の Alibaba Cloud アカウントにはすべてのリソースに対する完全な権限が必要です。Alibaba Cloud アカウントの認証情報が漏洩すると、重大なセキュリティリスクが発生する可能性があります。Resource Access Management (RAM) ユーザーを作成し、その RAM ユーザー用の AccessKey を作成することを推奨します。詳細については、「RAM ユーザーの作成」および「AccessKey の作成」をご参照ください。
-
Terraform コマンドを実行するために使用する RAM ユーザーに、以下の最小権限ポリシーをアタッチしてください。このポリシーにより、この例で使用するリソースを管理する権限が RAM ユーザーに付与されます。詳細については、「RAM ユーザー権限の管理」をご参照ください。
この権限ポリシーにより、RAM ユーザーは VPC、vSwitch、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": "*" } ] } -
Terraform 実行環境を準備します。Terraform は以下のいずれかの方法で使用できます。
-
Terraform Explorer での Terraform の使用:Alibaba Cloud は、インストール不要で使用できるオンライン Terraform 環境を提供しています。この方法は、コストをかけずに Terraform を迅速にテストおよびデバッグするのに最適です。
-
Terraform を使用したリソースの迅速な作成:Alibaba Cloud Shell には Terraform がプリインストール済みで、認証情報も設定されています。Cloud Shell で直接 Terraform コマンドを実行できます。この方法は、Terraform を迅速かつ低コストで使用するのに適しています。
-
Resource Orchestration Service (ROS) での Terraform の使用:ROS は Terraform のマネージドサービスを提供しています。Terraform ベースのテンプレートを作成して、Alibaba Cloud、AWS、または Azure 上のリソースを定義し、リソースパラメーターと依存関係を設定できます。
-
オンプレミスマシンへの Terraform のインストールと設定:この方法は、ネットワーク接続が不安定なシナリオやカスタム開発環境が必要な場合に適しています。
重要Terraform のバージョンが 0.12.28 以降であることを確認してください。バージョンを確認するには、
terraform --versionコマンドを実行します。 -
リソース
この例で使用される一部のリソースには料金が発生します。不要な課金を回避するため、不要になったらこれらのリソースを削除してください。
-
alicloud_zones:可用性ゾーンを照会します。
-
alicloud_instance_types:指定された条件を満たす ECS インスタンスタイプを照会します。
-
alicloud_vpc:VPC を作成します。
-
alicloud_vswitch:VPC をサブネットに分割するための 1 つ以上の vSwitch を作成します。
-
alicloud_cs_managed_kubernetes:ACK マネージドクラスターを作成します。
-
alicloud_cs_kubernetes_node_pool:ACK マネージドクラスター用のノードプールを作成します。
コンソールから Terraform 設定の生成
提供されている例を超えて構成をカスタマイズするには、ACK コンソールから Terraform 設定をエクスポートします。
ACK コンソール にログインします。左側のナビゲーションウィンドウで、クラスターリスト をクリックします。
クラスターリスト ページで、クラスターテンプレート をクリックします。
-
ダイアログボックスでクラスタータイプを選択し、作成する をクリックして、クラスター設定 ページでクラスターの詳細を設定します。
-
構成が完了したら、確認 ステップに進み、右上隅の 同機能のコード をクリックします。
-
サイドバーで Terraform タブをクリックして、自動生成された HCL コードを表示およびコピーします。
Terway を使用した ACK マネージドクラスター の作成
この例では、通常のノードプール、マネージドノードプール、および auto scaling ノードプールを備えた ACK マネージドクラスター を作成します。クラスターには、以下のデフォルトアドオンが含まれます:Terway (ネットワーク)、csi-plugin および csi-provisioner (ストレージ)、loongcollector (ロギング)、Nginx Ingress Controller、ack-arms-prometheus (モニタリング)、および ack-node-problem-detector (ノード診断)。
-
作業ディレクトリを作成し、そのディレクトリ内に main.tf という名前の設定ファイルを作成して、次のコードを main.tf にコピーします。
provider "alicloud" { region = var.region_id } variable "region_id" { type = string default = "cn-shenzhen" } variable "cluster_spec" { type = string description = "The specifications for the Kubernetes cluster. This can be empty. Valid values: ack.standard: Standard managed clusters; ack.pro.small: Professional managed clusters." default = "ack.pro.small" } # The availability zones for the vSwitches. variable "availability_zone" { description = "The availability zones for the vSwitches." default = ["cn-shenzhen-c", "cn-shenzhen-e", "cn-shenzhen-f"] } # A list of existing vSwitch IDs. variable "node_vswitch_ids" { description = "A list of existing vSwitch IDs for Terway nodes." type = list(string) default = [] } # A list of CIDR blocks for creating new vSwitches. variable "node_vswitch_cidrs" { description = "A list of CIDR blocks for creating new vSwitches when 'node_vswitch_ids' is not specified." type = list(string) default = ["172.16.0.0/23", "172.16.2.0/23", "172.16.4.0/23"] } # The configuration of the Terway network add-on. If empty, new Terway vSwitches are created based on terway_vswitch_cidrs by default. variable "terway_vswitch_ids" { description = "List of existing pod vswitch ids for terway." type = list(string) default = [] } # The CIDR blocks for creating vSwitches for Terway when terway_vswitch_ids is not specified. variable "terway_vswitch_cidrs" { description = "List of cidr blocks used to create several new vswitches when 'terway_vswitch_ids' is not specified." type = list(string) default = ["172.16.208.0/20", "172.16.224.0/20", "172.16.240.0/20"] } # The ECS instance types for worker nodes. variable "worker_instance_types" { description = "The ECS instance types for worker nodes." default = ["ecs.g6.2xlarge", "ecs.g6.xlarge"] } # The add-ons to install on the ACK cluster. Includes Terway (network), csi-plugin (storage), csi-provisioner (storage), loongcollector (logging), Nginx Ingress Controller, ack-arms-prometheus (monitoring), and ack-node-problem-detector (node diagnostics). 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" = "", } ] } # The name prefix for the ACK managed cluster. variable "k8s_name_prefix" { description = "The name prefix for the managed Kubernetes cluster." default = "tf-ack-shenzhen" } # Default resource names. 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}" } # ECS instance configuration for nodes. Queries for ECS instance types that meet the CPU and memory requirements. 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" } # Node vSwitches. 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) } # The vSwitches for pods. 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) } # ACK managed cluster. resource "alicloud_cs_managed_kubernetes" "default" { name = local.k8s_name_terway # The name of the Kubernetes cluster. cluster_spec = var.cluster_spec # Creates a professional managed cluster. vswitch_ids = split(",", join(",", alicloud_vswitch.vswitches.*.id)) # The vSwitches for the node pool. Specify one or more vSwitch IDs. The vSwitches must be in the specified availability zones. pod_vswitch_ids = split(",", join(",", alicloud_vswitch.terway_vswitches.*.id)) # Pod vSwitches. new_nat_gateway = true # Specifies whether to create a new nat gateway when you create the Kubernetes cluster. Default value: true. service_cidr = "10.11.0.0/16" # The Service CIDR block, required when using Flannel networking. It must not overlap with the VPC CIDR block or that of another Kubernetes cluster in the same VPC. This parameter cannot be changed after the cluster is created. The cluster supports a maximum of 256 nodes. slb_internet_enabled = true # Specifies whether to create an Internet-facing load balancer for the API server. Default value: false. enable_rrsa = true control_plane_log_components = ["apiserver", "kcm", "scheduler", "ccm"] # Control plane logs. dynamic "addons" { # Add-on configuration. for_each = var.cluster_addons content { name = lookup(addons.value, "name", var.cluster_addons) config = lookup(addons.value, "config", var.cluster_addons) } } } # Regular node pool. resource "alicloud_cs_kubernetes_node_pool" "default" { cluster_id = alicloud_cs_managed_kubernetes.default.id # The ID of the Kubernetes cluster. node_pool_name = local.nodepool_name # The name of the node pool. vswitch_ids = split(",", join(",", alicloud_vswitch.vswitches.*.id)) # The vSwitches for the node pool. Specify one or more vSwitch IDs. The vSwitches must be in the specified availability zones. instance_types = var.worker_instance_types instance_charge_type = "PostPaid" desired_size = 2 # The desired number of nodes in the node pool. install_cloud_monitor = true # Specifies whether to install CloudMonitor on the nodes. system_disk_category = "cloud_efficiency" system_disk_size = 100 image_type = "AliyunLinux" data_disks { # Data disk configuration for the node. category = "cloud_essd" # The category of the data disk. size = 120 # The size of the data disk. } } # Create a managed node pool. resource "alicloud_cs_kubernetes_node_pool" "managed_node_pool" { cluster_id = alicloud_cs_managed_kubernetes.default.id # The ID of the Kubernetes cluster. node_pool_name = local.managed_nodepool_name # The name of the node pool. vswitch_ids = split(",", join(",", alicloud_vswitch.vswitches.*.id)) # The vSwitches for the node pool. Specify one or more vSwitch IDs. The vSwitches must be in the specified availability zones. desired_size = 0 # The desired number of nodes in the node pool. 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 } } # Create an auto-scaling node pool. The node pool can be scaled out to a maximum of 10 nodes and must have at least 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 # Specifies whether to install CloudMonitor on the nodes. system_disk_category = "cloud_efficiency" system_disk_size = 100 image_type = "AliyunLinux3" data_disks { # Data disk configuration for the node. category = "cloud_essd" # The category of the data disk. size = 120 # The size of the data disk. } } -
次のコマンドを実行して、Terraform 実行環境を初期化します。
terraform init期待される出力:
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. -
実行計画を作成し、変更内容をプレビューします。
terraform plan -
次のコマンドを実行してクラスターを作成します。
terraform applyプロンプトが表示されたら、
yesと入力して Enter キーを押します。次の出力が表示されると、ACK クラスターが作成されたことを示します。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. -
結果の確認
Terraform show
次のコマンドを実行して、Terraform によって作成されたリソースの詳細を表示します。
terraform showACK コンソール
ACK コンソール にログインして、作成されたクラスターを確認します。
リソースのクリーンアップ
リソースを解放して課金を停止するには、terraform destroy コマンドを実行します。terraform destroy コマンドの詳細については、「一般的なコマンド」をご参照ください。
terraform destroy