求使用Terraform搭建GKE Autopilot的最简可用配置示例
GKE Autopilot 最简Terraform部署配置
前置要求
- 本地已安装Terraform、gcloud CLI工具
- 已通过
gcloud auth application-default login完成GCP身份认证 - 已在对应GCP项目中启用Kubernetes Engine API
- 替换配置中占位参数为你自己的实际值
配置代码
直接创建main.tf文件,填入以下内容即可,没有多余的VPC、节点池等非必要配置:
terraform { required_providers { google = { source = "hashicorp/google" version = "~> 4.70" } } } provider "google" { project = "替换为你的GCP项目ID" region = "替换为集群部署区域,示例:us-central1" } # GKE Autopilot集群资源 resource "google_container_cluster" "basic_autopilot" { name = "替换为你的集群名称" location = "替换为集群部署区域,和provider的region保持一致" # 开启Autopilot模式,所有底层节点、网络基础配置自动托管 enable_autopilot = true # 无其他额外配置即可满足基础运行要求,Autopilot默认会使用当前项目的默认VPC }
部署步骤
- 在
main.tf所在目录执行terraform init初始化Google提供商插件 - 执行
terraform plan预览即将创建的资源,确认没有报错 - 执行
terraform apply输入yes确认部署,等待5-10分钟即可完成集群创建 - 集群创建完成后执行
gcloud container clusters get-credentials 你的集群名称 --region 集群区域 --project 你的项目ID即可获取集群访问凭证,直接用kubectl操作集群
内容的提问来源于stack exchange,提问作者tmp dev
相关产品推荐
相关产品推荐

