亚马逊AWS官方博客

将 FSx for Lustre CSI 驱动程序与 Amazon EKS 结合使用

 

CSI + Amazon EKS

容器存储接口 (CSI)是在 Mesos 或 Kubernetes 等容器协调器上暴露存储的标准。CSI 为 AWS 等存储提供商提供了创建精简封套的机会,该封套允许 Kubernetes 集群自动预置和管理存储类的整个生命周期。

Kubernetes 中的存储

最初,Kubernetes 试图通过一系列计算来减轻运行容器化应用程序的低水平重复的繁重工作。早期,它引入了的概念(类似于当时的 Docker 卷)。通过添加生命周期管理,在配置 Pod 时,可连接 PersistentVolume,这将与内部 Cloud Controller 代码通信,而后者又反过来预置了开箱即用的必要存储机制。对于 AWS,此存储使用 Amazon Elastic Block Store (EBS) 构建。

大约在 2015 年的同一时间,Dell/EMC 的 {code} 团队开始开发一个名为 REX-Ray 的开源项目。REX-Ray 的目标是创建一个与协调器无关的存储解决方案。在 Kubernetes 社区增加支持之前,REX-Ray 在 Mesos 社区中成为热门,并且是 Mesosphere DC/OS 集群的受支持附加组件。在它流行之后,使用 REX-Ray 的经验重新编写该项目,并将其命名为容器存储接口 (CSI)

Amazon Elastic Container Service for Kubernetes (Amazon EKS) 团队一直忙于为我们的所有存储解决方案构建 CSI 驱动程序,包括Amazon FSx for Lustre:一个完全托管的文件系统,与 S3 集成并针对高性能计算 (HPC) 和机器学习等计算密集型工作负载进行了优化。由于 AWS FSx CSI 驱动程序可能对任何 Kubernetes 用户有用,因此我们已将其捐赠给 Kubernetes SIG-AWS。本文的其余部分将重点介绍如何将 AWS FSx CSI 驱动程序部署到 Amazon EKS 集群。

先决条件

在开始之前,您需要设置 Amazon EKS 集群。对于此博文,我们将在集群配置文件机制中使用 eksctl。首先,必须下载以下工具:

安装所有必需的工具后,即可以开始启动 Amazon EKS 集群。在此示例中,您将在我们位于俄勒冈州的 us-west-2 中部署集群; 您可以将 AWS_REGION 替换为支持 Amazon EKS 的任何区域,并且还支持 Amazon FSx for Lustre

步骤

部署集群

export AWS_REGION=us-west-2

在导出区域后,按照如下所示创建 ClusterConfig

cat >cluster.yaml <<EOF
apiVersion: eksctl.io/v1alpha4
kind: ClusterConfig
metadata:
  name: fsx-csi-driver
  region: $AWS_REGION
  version: 1.12

nodeGroups:
  - name: ng-1
    desiredCapacity: 2
EOF

在创建 ClusterConfig 文件后,可使用 eksctl create cluster 命令创建集群:

eksctl create cluster -f cluster.yaml

完成此操作大约需要 10 – 15 分钟时间,之后,便可获得可供使用的 Amazon EKS 集群。

AWS IAM 策略

首先,需要将 Amazon EKS 工作线程节点配置为具有管理 FSx 文件系统的适当权限。为此,请创建 policy.json 并将其添加到我们的工作线程节点 IAM 角色:

cat >policy.json <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "iam:CreateServiceLinkedRole",
        "iam:AttachRolePolicy",
        "iam:PutRolePolicy"
       ],
      "Resource": "arn:aws:iam::*:role/aws-service-role/s3.data-source.lustre.fsx.amazonaws.com/*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "fsx:*"
      ],
      "Resource": ["*"]
  }]
}
EOF

现在,您已编写 policy.json ,您可以使用 aws 命令行界面 (CLI) 创建 IAM 策略:

POLICY_ARN=$(aws iam create-policy --policy-name fsx-csi --policy-document file://./policy.json --query "Policy.Arn" --output text)

接下来,将此策略添加到您的工作线程节点 IAM 角色:

INSTANCE_ROLE_NAME=$(aws cloudformation describe-stacks --stack-name eksctl-fsx-csi-driver-nodegroup-ng-1 --output text --query Stacks[0].Outputs[1].OutputValue | sed -e 's/.*\///g')
aws iam attach-role-policy --policy-arn ${POLICY_ARN} --role-name ${INSTANCE_ROLE_NAME}

安装 FSx CSI 驱动程序

将策略添加到实例 IAM 角色后,即可开始部署 FSx CSI 驱动程序。这将部署 StatefulSetDaemonSet 以及允许 FSx CSI 驱动程序管理存储所需的所有RBAC 规则:

kubectl apply -f https://raw.githubusercontent.com/kubernetes-sigs/aws-fsx-csi-driver/master/deploy/kubernetes/manifest.yaml

您现在可以列出 kube-system 命名空间中的所有 Pod,并验证 fsx-csi-controller-0fsx-csi-node-* Pod 是否正在运行

kubectl get pods -n kube-system

然后,您可以部署 StorageClassPersistentVolumeClaimPod,以使用新的 FSx for Lustre文件系统。

配置存储类

在部署 FSx StorageClass 之前,您需要收集并设置一些组件:

  1. 查找要预置进的 FSx for Lustre 文件系统的子网 ID。
  2. 创建一个允许集群访问 FSx 文件系统的安全组。

获取集群的 VPC ID:

VPC_ID=$(aws ec2 describe-vpcs --filters "Name=tag:Name,Values=eksctl-fsx-csi-driver/VPC" --query "Vpcs[0].VpcId" --output text)

接下来,获取您的一个 Amazon EKS 集群子网 ID; 您的 Lustre 文件系统将在此子网内预置:

SUBNET_ID=$(aws ec2 describe-subnets --filters "[{\"Name\": \"vpc-id\",\"Values\": [\"$VPC_ID\"]},{\"Name\": \"tag:aws:cloudformation:logical-id\",\"Values\": [\"SubnetPrivateUSWEST2A\"]}]"  --query "Subnets[0].SubnetId" --output text)

使用子网 ID,为 FSx 文件系统创建安全组,并添加从 192.168.0.0/16 CIDR 范围打开端口 988 的入站规则:

SECURITY_GROUP_ID=$(aws ec2 create-security-group --group-name eks-fsx-security-group --vpc-id ${VPC_ID} --description "FSx for Lustre Security Group" --query "GroupId" --output text)
aws ec2 authorize-security-group-ingress --group-id ${SECURITY_GROUP_ID} --protocol tcp --port 988 --cidr 192.168.0.0/16

一旦设置了子网 ID 和安全组 ID,就可以创建 StorageClass

cat >storage-class.yaml <<EOF
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: fsx-sc
provisioner: fsx.csi.aws.com
parameters:
  subnetId: ${SUBNET_ID}
  securityGroupIds: ${SECURITY_GROUP_ID}
EOF

然后将 StorageClass 部署到集群中:

kubectl apply -f storage-class.yaml

配置持久卷声明

使用 CSI 驱动程序时,您仍将使用本机 Kubernetes PersistentVolumeClaim 执行此操作,但您将通过 storageClassName 键配置 StorageClass

cat >claim.yaml <<EOF
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: fsx-claim
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: fsx-sc
  resources:
    requests:
      storage: 3600Gi
EOF

此新文件系统允许 accessModeReadWriteMany ,其大小为 3600Gi(此值可以是任何整数,最终值将四舍五入,扩展增量为 3,600 GiB)。然后将 PersistentVolumeClaim 部署到集群中:

kubectl apply -f claim.yaml

应用 PersistentVolumeClaim 之后,CSI 驱动程序会收到此资源的通知,并使用 Amazon FSx API 预置新的 FSx for Lustre 文件系统。在部署 Pod 之前,可以观察 PersistentVolumeClaim 资源并等待声明变为 Bound

kubectl get persistentvolumeclaims fsx-claim -w

使用 FSx for Luster 部署 Pod

虽然不必严格等待 PersistentVolumeClaim 变为 Bound 再部署 Pod,但很高兴看到每个步骤都有效。现在您可以创建将使用 FSx 文件系统的 Pod 清单:

cat >pod.yaml <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: fsx-app
spec:
  containers:
  - name: app
    image: centos
    command: ["/bin/sh"]
    args: ["-c", "while true; do echo \"hello from FSx\" >> /data/out.txt; sleep 5; done"]
    volumeMounts:
    - name: persistent-storage
      mountPath: /data
  volumes:
  - name: persistent-storage
    persistentVolumeClaim:
      claimName: fsx-claim
EOF

最后,您可以部署 Pod,它将每隔五秒将字符串 hello from FSx 写入 /data/out.txt

kubectl apply -f pod.yaml

在 FSx for Lustre 中查看数据

通过部署 StorageClassPersistentVolumeClaimPod,您可以查看正在写入 Lustre 文件系统的数据:

kubectl exec -ti fsx-app -- tail -f /data/out.txt

如您所见,此操作每隔五秒将 hello from FSx 写入 FSx for Luster 文件系统中的 out.txt 文件:

清理

要清理集群,您需要执行以下步骤:

kubectl delete -f pod.yaml
kubectl delete -f claim.yaml
kubectl delete -f storage-class.yaml
kubectl delete -f https://raw.githubusercontent.com/kubernetes-sigs/aws-fsx-csi-driver/master/deploy/kubernetes/manifest.yaml
aws ec2 delete-security-group --group-id ${SECURITY_GROUP_ID}
aws iam detach-role-policy --role-name ${INSTANCE_ROLE_NAME} --policy-arn ${POLICY_ARN}
eksctl delete cluster -f cluster.yaml

总结

通过此集成,您现在可以为高性能计算工作负载动态预置 FSx for Lustre 文件系统,并将容器自动连接到此文件系统。您还可以使用动态预置从不同节点的多个 Pod 消耗相同的持久卷声明。如果您需要使用自动可用的数据集启动容器,例如加载机器学习培训数据集,您甚至可以使用 Dynamic Provisioning with Data RepositoryStorageClass 连接到现有的Amazon S3 存储桶。其他示例可以在 FSx for Lustre CSI 驱动程序文档中找到。

如果您有任何问题或功能请求,请在Kubernetes SIG 存储库上为 AWS FSx CSI 驱动程序创建一个问题 – 欢迎您建言献策!

本篇作者

Chris Hein

Chris Hein

Chris Hein 是一位资深的开发者,倡导 Amazon Web Services 的 Kubernetes/EKS。加入 Amazon 之前,Chris 曾就职于众多规模不等的公司,如 GoPro、Sproutling 和 Mattel。有关 Chris 的更多信息,请访问 https://aws.amazon.com/blogs/opensource/author/heichris/,并通过 @christopherhein 关注他

阅读 Chris 撰写的更多博文。

Cheng Pan

cheng是AWS EKS团队的软件开发工程师,是EFS和FSX等的KubNETES CSI驱动程序的主要维护者。