
我想透過自訂虛擬機器映像在 Azure 上的兩個不同區域部署規模集。我已經弄清楚瞭如何執行此操作,但我想根據部署規模集的區域來切換配置資訊。如何從虛擬機器偵測該虛擬機器的區域?
虛擬機器運行的是 CentOS。
答案1
有一個非常簡單的方法可以做到這一點,即使用虛擬機器內的元資料服務。您可以從 VM 內部執行此命令(所有 VM 的 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'