aws beanstalk 结合packer创建自定义平台

参考资料

今天使用eb平台创建环境的时候,发现有名为packer的选项,查询文档发现aws beanstalk支持自定义平台,这功能几乎没人用,好奇驱使下尝试一番。

从很多方面而言,自定义平台都是比自定义映像更高级的自定义方式。用户可以通过自定义平台从头开始开发整个新平台,自定义 Elastic Beanstalk 在平台实例上运行的操作系统、附加软件和脚本

拉取仓库

$ git clone git@github.com:aws-samples/eb-custom-platforms-samples.git
$ cd eb-custom-platforms-samples/NodePlatform_Ubuntu

之前介绍过通过packer构建eks自定义镜像,了解了packer的入口配置,对于beanstalk的自定义环境来说同样使用标准的packer构建配置。

$ cat custom_platform.json
{
  "variables": {
    "platform_name": "{{env `AWS_EB_PLATFORM_NAME`}}",
    "platform_version": "{{env `AWS_EB_PLATFORM_VERSION`}}",
    "platform_arn": "{{env `AWS_EB_PLATFORM_ARN`}}"
  },
  "builders": [
    {
      "type": "amazon-ebs",
      "name": "HVM AMI builder",
      "region": "cn-north-1",
      "source_ami": "ami-043b7efd4c7a4c758",
      "instance_type": "m3.medium",
      "ssh_username": "ubuntu",
      "ssh_pty": "true",
      "ami_name": "Beanstalk Custom Platform running Node on Ubuntu Server 16.04 LTS (built on {{isotime \"20060102150405\"}})",
      "tags": {
        "eb_platform_name": "{{user `platform_name`}}",
        "eb_platform_version": "{{user `platform_version`}}",
        "eb_platform_arn": "{{user `platform_arn`}}"
      }
    }
  ],
  "provisioners": [
    {
      "type": "file",
      "source": "builder",
      "destination": "/tmp/"
    },
    {
      "type": "shell",
      "execute_command": "chmod +x {{ .Path }}; {{ .Vars }} sudo {{ .Path }}",
      "scripts": [
        "builder/builder.sh"
      ]
    }
  ]
}

构建脚本`builder的含义,对于构建文件和脚本的具体含义,官方文档有详细描述

https://docs.amazonaws.cn/zh_cn/elasticbeanstalk/latest/dg/custom-platforms.html

$ tree -L 2
├── builder.sh
├── CONFIG
├── platform-uploads
│   ├── etc
│   └── opt
└── setup-scripts
    ├── 00-sync-apt.sh
    ├── 01-install-nginx.sh
    └── 02-setup-platform.sh

开始构建新的自定义平台,选择符合要求的ubuntu 16.04作为源ami

$ ebp init
$ ebp create
Creating application version archive "app-230114_061319657514".
Uploading NodePlatform_Ubuntu/app-230114_061319657514.zip to S3. This may take a while.
Upload Complete.
Note: An environment called 'eb-custom-platform-builder-packer' has been created in order to build your application. This environment will not automatically be terminated and it does have a cost associated with it. Once your platform creation has completed you can terminate this builder environment using the command 'eb terminate'.
2023-01-14 06:13:20    INFO    Initiated platform version creation for 'NodePlatform_Ubuntu/1.0.0'.
2023-01-14 06:13:23    INFO    Creating Packer builder environment 'eb-custom-platform-builder-packer'.

beanstalk回单独创建一个环境来进行新ami的构建,新环境使用专属ami启动

整体关系如下,实际是把packer当作应用程序进行部署
在这里插入图片描述

可见一共启动了两台实例

在这里插入图片描述

实例使用的ami为 aws-elasticbeanstalk-amzn-2018.03.0.x86_64-packer-hvm-202105262310

在这里插入图片描述

构建相关的日志可以登录构建实例查看对应日志,例如/var/log/packer-builder/NodePlatform_Ubuntu:1.0.0-builder.log

$ eb events
2023-01-14 06:15:27    ERROR   [Instance: i-xxxxxxxxx] Command failed on instance. Return code: 1 Output: (TRUNCATED)...e to Version 3. More information can be found here: https://aws.amazon.com/blogs/developer/deprecation-schedule-for-aws-sdk-for-ruby-v2/
'packer build' failed, the build log has been saved to '/var/log/packer-builder/NodePlatform_Ubuntu:1.0.0-builder.log'.
$ ls /var/log/packer-builder
NodePlatform_Ubuntu:1.0.0-builder.log  NodePlatform_Ubuntu:1.0.1-builder.log  rotated
$ cat NodePlatform_Ubuntu:1.0.1-builder.log

或者直接查看platform日志

$ ebp logs 

构建完毕之后直接在创建环境时指定即可

$ eb create -p <platform arn>

一直报pip安装相关错误,在部署脚本里也没找到对应的逻辑,推测是pakcer版本适配的报错(6年前的示例),无法继续尝试

相关错误

没有指定source ami

2023-01-14 07:26:12    ERROR   Packer failed with error: '1 error(s) occurred:
* A source_ami or source_ami_filter must be specified'

ami不兼容,需要提供packer构建配置中符合的ami类型,不能写实例存储的ami

Packer failed with error: '--> HVM AMI builder: The provided source AMI has an invalid root device type.
1.0.0 b"Expected 'ebs', got 'instance-store'.'"

无法找到ec2终端节点,中国区的适配错误

ERROR   Packer failed with error: '--> HVM AMI builder: Error querying AMI: RequestError: send request failed
caused by: Post https://ec2.region.amazonaws.com/: dial tcp: lookup ec2.region.amazonaws.com on 172.31.0.2:53: no such host'

packer构建错误

2023-01-14 06:25:18    ERROR   Packer failed with error: '--> HVM AMI builder: Script exited with non-zero exit status: 1'
2023-01-14 06:25:18    ERROR   'packer build' failed, the build log has been saved to '/var/log/packer-builder/NodePlatform_Ubuntu:1.0.1-builder.log'
2023-01-14 06:25:28    ERROR   [Instance: i-xxxxxxxxx] Command failed on instance. Return code: 1 Output: (TRUNCATED)...e to Version 3. More information can be found here: https://aws.amazon.com/blogs/developer/deprecation-schedule-for-aws-sdk-for-ruby-v2/
'packer build' failed, the build log has been saved to '/var/log/packer-builder/NodePlatform_Ubuntu:1.0.1-builder.log'.
2023-01-14 06:25:28    ERROR   Unsuccessful command execution on instance id(s) 'i-xxxxxxxxx'. Aborting the operation.
posted @ 2023-01-14 15:35  zhaojie10  阅读(7)  评论(0)    收藏  举报  来源