如何在Dask分布式AWS ECS/EC2集群中挂载文件夹
实现Dask EC2/ECS集群挂载共享存储读取模型
因为EC2/ECS工作节点是AWS远程实例,本地文件夹无法直接挂载,需要借助AWS共享存储服务同步模型文件后,再挂载到集群节点。以下分两种场景给出具体实现:
一、Dask EC2Cluster 挂载EFS(弹性文件系统)
前置准备
- 在AWS控制台创建EFS文件系统,确保其VPC与EC2集群的VPC一致,并在集群所在子网部署挂载目标。
- 将本地模型文件上传到EFS:可通过临时EC2实例挂载EFS后上传,或先同步到S3再转存至EFS。
创建集群时配置挂载
使用extra_volumes参数指定EFS挂载规则,示例代码:from dask_cloudprovider.aws import EC2Cluster efs_fs_id = "fs-xxxxxx" # 替换为你的EFS文件系统ID mount_path = "/mnt/model" region = "us-east-1" cluster = EC2Cluster( image_id="your-ami-id", # 推荐使用Dask官方AMI,默认支持NFS instance_type="t3.medium", extra_volumes=[ { "Name": "efs-model-mount", "Device": f"nfs4:{efs_fs_id}.efs.{region}.amazonaws.com:/", "MountPoint": mount_path, "FsType": "nfs4", "ReadOnly": False } ], region=region, # 其他集群配置(如密钥对、子网等) )调整模型读取代码
将model_path改为EFS挂载后的路径,比如/mnt/model/your_model.pkl。
二、Dask ECSCluster 挂载EFS
前置准备:同EC2场景,确保EFS与ECS集群VPC一致。
创建集群时配置挂载
使用volume_configs和mount_points参数配置EFS挂载,示例代码:from dask_cloudprovider.aws import ECSCluster cluster = ECSCluster( region="us-east-1", task_cpu=1024, task_memory=2048, volume_configs=[ { "name": "model-volume", "efsVolumeConfiguration": { "fileSystemId": "fs-xxxxxx", "rootDirectory": "/", "transitEncryption": "ENABLED" } } ], mount_points=[ { "sourceVolume": "model-volume", "containerPath": "/mnt/model", "readOnly": False } ], # 其他集群配置(如任务角色、子网等) )调整模型读取代码:同样使用
/mnt/model/your_model.pkl作为模型路径。
替代方案:直接从S3读取模型
如果不想挂载文件系统,可直接通过S3读取模型,需在工作节点安装s3fs依赖:
import s3fs import pickle def read_model(s3_model_path): fs = s3fs.S3FileSystem() with fs.open(s3_model_path, 'rb') as f: model = pickle.load(f) return model # 提交任务时传入S3路径 future = client.submit(read_model, "s3://your-bucket/model-dir/your_model.pkl") model = future.result()
创建集群时可通过extra_pip_packages=["s3fs"]参数自动安装依赖。
内容的提问来源于stack exchange,提问作者himanshu jain
相关产品推荐
相关产品推荐

