You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Terraform设置AWS EKS节点根卷使用gp3类型

实现方案

EKS托管节点组的EC2根卷类型无法通过aws_eks_node_group资源的原生参数直接指定,你之前配置的kubernetes_storage_class仅作用于Kubernetes层面为工作负载动态创建的持久卷,和节点自身的系统盘没有关系,自然不会生效。
要让节点根卷默认使用gp3类型,核心思路是通过自定义启动模板定义磁盘配置,再将节点组绑定到该启动模板,具体操作如下:

  • 首先创建适配EKS节点的自定义启动模板,在块存储映射中明确指定根卷为gp3类型
# 拉取当前EKS版本匹配的官方优化AMI
data "aws_ami" "eks_node" {
  most_recent = true
  owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["amazon-eks-node-${var.k8s_version}-v*"]
    # 如果使用Graviton/ARM实例,把上面的values替换为["amazon-eks-arm64-node-${var.k8s_version}-v*"]
  }
}

resource "aws_launch_template" "eks_node" {
  name_prefix   = "${var.name}-eks-node-"
  image_id      = data.aws_ami.eks_node.id
  instance_type = random_id.node_group.keepers.node_type
  iam_instance_profile {
    name = aws_iam_instance_profile.node.name # 替换为你原有节点使用的实例配置文件名
  }

  # 核心配置:指定根卷为gp3
  block_device_mappings {
    device_name = "/dev/xvda" # 官方EKS优化AMI默认根卷设备名,自定义AMI需要对应调整
    ebs {
      volume_size = random_id.node_group.keepers.node_disk
      volume_type = "gp3"
      iops        = 3000 # gp3默认3000IOPS,该阈值内不额外收费
      throughput  = 125  # gp3默认125MB/s吞吐,可根据实际负载调整
      encrypted   = true
    }
  }

  metadata_options {
    http_endpoint               = "enabled"
    http_tokens                 = "required"
    http_put_response_hop_limit = 2 # 该值必须≥2,否则节点无法正常加入集群
  }

  # 节点初始化脚本,和托管节点组默认初始化逻辑一致
  user_data = base64encode(join("\n", [
    "#!/bin/bash",
    "set -ex",
    "/etc/eks/bootstrap.sh ${aws_eks_cluster.cluster.name} --b64-cluster-ca ${aws_eks_cluster.cluster.certificate_authority[0].data} --apiserver-endpoint ${aws_eks_cluster.cluster.endpoint}"
  ]))

  tag_specifications {
    resource_type = "instance"
    tags = {
      Name = "${var.name}-eks-node"
    }
  }
}
  • 修改原有aws_eks_node_group资源,绑定上述启动模板,移除冲突参数
    注意:绑定自定义启动模板后,原资源中直接配置的disk_size、instance_types参数会和启动模板配置冲突,需要删除,调整后的配置如下:
resource "aws_eks_node_group" "cluster" {
  count           = length(local.availability_zones)
  capacity_type   = var.node_capacity_type
  cluster_name    = aws_eks_cluster.cluster.name
  node_group_name = "${var.name}-${local.availability_zones[count.index]}-${random_id.node_group.hex}"
  node_role_arn   = random_id.node_group.keepers.role_arn
  subnet_ids      = [var.private ? aws_subnet.private[count.index].id : aws_subnet.public[count.index].id]
  version         = var.k8s_version

  # 关联自定义启动模板
  launch_template {
    id      = aws_launch_template.eks_node.id
    version = aws_launch_template.eks_node.latest_version
  }

  # 原有节点标签、污点等配置可以正常保留,不受启动模板影响
}

配置应用后,EKS会自动触发节点组滚动更新,按默认策略逐台替换旧节点,新创建的节点将默认使用gp3类型根卷,整个过程不会中断集群业务。如果需要调整滚动更新速度,可以在节点组中配置update_config参数调整最大不可用节点比例。

内容的提问来源于stack exchange,提问作者twsouza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 17:45:36