使用 cloudformation 在 Elastic Beanstalk 上启动 docker 多容器

Posted

技术标签:

【中文标题】使用 cloudformation 在 Elastic Beanstalk 上启动 docker 多容器【英文标题】:Start docker multicontainer on Elastic Beanstalk with cloudformation 【发布时间】:2018-12-05 22:31:04 【问题描述】:

我想使用 canformation 在 Elastic Beanstalk (EBS) 上启动 docker multicontainer。

据我所知,doc 是我创建了 docker 映像。将其推送到 ECR。然后在我的项目的根目录下创建一个Dockerrun.aws.json。然后在Dorckerrun.aws.json 文件中链接ECR 路径。 ...就这样?

所以我创建了 docker 镜像并将其推送到 ECR。我还创建了具有相应值的Dockerrun.aws.json(有些我不太确定......例如mountPointshost.sourcePath 之间的区别是什么)。


  "AWSEBDockerrunVersion": 2,
  "volumes": [
    
      "name": "myApplication1",
      "host": 
        "sourcePath": "/var/app/current/myApplication1"
      
    ,
    
      "name": "myApplication2",
      "host": 
        "sourcePath": "/var/app/current/myApplication2"
      
    ,
    
      "name": "myApplication3",
      "host": 
        "sourcePath": "/var/app/current/myApplication3"
      
    
  ],
  "containerDefinitions": [
    
      "name": "myApplication1",
      "image": "123456789.dkr.ecr.eu-central-1.amazonaws.com/myDocker/myApplication1",
      "essential": true,
      "memory": 128,
      "mountPoints": [
        
          "sourceVolume": "????",
          "containerPath": "????",
          "readOnly": true
        ,
        
          "sourceVolume": "awseb-logs-myApplication1",
          "containerPath": "/var/log/myApplication1"
        
      ]
    ,
    
      "name": "myApplication2",
      "image": "123456789.dkr.ecr.eu-central-1.amazonaws.com/myDocker/myApplication2",
      "essential": true,
      "memory": 128,
      "portMappings": [
        
          "hostPort": 80,
          "containerPort": 80
        
      ],
      "links": [
        "myApplication1", "myApplication3"
      ],
      "mountPoints": [
        
          "sourceVolume": "????",
          "containerPath": "????",
          "readOnly": true
        ,
        
          "sourceVolume": "?????",
          "containerPath": "????",
          "readOnly": true
        ,
        
          "sourceVolume": "awseb-logs-myApplication2",
          "containerPath": "/var/log/myApplication2"
        
      ]
    ,
    
      "name": "myApplication3",
      "image": "123456789.dkr.ecr.eu-central-1.amazonaws.com/myDocker/myApplication3",
      "essential": true,
      "memory": 128,
      "mountPoints": [
        
          "sourceVolume": "?????",
          "containerPath": "?????",
          "readOnly": true
        ,
        
          "sourceVolume": "awseb-logs-myApplication3",
          "containerPath": "/var/log/myApplication3"
        
      ]
    
  ]

但我想知道如何在 cloudformation 中启动它?我的假设是我必须在 cloudformation 模板 (yaml) 中定义 EBS 并在某处引用资源 Dockerrun.aws.json。如果是这样,怎么做?我还没有找到用于此目的的模板(仅适用于单个 docker 容器)。

【问题讨论】:

【参考方案1】:

实例上的卷 从 Amazon EC2 容器实例中的文件夹或源包创建卷 (部署到 /var/app/current)。使用容器定义中的 mountPoints 将这些卷挂载到 Docker 容器中的路径。

Mount 是 ec2 主机上 Volume 的 docker mount。

要挂载的 Amazon EC2 容器实例中的卷,以及 Docker 容器文件系统上挂载它们的位置。 当您挂载包含应用程序内容的卷时,您的容器可以读取 您在源包中上传的数据。当您挂载日志卷以进行写入时 日志数据,Elastic Beanstalk 可以从这些卷中收集日志数据。

对于云形成,您需要创建以下内容。

ElasticBeanstalk 环境


   "Type" : "AWS::ElasticBeanstalk::Environment",
   "Properties" : 
      "ApplicationName" : String,
      "CNAMEPrefix" : String,
      "Description" :  String,
      "EnvironmentName" :  String,
      "OptionSettings" : [ OptionSetting, ... ],
      "PlatformArn" : String,
      "SolutionStackName" : String,
      "Tags" : [ Resource Tag, ... ],
      "TemplateName" : String,
      "Tier" : Environment Tier,
      "VersionLabel" : String
   

Docker 平台 https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/concepts.platforms.html#concepts.platforms.docker

ElasticBeanstalk 应用程序


   "Type" : "AWS::ElasticBeanstalk::Application",
   "Properties" : 
      "ApplicationName" : String,
      "Description" : String,
      "ResourceLifecycleConfig" : ApplicationResourceLifecycleConfig

   

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-beanstalk.html

ElasticBeanstalk 应用程序版本


  "Type" : "AWS::ElasticBeanstalk::ApplicationVersion",
  "Properties" : 
    "ApplicationName" : String,
    "Description" : String,
    "SourceBundle" :  SourceBundle 
  

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-beanstalk-version.html

您可以上传您的应用程序代码并运行 Docker。 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-beanstalk-sourcebundle.html

ElasticBeanstalk 配置模板


  AWS::ElasticBeanstalk::ConfigurationTemplate
    
      "Type" : "AWS::ElasticBeanstalk::ConfigurationTemplate",
      "Properties" :   
        "ApplicationName" : String,
        "Description" : String,
        "EnvironmentId" : String,
        "OptionSettings" : [ ConfigurationOptionSetting, ... ],
        "PlatformArn" : String,
        "SolutionStackName" : String,
        "SourceConfiguration" : SourceConfiguration
       
    

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker_image.html https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker_v2config.html

【讨论】:

以上是关于使用 cloudformation 在 Elastic Beanstalk 上启动 docker 多容器的主要内容,如果未能解决你的问题,请参考以下文章

Cloudformation + OpsWorks

使用 CloudFormation 在 ECS 服务上定义多个任务

通过 cloudformation 使用 aws `cdk synth` 输出

使用 Boto 从 CloudFormation 模板返回输出?

如何使用单个 cloudformation 模板创建多个 Elasticbeanstalk 环境

如何使用 cloudformation 模板创建 cloudwatch 事件?