无法删除Azure 容器实例使用的虚拟网络或子网

本文讨论删除Azure 容器实例 (ACI) 使用的虚拟网络 (VNet) 或子网时发生的错误,并提供解决方法。

症状

  • 删除 ACI 使用的子网时,会收到类似于以下的错误:

    Failed to delete subnet '<subnet-name>'.
    Error: 'Subnet /subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Network/virtualNetworks/<vnet-name>/subnets/<subnet-name> requires any of the following delegations
    [Microsoft.ContainerInstance/containerGroups] to reference service association link /
    subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Network/virtualNetworks/<vnet-name>/subnets/<subnet-name>/serviceAssociationLinks/acisal.'
    
    Subnet <subnet-name> is in use by /subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Network/networkProfiles/aci-network-profile-<network-profile-name>/containerNetworkInterfaceConfigurations/eth0/ipConfigurations/ipconfigprofile and cannot be deleted. 
    In order to delete the subnet, delete all the resources within the subnet. See aka.ms/deletesubnet.
    
    Failed to delete subnet '<subnet-name>'. 
    Error: Subnet <subnet-name> is in use by /subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Network/networkProfiles/aci-network-profile-<network-profile-name>/containerNetworkInterfaceConfigurations/eth0/ipConfigurations/ipconfigprofile/aci-network-profile-<network-profile-name>/eth0/ipconfigprofile and cannot be deleted. 
    In order to delete the subnet, delete all the resources within the subnet. See aka.ms/deletesubnet.
    
  • 删除 ACI 使用的 VNet 时,会收到以下错误:

    Failed to delete virtual network '<vnet-name>'. 
    Error: 'Subnet /subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Network/virtualNetworks/<vnet-name>/subnets/<subnet-name> requires any of the following delegations [Microsoft.ContainerInstance/containerGroups] to reference service association link 
    /subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Network/virtualNetworks/<vnet-name>/subnets/<subnet-name>/serviceAssociationLinks/acisal.'
    

ACI 所需的子网委派必须引用残差服务关联链接,这可以防止删除 ACI 使用的 VNet 或子网。

  1. 导航到Azure 门户中的子网。

  2. 将子网委派更改为 “无”。

  3. 使用 az network profile delete 命令删除网络配置文件,以确保没有网络配置文件链接到子网。

  4. 如果步骤 3 中的命令失败,则可能存在一个挥之不去的网络配置文件。 若要删除一个挥之不去的网络配置文件,请使用以下命令:

    az network profile delete --id resourceIdOfNetworkProfile
    
  5. 如果网络配置文件仍阻止子网更新,请尝试再次将子网委派设置为 “无 ”。

  6. 如果上述步骤不起作用,请尝试使用指定的 API 版本(例如版本 2018-10-01)通过 Azure CLI 删除服务关联链接:

    az resource delete --ids /subscriptions/<subscription-id>/resourceGroups/<resourcegroup-name>/providers/Microsoft.Network/virtualNetworks/<vnet-name>/subnets/<subnet-name>/providers/Microsoft.ContainerInstance/serviceAssociationLinks/default --api-version 2018-10-01
    

原因 2:网络配置文件阻止删除 VNet/子网

删除容器组时,可能无法正确删除 ACI 在创建容器组期间创建的网络配置文件。 这会导致 VNet 或子网中保留一些内容,从而阻止某些删除操作。

解决方法 1:从 Azure 门户中删除容器组的网络配置文件

删除所有 ACI 容器组后,请执行以下步骤:

  1. 转到资源组。
  2. 选择 “显示隐藏类型”。 默认情况下,网络配置文件隐藏在Azure 门户中。
  3. 选择与容器组相关的网络配置文件。
  4. 选择“删除”。
  5. 删除 VNet 或子网。

解决方法 2:通过 Azure CLI 删除容器组的网络配置文件

删除所有 ACI 容器组后,请执行以下步骤:

  1. 获取网络配置文件 ID:

    NetworkProfile=$(az network vnet subnet show -g $RES_GROUP --vnet-name $VNET_NAME --name $SUBNET_NAME -o tsv --query ipConfigurationProfiles[].id)
    
  2. 删除网络配置文件:

    az network profile delete --ids $NetworkProfile --yes
    
  3. 删除子网:

    az network vnet subnet delete --resource-group $RES_GROUP --vnet-name $VNET_NAME --name $SUBNET_NAME
    
  4. 删除 VNet:

    az network vnet delete --resource-group $RES_GROUP --name $SUBNET_NAME
    

解决方法 3:通过 Azure CLI 更新 containerNetworkInterfaceConfigurations 属性

如果通过 Azure 门户 和 Azure CLI 删除网络配置文件失败,请将网络配置文件属性containerNetworkInterfaceConfigurations更新为空列表:

  1. 获取网络配置文件 ID:

    NETWORK_PROFILE_ID=$(az network profile list --resource-group <resource-group-name> --query [0].id --output tsv)
    
  2. 更新网络配置文件:

    az resource update --ids $NETWORK_PROFILE_ID --set properties.containerNetworkInterfaceConfigurations=[]
    
  3. 删除网络配置文件和子网。

联系我们寻求帮助

如果你有任何疑问或需要帮助,请创建支持请求联系 Azure 社区支持。 还可以向 Azure 反馈社区提交产品反馈。