【问题标题】:How do I programmatically get the Digital Ocean ID of the current Droplet from within the Droplet?如何以编程方式从 Droplet 中获取当前 Droplet 的 Digital Ocean ID?
【发布时间】:2018-07-30 01:24:32
【问题描述】:

我正在我的 AWS 和 Digital Ocean 服务器上设置监控和日志聚合,并且我希望能够在来自 Digital Ocean 的日志消息的元数据中包含一个 Droplet ID。

在 AWS 上,有一种方法可以从实例内部获取实例 ID:How to get the instance id from within an ec2 instance?

我正在 Digital Ocean 中寻找类似的东西,这样我就可以在我的日志中使用唯一的 ID 来识别水滴。

我不是在寻找类似主机名的东西,因为主机名可以设置为 /etc/hostname 中的任何内容。我可能在负载均衡器后面有多个 Web 服务器,它们都响应相同的主机名。 IP 地址可以更改,所以我不希望这样。

我想要一个唯一的 ID 来帮助我在 API 或 Web 控制台中找到我的 Droplet,并且我希望能够从在 Droplet 上运行的 shell 脚本中获取它。

【问题讨论】:

    标签: digital-ocean devops provisioning droplet


    【解决方案1】:

    您可以在液滴中运行curl http://169.254.169.254/metadata/v1/id,这将为您获取液滴的 ID。 如果你想在 shell 脚本中使用它,你可以这样做:

    #!/bin/sh
    droplet_id=$(curl http://169.254.169.254/metadata/v1/id)
    echo "droplet id: $droplet_id"
    
    # use $droplet_id in your script whenever you need to identify the droplet
    
    ...
    

    【讨论】:

      猜你喜欢
      • 2020-10-02
      • 2018-03-06
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多