【问题标题】:Removing VHD's from Azure Resource Manager after removing VM删除 VM 后从 Azure 资源管理器中删除 VHD
【发布时间】:2020-05-30 06:13:13
【问题描述】:

引起我注意的是,如果您创建一个新 VM,然后删除它(及其关联的资源),您确实没有实际上删除了相应的 VHD 文件 -- 该文件是留在 blob 存储中,阻止配置具有相同主机名的新机器,并且还会为浪费和未使用的存储花费真金白银。我希望将 VHD 连同 VM 一起删除。

我在哪里可以找到关于如何处理这个问题的当前智慧的好文章?我只能找到 2013 年之前的参考资料,这些参考资料显然是针对“经典”版本的。

我编写了以下代码来尝试纠正这种情况。我有两个用例,首先我必须清除所有累积的垃圾,然后我“只是”需要确保在删除每台未来的机器后进行清理。

write-output("Removing orphaned disks for hostname ""$hostname"" ...")
$storageContext = (Get-AzureRmStorageAccount | Where-Object{$_.StorageAccountName -match $azureStorage}).Context
$storageBlob = Get-AzureStorageBlob -Context $storageContext -Container "vhds"
$vhdList = $storageBlob | Where-Object{$_.Name -match "$hostname"}
foreach($disk in $vhdList) {
    $diskname = $disk.Name
    write-output("Removing VHD ""$diskname"" ...")
    Remove-AzureDisk -Diskname "$diskname" -DeleteVHD
    write-output("Removed VHD ""$diskname"" ... [OK]")
}
write-output("Removed orphaned disks ... [OK]")

现在,当我运行它时,我会得到我希望看到的 VHD 文件的漂亮列表(以及一些相应的“*.status”文件)。但是,Remove-AzureDisk 命令会产生错误Remove-AzureDisk : ResourceNotFound: The disk with the specified name does not exist,因此它并没有真正起作用。

您会注意到我在中途从新的“ARM”命令切换到“经典”版本——这可能是我的问题的一部分,但我没有运气想出更好的命令.

更新:

这似乎可以解决问题:

# Verify that the OS VHD does not already exist
write-output("Checking for blocking VHD's ...")
$storageContext = (Get-AzureRmStorageAccount | Where-Object{$_.StorageAccountName -match $azureStorage}).Context
$storageBlob = Get-AzureStorageBlob -Context $storageContext -Container "vhds"
$vhdList = $storageBlob | Where-Object{$_.Name -match "$hostname"}
if ($vhdList) {
    write-output("There is an existing VHD blocking host ""$hostname"" in storage container ""$($storageContext.BlobEndPoint)vhds""):")
    foreach($vhdName in $vhdList.Name) {
        write-output("- $vhdName")
    }
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
    $answer = [System.Windows.Forms.MessageBox]::Show("There is an existing VHD blocking host ""$hostname"" in storage container ""$($storageContext.BlobEndPoint)vhds"").`nDo you wish to remove the existing VHD file?" , "Create New VM" , $MB_YESNO)
    if ($answer -eq "YES" ) {
        # Remove VHD files
        foreach($diskName in $vhdList.Name) {
            write-output("- Removing VHD ""$diskName""")
            Remove-AzureStorageBlob -Blob $diskName -Container "vhds" -Context $storageContext
        }
        write-output("Checked for blocking VHD's ... [OK]")
    } else {
        exit(331)
    }
}

【问题讨论】:

  • 如何删除虚拟机?你用什么命令?
  • 为了测试,我通过 Web 界面删除了 VM。据我(最近)了解,这会产生将 VHD 留在原地的不良影响。

标签: powershell azure azure-powershell azure-resource-manager


【解决方案1】:

Azure 资源管理的普遍看法是,您不再考虑原子单元中的资源,例如 VM、存储、网络,而是开始将它们视为一组资源。一个资源组,可以说是;)

理论上,您可以创建一个模板,通过 Rest、Powershell 或模板文件一次性创建整个部署。

然后,您无需删除虚拟机并重新创建它,而是删除整个资源组,然后再次运行部署,您将回到原来的位置。

它确实使整个事情更易于管理,并允许更复杂的构建。

使用该模型,如果您删除整个资源组,则所有底层资源、blob、存储帐户、网络都将被删除。

那么,关于你的问题。

Azure 不像经典管理器那样创建磁盘。基本上你将机器指向一个blob vhd,就是这样。所以你的命令的问题是没有任何磁盘可以删除。

所以你想要的命令

Remove-AzureStorageBlob -Blob $diskname -Container vhds -Context $context 

【讨论】:

  • 我曾考虑过这样做(每个 VM 一个专用资源组),但我得到的设置当前围绕单个组中的多个 VM。然而,我很有可能会挑战这一点,尤其是考虑到如此好的动机。
  • 您是否特别需要相互独立地添加和删除虚拟机?有没有可以围绕它们使用的分组。您可以将它们分成更大的组 - 随后您可以将它们视为一个单元 - 管理起来会更容易。
  • 是的,单独的;这些是运行(独立)测试套件的测试机器。任何时候拥有 50-100 个资源组,加上相当多的营业额会有问题吗?
  • 我删除了资源组。 VHD 列表不受影响。 :-( 我认为这种方法行不通。必须有某种形式的“正式正确方法”(显然)不明显。
  • @klaymendk 有一个更好的方法,你可以使用 vm 规模集,如果你发布一个新问题,概述你的问题,当我回到前面时,我会写一个正确的答案键盘!
【解决方案2】:

检查服务器时钟和客户端时钟

存储服务确保请求到达服务的时间不超过 15 分钟。这可以防止某些安全攻击,包括重放攻击。当此检查失败时,服务器返回响应码 403 (Forbidden)。

【讨论】:

  • 我对这与问题的关系有点困惑?
  • 我的假设是当他疲倦地使用 Remove-AzureDisk 删除时找不到资源,可能是因为时钟,所以请仔细检查该部分。
  • 那是因为它是一台 ARM 机器而且他们没有磁盘 ;)
最近更新 更多