【问题标题】:How to use Amazon EFS with EKS in Terraform如何在 Terraform 中将 Amazon EFS 与 EKS 结合使用
【发布时间】:2021-03-10 19:20:33
【问题描述】:

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

aws/ k8s/

aws/ 内部是 .tf 文件,描述了 VPC、网络、安全组、IAM 角色、EKS 集群、EKS 节点组和一些 EFS 挂载。这些都是使用 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 集群?

【问题讨论】:

  • 你刚才说,它不起作用,但不提供任何代码或错误信息,我们怎么能猜到。如果您确实需要帮助,请尽可能提供详细信息。
  • 我已经更新了@BMW 的问题,我没有任何代码可以解决这就是我问这个问题的原因。

标签: amazon-web-services kubernetes amazon-eks kustomize amazon-efs


【解决方案1】:

@BMW 说得对,我能够将这一切都导入 Terraform。

aws/ 目录中,我创建了所有 AWS 资源、VPC、EKS、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
      }
    }
  }
}

【讨论】:

    【解决方案2】:

    如果您能够假设已安装 kubectl,则泰勒的这部分答案可以自动化:

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

    # https://docs.aws.amazon.com/eks/latest/userguide/efs-csi.html
    resource "null_resource" "install_efs_csi_driver" {
      depends_on = [module.eks.aws_eks_cluster]
      provisioner "local-exec" {
        command = format("kubectl --kubeconfig %s apply -k 'github.com/kubernetes-sigs/aws-efs-csi-driver/deploy/kubernetes/overlays/stable/?ref=release-1.1'", module.eks.kubeconfig_filename)
      }
    }
    

    【讨论】:

      【解决方案3】:

      这是我对您的问题的理解。

      首先你需要使用 terraform 来创建 EFS

      resource "aws_efs_file_system" "foo" {
        creation_token = "my-product"
      
        tags = {
          Name = "MyProduct"
        }
      }
      
      resource "aws_efs_mount_target" "alpha" {
        file_system_id = "${aws_efs_file_system.foo.id}"
        subnet_id      = "${aws_subnet.alpha.id}" # depend on how you set the vpc with terraform
      }
      

      之后需要记录efs id,例如fs-582a03f3

      然后为 EFS 添加新的 csi 驱动程序并设置持久卷,这些都是在 kubernetes 中直接使用 kubectl、helm charts、kustomize 完成的,或者您可以通过 aws_efs_file_system.foo.id (https://www.terraform.io/docs/providers/kubernetes/index.html) 使用 terraform kubernetes provider 来完成

      p>
      ---
      apiVersion: storage.k8s.io/v1beta1
      kind: CSIDriver
      metadata:
        name: efs.csi.aws.com
      spec:
        attachRequired: false
      
      ---
      apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: efs-pv
      spec:
        capacity:
          storage: 5Gi
        volumeMode: Filesystem
        accessModes:
          - ReadWriteMany
        persistentVolumeReclaimPolicy: Retain
        storageClassName: efs-sc
        csi:
          driver: efs.csi.aws.com
          volumeHandle: fs-582a03f3
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-05
        • 1970-01-01
        • 1970-01-01
        • 2021-01-22
        • 2020-12-17
        • 1970-01-01
        • 2021-05-31
        • 2016-08-08
        相关资源
        最近更新 更多