2015-08-17 59 views
5

我正在将旧应用程序迁移到Elastic Beanstalk。它需要持久存储(暂时)。我想挂载EBS卷。将EBS卷(不是快照)挂载到Elastic Beanstalk EC2

我希望以下内容将在.ebextensions/ebs.config工作:

commands: 
    01mkdir: 
    command: "mkdir /data" 
    02mount: 
    command: "mount /dev/sdh /data" 

option_settings: 
    - namespace: aws:autoscaling:launchconfiguration 
    option_name: BlockDeviceMappings 
    value: /dev/sdh=vol-XXXXX 

https://blogs.aws.amazon.com/application-management/post/Tx224DU59IG3OR9/Customize-Ephemeral-and-EBS-Volumes-in-Elastic-Beanstalk-Environments

但不幸的是我得到的参数snapshotId以下错误“(第一卷-XXXX)是无效的预计:“卡扣......'。”

很明显,这种方法只允许快照。任何人都可以提出修正或替代方法。

回答

7

我找到了解决方案。可以通过删除“睡眠10”来改进,但不幸的是,因为aws ec2 attach-volume是异步的并且在附件发生之前立即返回,所以需要这样做。

container_commands: 
    01mount: 
    command: "aws ec2 attach-volume --volume-id vol-XXXXXX --instance-id $(curl -s http://169.254.169.254/latest/meta-data/instance-id) --device /dev/sdh" 
    ignoreErrors: true 
    02wait: 
    command: "sleep 10" 
    03mkdir: 
    command: "mkdir /data" 
    test: "[ ! -d /data ]" 
    04mount: 
    command: "mount /dev/sdh /data" 
    test: "! mountpoint -q /dev/sdh" 

注意。理想情况下,它将运行在commands部分不是container_commands但环境变量没有及时设置。

+0

而不是'ignoreErrors'可以'测试: “[!-b的/ dev/SDH]”'。 – Dan

+0

我不明白你的说明:什么环境变量? 如果这是在'commands'而不是'container_commands'中,您是否需要像@hashinclude提到的那样重新启动Docker容器? –

2

要添加到@西蒙的回答(以避免粗心的陷阱):

  • 如果安装最终将泊坞窗容器(例如里面,如果你正在运行詹金斯使用,要持久存储坚持jenkins_home),您需要在运行安装后重新启动Docker容器。
  • 您需要在EB中假定针对EC2实例(或实例/ * ARN)和要附加的卷(或卷/ * ARN)允许的'ec2:AttachVolumes'操作角色政策。没有这个,aws ec2 attach-volume命令失败。
  • 您需要在--region传递给aws ec2 ...命令以及(至少在写这篇文章的)
0

这是有缺失的配置:

commands: 
    01mount: 
    command: "export AWS_ACCESS_KEY_ID=<replace by your AWS key> && export AWS_SECRET_ACCESS_KEY=<replace by your AWS secret> && aws ec2 attach-volume --volume-id <replace by you volume id> --instance-id $(curl -s http://169.254.169.254/latest/meta-data/instance-id) --device /dev/xvdf --region <replace with your region>" 
    ignoreErrors: true 
    02wait: 
    command: "sleep 10" 
    03mkdir: 
    command: "mkdir /home/lucene" 
    test: "[ ! -d /home/lucene ]" 
    04mount: 
    command: "mount /dev/xvdf /home/lucene" 
    test: "! mountpoint -q /dev/xvdf"