如何在 Terraform 中将 Amazon EFS 与 EKS 结合使用

How to use Amazon EFS with EKS in Terraform

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

aws/ k8s/

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

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

我无法弄清楚如何将 EFS 安装作为持久卷安装到我的 pods。

我找到了描述使用 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 来创建 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 使用 terraform kubernetes provider 完成(https://www.terraform.io/docs/providers/kubernetes/index.html)

---
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

@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
      }
    }
  }
}

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

After the EKS cluster is created I had to manually install the EFS CSI driver into the cluster before continuing."

# 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)
  }
}