如何在Terraform中使用Amazon EFS和EKS [英] How to use Amazon EFS with EKS in Terraform

查看:0
本文介绍了如何在Terraform中使用Amazon EFS和EKS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我有两个目录:

aws/ k8s/

aws/内部是描述私有网络、网络、安全组、IAM角色、EKS集群、EKS节点组和几个EFS挂载的.tf文件。这些都是使用AWS提供程序,状态存储在S3中。

然后在k8s/中,我将使用Kubernetes提供程序并在我创建的EKS集群中创建Kubernetes资源。此状态存储在同一S3存储桶中的不同状态文件中。

我无法弄清楚如何将EFS装载作为永久卷装载到我的Pod。

我找到了描述使用EFS-Provisioner Pod来执行此操作的文档。请参见How do I use EFS with EKS?

在较新的EKS文档中,他们现在说要使用Amazon EFS CSI Driver。第一步是对以下文件执行kubectl apply

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
- ../../base
images:
- name: amazon/aws-efs-csi-driver
  newName: 602401143452.dkr.ecr.us-west-2.amazonaws.com/eks/aws-efs-csi-driver
  newTag: v0.2.0
- name: quay.io/k8scsi/livenessprobe
  newName: 602401143452.dkr.ecr.us-west-2.amazonaws.com/eks/csi-liveness-probe
  newTag: v1.1.0
- name: quay.io/k8scsi/csi-node-driver-registrar
  newName: 602401143452.dkr.ecr.us-west-2.amazonaws.com/eks/csi-node-driver-registrar
  newTag: v1.1.0

有人知道我在Terraform中会怎么做吗?或者通常如何将EFS文件共享作为PV装载到EKS群集?

推荐答案

@宝马做得对,我能够把这一切都整合到Terraform中。

aws/目录中,我创建了我的所有AWS资源、VPC、EK、Worker等和EFS挂载。

resource "aws_efs_file_system" "example" {
  creation_token = "${var.cluster-name}-example"

  tags = {
    Name = "${var.cluster-name}-example"
  }
}

resource "aws_efs_mount_target" "example" {
  count = 2
  file_system_id = aws_efs_file_system.example.id
  subnet_id = aws_subnet.this.*.id[count.index]
  security_groups = [aws_security_group.eks-cluster.id]
}

我还从AWS提供商计划导出EFS文件系统ID。

output "efs_example_fsid" {
  value = aws_efs_file_system.example.id
}

创建EKS群集后,我必须手动将EFS CSI驱动程序安装到群集中,然后才能继续。

然后在k8s/目录中引用aws/状态文件,以便可以在PV创建中使用EFS文件系统ID。

data "terraform_remote_state" "remote" {
  backend = "s3"
  config = {
    bucket = "example-s3-terraform"
    key    = "aws-provider.tfstate"
    region = "us-east-1"
  }
}

然后使用Kubernetes提供程序创建永久卷。

resource "kubernetes_persistent_volume" "example" {
  metadata {
    name = "example-efs-pv"
  }
  spec {
    storage_class_name = "efs-sc"
    persistent_volume_reclaim_policy = "Retain"
    capacity = {
      storage = "2Gi"
    }
    access_modes = ["ReadWriteMany"]
    persistent_volume_source {
      nfs {
        path = "/"
        server = data.terraform_remote_state.remote.outputs.efs_example_fsid
      }
    }
  }
}

这篇关于如何在Terraform中使用Amazon EFS和EKS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆