tencent cloud

容器服务

动态与公告
产品动态
公告
产品发布记录
产品简介
产品概述
产品优势
产品架构
应用场景
产品功能
基本概念
原生 Kubernetes 名词对照
容器服务高危操作
地域和可用区
开源组件
购买指南
购买指引
购买 TKE 标准集群
购买原生节点
购买超级节点
快速入门
新手指引
快速创建一个标准集群
入门示例
容器应用部署 Check List
集群配置
标准集群概述
集群管理
网络管理
存储管理
节点管理
GPU 资源管理
远程终端
应用配置
工作负载管理
服务和配置管理
组件和应用管理
弹性伸缩
容器登录方式
可观测配置
运维可观测性
成本洞察和优化
调度配置
调度组件概述
资源利用率优化调度
业务优先级保障调度
Qos 感知调度
安全和稳定性
容器服务安全组设置
身份验证和授权
应用安全
多集群管理
计划升级
备份中心
云原生服务指南
云原生 etcd
Prometheus 监控服务
TKE Serverless 集群指南
TKE 注册集群指南
实践教程
集群
Serverless 集群
调度
安全
服务部署
网络
发布
日志
监控
运维
Terraform
DevOps
弹性伸缩
容器化
微服务
成本管理
混合云
AI
故障处理
节点磁盘爆满排障处理
节点高负载排障处理
节点内存碎片化排障处理
集群 DNS 解析异常排障处理
集群 Kube-Proxy 异常排障处理
集群 API Server 网络无法访问排障处理
Service&Ingress 网络无法访问排障处理
Service&Ingress 常见报错和处理
Nginx Ingress 偶现 Connection Refused
CLB Ingress 创建报错排障处理
Pod 网络无法访问排查处理
Pod 状态异常与处理措施
授权腾讯云售后运维排障
CLB 回环问题
API 文档
History
Introduction
API Category
Making API Requests
Elastic Cluster APIs
Resource Reserved Coupon APIs
Cluster APIs
Third-party Node APIs
Relevant APIs for Addon
Network APIs
Node APIs
Node Pool APIs
TKE Edge Cluster APIs
Cloud Native Monitoring APIs
Scaling group APIs
Super Node APIs
Other APIs
Data Types
Error Codes
TKE API 2022-05-01
常见问题
TKE 标准集群
TKE Serverless 集群
运维类
隐患处理
服务类
镜像仓库类
远程终端类
事件类
资源管理类
服务协议
TKE Service Level Agreement
TKE Serverless Service Level Agreement
联系我们
词汇表
文档容器服务实践教程 Terraform使用 Terraform 管理 TKE 集群和节点池

使用 Terraform 管理 TKE 集群和节点池

PDF
聚焦模式
字号
最后更新时间: 2023-09-05 09:38:03

安装 Terraform

前往 Terraform 官网,使用命令行直接安装 Terraform 或下载二进制安装文件。

认证和鉴权

获取凭证

在首次使用 Terraform 之前,请前往 云 API 密钥页面 申请安全凭证 SecretId 和 SecretKey。若已有可使用的安全凭证,则跳过该步骤。
1. 登录 访问管理控制台,在左侧导航栏,选择访问密钥 > API 密钥管理
2. 在 API 密钥管理页面,单击新建密钥,即可以创建一对 SecretId/SecretKey。

鉴权

方式1:(推荐)使用环境变量注入账号的访问密钥

请将如下信息添加至环境变量配置:
export TENCENTCLOUD_SECRET_ID="xxx" # 替换为账号访问密钥的SecretId
export TENCENTCLOUD_SECRET_KEY="xxx" # 替换为账号访问密钥的SecretKey

方式2:在 Terraform 配置文件的 provider 代码块中填写账号的访问密钥

在用户目录下创建 provider.tf 文件,输入如下内容:
注意
使用此方式请务必注意配置文件中密钥的安全性。
provider "tencentcloud" {
secret_id = "xxx" # 替换为账号访问密钥的SecretId
secret_key = "xxx" # 替换为账号访问密钥的SecretKey
}

使用 Terraform 创建 TKE 集群

1. 创建一个工作目录,并在工作目录中创建名为 main.tf 的 Terraform 配置文件。
说明
main.tf 文件描述的是以下 Terraform 配置:
创建一个新的 VPC,并创建一个该 VPC 下的 Subnet 子网。
创建一个 TKE 托管集群。
在该 TKE 集群下创建一个节点池。
main.tf 文件内容如下:
# 标识使用腾讯云的Terraform Provider
terraform {
required_providers {
tencentcloud = {
source = "tencentcloudstack/tencentcloud"
}
}
}

# 定义本地变量,实际使用时按需修改下列变量实际值。后面各代码块中会引用下列变量的值。
locals {
region = "xxx" # 使用的地域,如ap-beijing,即北京
zone1 = "xxx" # 地域下的一个可用区,如ap-beijing-1,即北京一区
vpc_name = "xxx" # 设置VPC的名字,如tke-tf-demo
vpc_cidr_block = "xxx" # VPC的CIDR设置,如10.0.0.0/16
subnet1_name = "xxx" # 子网1的名字,如tke-tf-demo-sub1
subnet1_cidr_block = "xxx" # 子网1的CIDR设置,如10.0.1.0/24
cluster_name = "xxx" # TKE集群的name,如tke-tf-demo-cluster
network_type = "xxx" # TKE托管集群的网络模式,如GR,表示使用Global Route
cluster_cidr = "xxx" # 集群的容器网络,不能与网络冲突,如172.26.0.0/20
cluster_version = "xxx" # TKE集群的Kubernetes版本,如1.22.5
}


# 腾讯云provider的基本配置
provider "tencentcloud" {
# 如果使用配置文件中写入密钥的方式,在此处写入SecretId和SecretKey。但更推荐使用环境变量注入的方式。
# secret_id = "xxx"
# secret_key = "xxx"
region = local.region
}

# 声明VPC资源
resource "tencentcloud_vpc" "vpc_example" {
name = local.vpc_name
cidr_block = local.vpc_cidr_block
}

# 声明子网资源
resource "tencentcloud_subnet" "subnet_example" {
availability_zone = local.zone1
cidr_block = local.subnet1_cidr_block
name = local.subnet1_name
vpc_id = tencentcloud_vpc.vpc_example.id # 指定子网资源所属VPC为前面创建的
}

# 声明TKE集群资源,将创建网络为Global Route的集群
resource "tencentcloud_kubernetes_cluster" "managed_cluster_example" {
vpc_id = tencentcloud_vpc.vpc_example.id # 引用前面创建获得的VPC Id
cluster_name = local.cluster_name
network_type = local.network_type
cluster_cidr = local.cluster_cidr
cluster_version = local.cluster_version
}


# 如果需要创建VPC-CNI模式的集群,可以用下面的声明
# resource "tencentcloud_kubernetes_cluster" "managed_cluster_example" {
# vpc_id = tencentcloud_vpc.vpc_example.id # 引用前面创建获得的VPC Id
# cluster_name = local.cluster_name
# network_type = "VPC-CNI"
# eni_subnet_ids = [tencentcloud_subnet.subnet_example.id]
# service_cidr = "172.16.0.0/24"
# cluster_version = local.cluster_version
# }
2. (可选)若您首次使用腾讯云容器服务,您需要为当前服务角色授权,赋予容器服务操作权限后才能正常地访问您的其他云服务资源。如果您已完成过授权,请直接跳过此步骤。
您可以在首次登录 容器服务控制台 时,对当前账号授予腾讯云容器服务操作云服务器 CVM、负载均衡 CLB、云硬盘 CBS 等云资源的权限。详情请参见 服务授权
您也可以在 Terraform 配置文件中完成授权。您需要在工作目录下新建 cam.tf 文件,文件内容如下:
########################### 请按需在Terraform配置文件中添加声明配置,已在控制台完成授权的角色,则不需要再添加 #############

# 创建服务预设角色TKE_QCSRole
resource "tencentcloud_cam_role" "TKE_QCSRole" {
name = "TKE_QCSRole"
document = <<EOF
{
"statement": [
{
"action":"name/sts:AssumeRole",
"effect":"allow",
"principal":{
"service":"ccs.qcloud.com"
}
}
],
"version":"2.0"
}
EOF
description = "当前角色为 腾讯云容器服务 服务角色,该角色将在已关联策略的权限范围内访问您的其他云服务资源。"
}

# 预设策略 QcloudAccessForTKERole
data "tencentcloud_cam_policies" "qca" {
name = "QcloudAccessForTKERole"
}

# 预设策略 QcloudAccessForTKERoleInOpsManagement
data "tencentcloud_cam_policies" "ops_mgr" {
name = "QcloudAccessForTKERoleInOpsManagement"
}

# 角色TKE_QCSRole关联QcloudAccessForTKERole策略
resource "tencentcloud_cam_role_policy_attachment" "QCS_QCA" {
role_id = lookup(tencentcloud_cam_role.TKE_QCSRole, "id")
policy_id = data.tencentcloud_cam_policies.qca.policy_list.0.policy_id
}

# 角色TKE_QCSRole关联策略QcloudAccessForTKERoleInOpsManagement
resource "tencentcloud_cam_role_policy_attachment" "QCS_OpsMgr" {
role_id = lookup(tencentcloud_cam_role.TKE_QCSRole, "id")
policy_id = data.tencentcloud_cam_policies.ops_mgr.policy_list.0.policy_id
}

########################### 上述声明完成TKE_QCSRole角色的创建和授权 ###########################
########################### 下列声明完成IPAMDofTKE_QCSRole角色的创建和授权 ####################


# 创建服务预设角色IPAMDofTKE_QCSRole
resource "tencentcloud_cam_role" "IPAMDofTKE_QCSRole" {
name = "IPAMDofTKE_QCSRole"
document = <<EOF
{
"statement": [
{
"action":"name/sts:AssumeRole",
"effect":"allow",
"principal":{
"service":"ccs.qcloud.com"
}
}
],
"version":"2.0"
}
EOF
description = "当前角色为 容器服务IPAMD支持 服务角色,该角色将在已关联策略的权限范围内访问您的其他云服务资源。"
}

# 预设策略 QcloudAccessForIPAMDofTKERole
data "tencentcloud_cam_policies" "qcs_ipamd" {
name = "QcloudAccessForIPAMDofTKERole"
}

# 角色IPAMDofTKE_QCSRole关联策略QcloudAccessForIPAMDofTKERole
resource "tencentcloud_cam_role_policy_attachment" "QCS_Ipamd" {
role_id = lookup(tencentcloud_cam_role.IPAMDofTKE_QCSRole, "id")
policy_id = data.tencentcloud_cam_policies.qcs_ipamd.policy_list.0.policy_id
}
########################### 上述声明完成IPAMDofTKE_QCSRole角色的创建和授权 ###########################
########################### 下列声明完成TKE_QCSLinkedRoleInEKSLog角色的创建和授权 ####################
# 创建服务预设角色TKE_QCSLinkedRoleInEKSLog,如需开启日志采集使用。
resource "tencentcloud_cam_service_linked_role" "service_linked_role" {
qcs_service_name = ["cvm.qcloud.com", "ekslog.tke.cloud.tencent.com"]
description = "tke log role created by terraform"
tags = {
"createdBy" = "terraform"
}
}
3. 执行以下命令,初始化 Terraform 的运行环境。
terraform init
返回信息如下所示:
Initializing the backend...

Initializing provider plugins...
- Finding tencentcloudstack/tencentcloud versions matching "~> 1.78.13"...
- Installing tencentcloudstack/tencentcloud v1.78.13...
...

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.

...
4. 执行以下命令,查看 Terraform 根据配置文件生成的资源规划。
terraform plan
返回信息如下所示:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:
...

Plan: 3 to add, 0 to change, 0 to destroy.
...
5. 执行以下命令,创建资源。
terraform apply
返回信息如下所示:
...
Plan: 3 to add, 0 to change, 0 to destroy.

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 创建资源,返回信息如下所示:
...
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
至此,上述步骤完成了 VPC、子网、TKE 托管集群的创建。您可以在腾讯云控制台查看创建的资源。

使用 Terraform 创建 TKE 节点池

1. 创建一个工作目录,并在工作目录中创建名为 nodepool.tf 的 Terraform 配置文件。 nodepool.tf 文件内容如下:
# 定义本地变量,实际使用时按需修改下列变量实际值。后面各代码块中会引用下列变量的值。
# 实际使用时您也可以通过引用Terraform相关resource实例(如集群tencentcloud_kubernetes_cluster)的方式获取需要的值。
locals {
node_pool_name = "xxx" # 节点池名称,如tke-tf-demo-node-pool
max_node_size = xxx # 节点池最大节点数量
min_node_size = xxx # 节点池最小节点数量
cvm_instance_type = "xxx" # 节点池CVM机型,可选值参考https://cloud.tencent.com/document/api/213/15749获取
cvm_pass_word = "xxx" # 节点池CVM机器登录密码,请注意密码安全性,8-16位
security_group_ids = ["sg-xxx", "sg-xxx"] # 节点池关联的安全组Id数组
}

# 声明TKE节点池资源
resource "tencentcloud_kubernetes_node_pool" "example_node_pool" {
cluster_id = tencentcloud_kubernetes_cluster.managed_cluster_example.id # 节点池关联前面创建的集群
delete_keep_instance = false # 设置为false,表明删除节点池时删除关联的CVM实例
max_size = local.max_node_size
min_size = local.min_node_size
name = local.node_pool_name
vpc_id = tencentcloud_vpc.vpc_example.id
subnet_ids = [tencentcloud_subnet.subnet_example.id] # 节点池关联的子网Id数组
auto_scaling_config {
instance_type = local.cvm_instance_type
# key_ids = ["xxx"] # 设置节点池CVM机器登录密钥
password = local.cvm_pass_word # 设置节点池CVM机器登录密码,请注意密码安全性
security_group_ids = local.security_group_ids
}
}
2. 执行以下命令,查看 Terraform 根据配置文件生成的资源规划。
terraform plan
返回信息如下所示:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:
...

Plan: 1 to add, 0 to change, 0 to destroy.
...
3. 执行以下命令,创建资源。
terraform apply
返回信息如下所示:
...
Plan: 1 to add, 0 to change, 0 to destroy.

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 创建资源,返回信息如下所示:
...
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
至此,上述步骤完成了节点池的创建。您可以在腾讯云控制台查看创建的资源。

使用 Terraform 清理资源

如果您需要删除已创建的 VPC、子网、TKE 托管集群资源,可以执行以下命令。
terraform destroy
返回信息如下所示:
...
Plan: 0 to add, 0 to change, 3 to destroy.

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: 3 destroyed.

相关文档

帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈