gpt4 book ai didi

amazon-web-services - 从实例元数据端点检索正确的 Amazon 附加 EBS 设备

转载 作者:行者123 更新时间:2023-12-04 11:11:46 25 4
gpt4 key购买 nike

编辑和 TL;DR:

ubuntu@ip-172-31-19-77:~/.aws$ aws ec2 describe-instances | jq . | grep -i device
"BlockDeviceMappings": [],
"RootDeviceType": "ebs",
"RootDeviceName": "/dev/sda1",
"DeviceIndex": 0,
"BlockDeviceMappings": [
"DeviceName": "/dev/sda1",
"DeviceName": "/dev/xvdb",
"RootDeviceType": "ebs",
"RootDeviceName": "/dev/sda1",
"DeviceIndex": 0,
"BlockDeviceMappings": [
"DeviceName": "/dev/sda1",
"RootDeviceType": "ebs",
"RootDeviceName": "/dev/sda1",
"DeviceIndex": 0,
"BlockDeviceMappings": [
"DeviceName": "/dev/xvda",
"RootDeviceType": "ebs",
"RootDeviceName": "/dev/xvda",
"DeviceIndex": 0,
"BlockDeviceMappings": [
"DeviceName": "/dev/sda1",
"DeviceName": "/dev/sdf",
"RootDeviceType": "ebs",
"RootDeviceName": "/dev/sda1",
ubuntu@ip-172-31-19-77:~/.aws$ aws ec2 describe-volumes | jq . | grep -i device
"Device": "/dev/sda1"
"Device": "/dev/sdf"
"Device": "/dev/xvda"
"Device": "/dev/sda1"
"Device": "/dev/xvdb"
"Device": "/dev/sda1"
ubuntu@ip-172-31-19-77:~/.aws$ ls /dev/sd*
ls: cannot access '/dev/sd*': No such file or directory
ubuntu@ip-172-31-19-77:~/.aws$ ls /dev/xv*
ls: cannot access '/dev/xv*': No such file or directory
ubuntu@ip-172-31-19-77:~/.aws$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 1G 0 disk
nvme1n1 259:1 0 8G 0 disk
└─nvme1n1p1 259:2 0 8G 0 part /

说明:

我想提前知道特定 AWS 实例应该具有后附加的块设备。

不幸的是, the instance metadata endpoint for block devices introduced back in 2007似乎不再可靠地工作了?

这是一个例子 t2.medium我正在运行的实例 latest Ubuntu (17.10) AMI在撰写本文时( not Amazon Linux ):

enter image description here

不仅 EBS 模块显示在控制台中,而且似乎已正确连接:

enter image description here

据官方 AWS docs on block-device-mappings :

block-device-mapping/ebsN: The virtual devices associated with Amazon EBS volumes, if any are present. Amazon EBS volumes are only available in metadata if they were present at launch time or when the instance was last started. The N indicates the index of the Amazon EBS volume (such as ebs1 or ebs2). 2007-12-15



但不幸的是,在实例完全启动并运行后没有这样的端点 并附加所有 EBS 卷 :
$ curl http://169.254.169.254/latest/meta-data/block-device-mapping/
ami
ephemeral0
ephemeral1
$ curl http://169.254.169.254/latest/meta-data/block-device-mapping/ami
/dev/sda1
$ curl http://169.254.169.254/latest/meta-data/block-device-mapping/ebs1
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>404 - Not Found</title>
</head>
<body>
<h1>404 - Not Found</h1>
</body>
</html>
$ curl http://169.254.169.254/latest/meta-data/block-device-mapping/ebs2
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>404 - Not Found</title>
</head>
<body>
<h1>404 - Not Found</h1>
</body>
</html>

另一方面,操作系统(在 XEN 设备方案下)通过 xvdXX 公开卷。设备:
$ ls /dev/xvd*
/dev/xvda /dev/xvda1 /dev/xvdf /dev/xvdg

所以这给我留下了一个糟糕的解决方案 just assuming which block device is going to be depending on which instance我正在运行,它可以预见地中断一次,例如, Amazon introduces new iron具有完全不同的底层块设备命名方案,例如 /dev/nvme0n1p1 :

https://twitter.com/braincode/status/968005482102190080

以下是在附加了 EBS 卷的新实例化 M5 实例上发生的情况:
$ curl http://169.254.169.254/latest/meta-data/block-device-mapping/
ami
ebs2
root

$ curl http://169.254.169.254/latest/meta-data/block-device-mapping/ebs2
sdf
  • 为什么只有ebs2列出而不是ebs1 ?
  • sdf ?

  • 没有一个 sd*在 m5 实例的操作系统上:
    $ ls /dev/sd*
    ls: cannot access '/dev/sd*': No such file or directory

    我究竟做错了什么?这是一个已知的错误? people/boto/cloudinit 模块如何处理?

    最佳答案

    获取和标记 EBS 根卷 从运行 EC2 实例:
    这是您可以实现的方法:

    Instance_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
    Instance_Name=$(aws ec2 describe-instances --instance-ids ${Instance_ID} --query 'Reservations[0].Instances[0].Tags[?Key==`Name`].Value' --output text)
    RootDeviceName=$(aws ec2 describe-instances --instance-ids ${Instance_ID} --query 'Reservations[0].Instances[0].RootDeviceName' --output text)
    RootVolumeId=$(aws ec2 describe-instances --instance-ids ${Instance_ID} --query "Reservations[0].Instances[0].BlockDeviceMappings[?DeviceName==\`${RootDeviceName}\`].Ebs.VolumeId" --output text)

    如果要设置 RootVolume 标签(CloudFormation 尚不支持(2019-03-14)):
    aws ec2 create-tags --resources ${RootVolumeId} --tags Key=Name,Value=RootVolume-${Instance_Name}

    为此,您需要以下 IAM 权限:
    "ec2:DescribeInstances"
    "ec2:CreateTags"

    这是它的工作原理:

    看看(缩短的)ec2 json 响应:

    aws ec2 describe-instances --instance-ids ${Instance_ID}

    {
    "Reservations": [
    {
    "Instances": [
    {
    "InstanceId": "i-XXXXX",
    "BlockDeviceMappings": [
    {
    "DeviceName": "/dev/xvda",
    "Ebs": {
    "Status": "attached",
    "DeleteOnTermination": true,
    "VolumeId": "vol-XXXXX",
    "AttachTime": "2019-02-27T07:56:07.000Z"
    }
    },
    {
    "DeviceName": "/dev/sdm",
    "Ebs": {
    "Status": "attached",
    "DeleteOnTermination": false,
    "VolumeId": "vol-XXXXX",
    "AttachTime": "2019-02-27T07:58:02.000Z"
    }
    }
    ],
    "RootDeviceType": "ebs",
    "RootDeviceName": "/dev/xvda",
    }
    ],
    "ReservationId": "r-XXXXX",
    "RequesterId": "XXXXX",
    "Groups": [],
    "OwnerId": "XXXXX"
    }
    ]
    }

    如您所见:每个实例都有 BlockDeviceMappings 和 RootDeviceName 属性。
    1) 您可以选择当前为 设备名称 作为来自 的值根设备名称 属性(property)。
    2) 有了这些信息,您就可以解析您的 块设备映射 通过匹配您选择的 设备名称 最后得到 卷 ID .

    就是这样。

    关于amazon-web-services - 从实例元数据端点检索正确的 Amazon 附加 EBS 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49891037/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com