我想从自定义虚拟机映像在 Azure 上的两个不同区域部署规模集。我已经知道如何做到这一点,但我想根据规模集部署的区域切换配置信息。如何从虚拟机中检测虚拟机的区域?
虚拟机运行的是CentOS。
答案1
有一个简单的方法可以做到这一点,即使用虚拟机内的元数据服务。您可以从虚拟机内部运行此命令(所有虚拟机的 URL 相同)
curl -H Metadata:true http://169.254.169.254/metadata/instance?api-version=2017-03-01
它将返回一个包含有关虚拟机的数据(包括区域)的 JSON 对象:
{
"compute": {
"location": "westus",
"name": "avset2",
"offer": "UbuntuServer",
"osType": "Linux",
"placementGroupId": "",
"platformFaultDomain": "1",
"platformUpdateDomain": "1",
"publisher": "Canonical",
"resourceGroupName": "myrg",
"sku": "16.04-LTS",
"subscriptionId": "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx",
"tags": "",
"version": "16.04.201708030",
"vmId": "13f56399-bd52-4150-9748-7190aae1ff21",
"vmScaleSetName": "",
"vmSize": "Standard_D1",
"zone": "1"
},
"network": {
"interface": [
{
"ipv4": {
"ipAddress": [
{
"privateIpAddress": "10.1.2.5",
"publicIpAddress": "X.X.X.X"
}
],
"subnet": [
{
"address": "10.1.2.0",
"prefix": "24"
}
]
},
"ipv6": {
"ipAddress": []
},
"macAddress": "000D3A36DDED"
}
]
}
}
答案2
使用杰奇仅获取区域名称
curl -s -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance/compute?api-version=2021-02-01" | jq '.location'