利用Terraform 部署 Kubernetes集群最佳实践
1. install Terraform
Download the Terraform at ( https://www.terraform.io/downloads.html?spm=a2c4g.11186623.2.4.21DyQW), you can choose the right version and platform. This document takes Terraform installed on Linux as an example. Under the /root/terraform path:
- wget https://releases.hashicorp.com/terraform/0.11.6/terraform_0.11.6_linux_amd64.zip
- unzip the file, you will get a binary file terraform.
- create the following entries under /etc/profile, add the path /root/terraform of binary files to PATH environment variables.
2. install Terraform-provider of aliyun
official download address: https://github.com/alibaba/terraform-provider/releases?spm=a2c4g.11186623.2.5.qK1FGC
Under the /root/terraform path:
1.
wget https://github.com/alibaba/terraform-provider/releases/download/V1.9.1/terraform-provider-alicloud_linux-amd64.tgz
2.
tar -zxvf terraform-provider-alicloud_linux-amd64.tgz
You will get a bin folder, and there is a terraform-provider-alicloud file in the folder.
3. Create .terraformrc file in the /root/terraform directory.
4. Add the following to the file.
5. Run the following command to detect the operation of the Terraform. If installed successfully, you will see
3. Deploying Kubernetes cluster
main.tf (The resources that will be deployed are defined.)
region:
provider "alicloud" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
}
zone:
data "alicloud_zones" "default" {
"available_instance_type" = "${data.alicloud_instance_types.instance_type.instance_types.0.id}"
"available_disk_category" = "${var.disk_category}"
}
security group:
resource "alicloud_security_group" "group" {
name = "${var.short_name}"
description = "New security group"
vpc_id = "xxxxxx"
}
Kubernetes cluster:
resource "alicloud_cs_kubernetes" "main" {
name_prefix = xxxxx
availability_zone = "${data.alicloud_zones.default.zones.0.id}"
new_nat_gateway = true
master_instance_type = "ecs.n4.small"
worker_instance_type = "ecs.n4.small"
worker_number = 10
password = xxxxxx
pod_cidr = xxxxx
service_cidr = xxxx
enable_ssh = true
install_cloud_monitor = true
vswitch_id = xxxxxx
}
4. run Terraform
Under root/terraform path:
terraform init
terraform plan
terraform apply
5. view the cluster was created
You can now view the cluster created by the terraform at the container service console.