本文适合不喜欢在浏览器中点点点,并刚刚入门或者想要入门 terraform 的小伙伴。
只需要下载 terraform,并将其提供的可执行文件放到系统路径。
各个云厂商都有相应的 SDK,以 provider 的形式提供给 terraform,腾讯云的 provider api 参考如下地址:
https://registry.terraform.io/providers/tencentcloudstack/tencentcloud/latest/docs
需要编写类似如下配置:
terraform {
required_providers {
tencentcloud = {
source = "tencentcloudstack/tencentcloud"
}
}
}
provider "tencentcloud" {
secret_id = var.SECRET_ID
secret_key = var.SECRET_KEY
region = "ap-shanghai"
}
terraform init
后,系统会去下载腾讯云的相关 SDK。首先通过腾讯云的 CAM 可以生产密钥,请到这个地址:https://console.cloud.tencent.com/cam/capi
在实际的使用中,我们不应该将 secret_id 和 secret_key 这些敏感信息写入到 tf 文件中,下面的例子将这个信息写入 环境变量。
export TF_VAR_SECRET_ID=xxxx
export TF_VAR_SECRET_KEY=xxxx
TF_VAR_
作为前缀。腾讯云的 SDK 中直接支持如下变量,可以直接使用。
export TENCENTCLOUD_SECRET_ID="my-secret-id"
export TENCENTCLOUD_SECRET_KEY="my-secret-key"
export TENCENTCLOUD_REGION="ap-guangzhou"
export TENCENTCLOUD_ASSUME_ROLE_ARN="my-role-arn"
export TENCENTCLOUD_ASSUME_ROLE_SESSION_NAME="my-session-name"
export TENCENTCLOUD_ASSUME_ROLE_SESSION_DURATION=3600
现在 provider 中可以删除密钥了。
provider "tencentcloud" {
region = "ap-beijing"
}
创建安全组,并指定出入规则。
resource "tencentcloud_security_group" "sg01" {
name = "some-sg"
description = "security group @ powered by terraform"
}
resource "tencentcloud_security_group_lite_rule" "sg01-rule" {
security_group_id = tencentcloud_security_group.sg01.id
ingress = [
"ACCEPT#0.0.0.0/0#ALL#ICMP",
"ACCEPT#0.0.0.0/0#22#TCP",
"ACCEPT#0.0.0.0/0#30000-32768#TCP",
"ACCEPT#0.0.0.0/0#30000-32768#UDP",
]
egress = [
"ACCEPT#0.0.0.0/0#ALL#ALL",
]
}
基础的 provider 和 region 设置好之后,可以创建 vpc 和子网了。
# 查询当前可用区
data "tencentcloud_availability_zones" "all_zones" {
}
# 定义一个 VPC 网络
resource "tencentcloud_vpc" "vpc01" {
name = "some-01"
cidr_block = "10.0.0.0/16"
is_multicast = false
}
# 定义子网,这里会给每个 zone 定义一个子网
resource "tencentcloud_subnet" "subset01" {
count = length(data.tencentcloud_availability_zones.all_zones.zones)
name = "some-subset-${count.index}"
vpc_id = tencentcloud_vpc.vpc01.id
availability_zone = data.tencentcloud_availability_zones.all_zones.zones[count.index].name
cidr_block = "10.0.${count.index}.0/24"
is_multicast = false
}
count
和 count.index
创建一个 k8s 集群需要的参数很多,直接上代码:
# 创建 TKE 集群
resource "tencentcloud_kubernetes_cluster" "tke_managed" {
vpc_id = tencentcloud_vpc.vpc01.id
cluster_version = "1.18.4"
cluster_cidr = "172.16.0.0/16"
cluster_max_pod_num = 64
cluster_name = "some-tke-01"
cluster_desc = "created by terraform"
cluster_max_service_num = 2048
cluster_internet = true
managed_cluster_internet_security_policies = ["0.0.0.0/0"]
cluster_deploy_type = "MANAGED_CLUSTER"
cluster_os = "tlinux2.4x86_64"
container_runtime = "containerd"
deletion_protection = false
worker_config {
instance_name = "some-node"
availability_zone = data.tencentcloud_availability_zones.all_zones.zones[0].name
instance_type = "S2.MEDIUM4"
system_disk_type = "CLOUD_SSD"
system_disk_size = 50
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
internet_max_bandwidth_out = 1
public_ip_assigned = true
subnet_id = tencentcloud_subnet.subset01[0].id
security_group_ids = [tencentcloud_security_group.sg01.id]
enhanced_security_service = false
enhanced_monitor_service = false
password = "youPassword_@"
}
}
TKE 建议使用节点池增加或者减少节点,并支持弹性伸缩。
# 创建一个节点池
resource "tencentcloud_kubernetes_node_pool" "node-pool" {
name = "some-pool"
cluster_id = tencentcloud_kubernetes_cluster.tke_managed.id
max_size = 10
min_size = 0
vpc_id = tencentcloud_vpc.vpc01.id
subnet_ids = [for s in tencentcloud_subnet.subset01 : s.id]
retry_policy = "INCREMENTAL_INTERVALS"
desired_capacity = 1
enable_auto_scale = true
delete_keep_instance = false
node_os = "tlinux2.4x86_64"
auto_scaling_config {
instance_type = "S2.MEDIUM4"
system_disk_type = "CLOUD_PREMIUM"
system_disk_size = "50"
security_group_ids = [tencentcloud_security_group.sg01.id]
data_disk {
disk_type = "CLOUD_PREMIUM"
disk_size = 50
}
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
internet_max_bandwidth_out = 10
public_ip_assigned = true
password = "youPassword_@"
enhanced_security_service = false
enhanced_monitor_service = false
}
}
创建完集群之后,我们希望能直接保存 kubeconfig 文件。这里可以增加一个 output,如下:
output "KUBECONFIG" {
value = tencentcloud_kubernetes_cluster.tke_managed.kube_config
}
上述代码可以都写入 main.tf 文件中。
terraform init
terraform apply -auto-approve
terraform output -raw KUBECONFIG > kube.config
terraform destroy --auto-approve
# main.tf
# 名字标识
variable "name" {
default = "wellxie"
}
# 机型
variable "region" {
default = "ap-bangkok"
}
# k8s 版本
variable "k8s_ver" {
default = "1.18.4"
}
# pod ip 地址段
variable "pod_ip_seg" {
default = "172.16"
}
# vpc ip 地址段
variable "vpc_ip_seg" {
default = "10.0"
}
# 机型
variable "default_instance_type" {
default = "S2.MEDIUM4"
}
# node 密码
variable "node_password" {
default = "P@ssw@rd123"
}
terraform {
required_providers {
tencentcloud = {
source = "tencentcloudstack/tencentcloud"
}
}
}
# 指定腾讯云和其大区
provider "tencentcloud" {
region = var.region
}
# 定义安全组
resource "tencentcloud_security_group" "sg01" {
name = "${var.name}-sg"
description = "${var.name} security group @ powered by terraform"
}
resource "tencentcloud_security_group_lite_rule" "sg01-rule" {
security_group_id = tencentcloud_security_group.sg01.id
ingress = [
"ACCEPT#0.0.0.0/0#ALL#ICMP",
"ACCEPT#0.0.0.0/0#22#TCP",
"ACCEPT#0.0.0.0/0#30000-32768#TCP",
"ACCEPT#0.0.0.0/0#30000-32768#UDP",
]
egress = [
"ACCEPT#0.0.0.0/0#ALL#ALL",
]
}
# 查询当前可用区, 将设置到节点池
data "tencentcloud_availability_zones" "all_zones" {
}
# 定义一个 VPC 网络
resource "tencentcloud_vpc" "vpc01" {
name = "${var.name}-01"
cidr_block = "${var.vpc_ip_seg}.0.0/16"
is_multicast = false
tags = {
"user" = var.name
}
}
# 定义子网,这里会给每个 zone 定义一个子网
resource "tencentcloud_subnet" "subset01" {
count = length(data.tencentcloud_availability_zones.all_zones.zones)
name = "${var.name}-subset-${count.index}"
vpc_id = tencentcloud_vpc.vpc01.id
availability_zone = data.tencentcloud_availability_zones.all_zones.zones[count.index].name
cidr_block = "${var.vpc_ip_seg}.${count.index}.0/24"
is_multicast = false
tags = {
"user" = var.name
}
}
# 创建 TKE 集群
resource "tencentcloud_kubernetes_cluster" "tke_managed" {
vpc_id = tencentcloud_vpc.vpc01.id
cluster_version = var.k8s_ver
cluster_cidr = "${var.pod_ip_seg}.0.0/16"
cluster_max_pod_num = 64
cluster_name = "${var.name}-tke-01"
cluster_desc = "created by terraform"
cluster_max_service_num = 2048
cluster_internet = true
managed_cluster_internet_security_policies = ["0.0.0.0/0"]
cluster_deploy_type = "MANAGED_CLUSTER"
cluster_os = "tlinux2.4x86_64"
container_runtime = "containerd"
deletion_protection = false
worker_config {
instance_name = "${var.name}-node"
availability_zone = data.tencentcloud_availability_zones.all_zones.zones[0].name
instance_type = var.default_instance_type
system_disk_type = "CLOUD_SSD"
system_disk_size = 50
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
internet_max_bandwidth_out = 1
public_ip_assigned = true
subnet_id = tencentcloud_subnet.subset01[0].id
security_group_ids = [tencentcloud_security_group.sg01.id]
enhanced_security_service = false
enhanced_monitor_service = false
password = var.node_password
}
labels = {
"user" = var.name
}
}
# 创建一个节点池
resource "tencentcloud_kubernetes_node_pool" "node-pool" {
name = "${var.name}-pool"
cluster_id = tencentcloud_kubernetes_cluster.tke_managed.id
max_size = 10
min_size = 0
vpc_id = tencentcloud_vpc.vpc01.id
subnet_ids = [for s in tencentcloud_subnet.subset01 : s.id]
retry_policy = "INCREMENTAL_INTERVALS"
desired_capacity = 0
enable_auto_scale = true
delete_keep_instance = false
node_os = "tlinux2.4x86_64"
auto_scaling_config {
instance_type = var.default_instance_type
system_disk_type = "CLOUD_PREMIUM"
system_disk_size = "50"
security_group_ids = [tencentcloud_security_group.sg01.id]
data_disk {
disk_type = "CLOUD_PREMIUM"
disk_size = 50
}
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
internet_max_bandwidth_out = 10
public_ip_assigned = true
password = var.node_password
enhanced_security_service = false
enhanced_monitor_service = false
}
labels = {
"user" = var.name,
}
}
output "KUBECONFIG" {
description = "下面的配置是 kubeconfig,请拷贝并妥善存储"
value = tencentcloud_kubernetes_cluster.tke_managed.kube_config
}
TENCENTCLOUD_SECRET_ID
和 TENCENTCLOUD_SECRET_KEY
腾讯云提供产品化的 terraform,产品名称是 TIC,可以参考如下链接:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。