删除VM后从Azure资源管理器删除VHD [英] Removing VHD's from Azure Resource Manager after removing VM

查看:112
本文介绍了删除VM后从Azure资源管理器删除VHD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提醒我,如果创建一个新的VM,然后将其(及其相关资源)删除,则 not 实际上并不删除相应的VHD文件-该文件是保留在Blob存储中,这会阻止以相同的主机名配置新计算机,并且还会浪费大量资金用于浪费和未使用的存储.我希望删除VHD和VM.

It was brought to my attention that, if you create a new VM and then delete it (and its associated resources), you do not in fact delete the corresponding VHD file -- that file is left in blob storage, blocking the provisioning of a new machine with the same hostname, and also costing real money for wasted and unused storage. I wish to delete the VHD's along with the VM's.

在哪里可以找到有关如何解决此问题的最新智慧的文章?我所能找到的只是2013年以前的参考文献,显然是针对经典"版本的.

Where can I find a good write-up of the current wisdom on how to deal with this? All I can find is references from before 2013 which evidently target the "classic" version.

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

I have written up the following code to try to remedy this situation. I have two use cases, first of all I have to get rid of all the accumulated junk, and after that I "just" need to make sure to clean up after each future machine when it is deleted.

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,因此它实际上无法正常工作.

Now, when I run that, I get nice listings of the VHD files I expect to see (and also some corresponding "*.status" files). However, the Remove-AzureDisk command yields the error Remove-AzureDisk : ResourceNotFound: The disk with the specified name does not exist so it's not really working.

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

You'll notice that I switch from the new "ARM" commands to the "classic" version half-way through -- this is likely part of my problem, but I've had no luck in coming up with better commands.

更新:

这似乎可以解决问题:

# 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)
    }
}

推荐答案

Azure资源管理的一般智慧是,您不再考虑原子单位的资源,例如VM,存储,网络,而是开始将其视为一组资源.资源组,原为;)

The general wisdom of Azure Resource Management is that you stop thinking about resources in atomic units, like VMs, storage, networks, and instead you start thinking of them as a group of resources. A resource group, as it were ;)

理论上讲,您将创建一个模板,该模板可通过Rest,Powershell或模板文件一次创建整个部署.

The theory is that you create a template that creates an entire deployment in a single go, either via Rest, Powershell, or template file.

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

then instead of deleting the VM and recreating it, you delete the entire Resource Group, and run the deployment again and you'll be back to exactly where you were.

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

It does make the whole thing much more manageable, and allows for much more complex builds.

使用该模型,如果您删除整个资源组,那么所有基础资源,blob,存储帐户和网络都将被删除.

Using that model, if you delete the entire resource group, then all of the underlying resources, blobs, storage accounts, networks will be deleted.

因此,请解决您的问题.

So, on to your problem.

Azure创建磁盘的方式与传统管理器不同.基本上,您将机器指向blob vhd,仅此而已.因此,您的命令存在的问题是没有要删除的磁盘.

Azure doesn't create disks in the same way that classic manager did. Basically you point the machine to a blob vhd and that's it. So the problem with your command is that there isn't any disk to delete.

所以您想要的命令

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

这篇关于删除VM后从Azure资源管理器删除VHD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆