【问题标题】:Azure - Managed Disks, how to create snapshotAzure - 托管磁盘,如何创建快照
【发布时间】:2017-07-08 06:40:20
【问题描述】:

我有使用存储帐户和磁盘部署 VM 的应用程序,我想将其转换为使用托管磁盘 - 因为这是 Azure 存储的未来。我正在查看 REST API - 我错过了两件事: 1.如何从现有的托管磁盘创建快照,有一个创建快照的API,但它是空的或来自旧的非托管 2.我可以选择创建磁盘的lun吗?

【问题讨论】:

    标签: azure azure-storage


    【解决方案1】:
    1. 如何从现有托管磁盘创建快照,有一个 API 可以创建快照,但它是空的或来自旧的非托管

    根据您的描述,我创建了一个测试演示来创建现有托管磁盘(OS 磁盘)的快照,效果很好。 我创建了一个 Windows VM 并将托管磁盘用作 OS 磁盘,然后我创建了另一个托管磁盘并将其添加到 VM。

    结果如下: 如果您想创建现有托管磁盘的快照(它有数据),我建议您可以将请求发送到以下 url。

    Url: https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Compute/snapshots/{snapshotName}?api-version={api-version}
    
    Method: PUT
    
    Parameter:
    subscriptionId  The identifier of your subscription where the snapshot is being created.
    resourceGroup   The name of the resource group that will contain the snapshot.
    snapshotName    The name of the snapshot that is being created. The name can’t be changed after the snapshot is created. Supported characters for the name are a-z, A-Z, 0-9 and _. The max name length is 80 characters.
    api-version The version of the API to use. The current version is 2016-04-30-preview.
    
    Request content:
    {
      "properties": {
        "creationData": {
          "createOption": "Copy",
          "sourceUri": "/subscriptions/{subscriptionId}/resourceGroups/{YourResourceGroup}/providers/Microsoft.Compute/disks/{YourManagedDiskName}"
        }
      },
      "location": "eastasia"
    }
    

    更多细节,您可以参考以下C#代码:

    json.txt:

    {
      "properties": {
        "creationData": {
          "createOption": "Copy",
          "sourceUri": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/BrandoSecondTest/providers/Microsoft.Compute/disks/BrandoTestVM"
        }
      },
      "location": "eastasia"
    }
    

    代码:

    static void Main(string[] args)
            {
                string body = File.ReadAllText(@"D:\json.txt");
                // Display the file contents to the console. Variable text is a string.
                string tenantId = "xxxxxxxxxxxxxxxxxxxxxxxx";
                string clientId = "xxxxxxxxxxxxxxxxxxxxxxxx";
                string clientSecret = "xxxxxxxxxxxxxxxxxxxx";
                string authContextURL = "https://login.windows.net/" + tenantId;
                var authenticationContext = new AuthenticationContext(authContextURL);
                var credential = new ClientCredential(clientId, clientSecret);
                var result = authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/", clientCredential: credential).Result;
                if (result == null)
                {
                   throw new InvalidOperationException("Failed to obtain the JWT token");
                }
                string token = result.AccessToken;
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://management.azure.com/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/xxxxxxxxxxxxxxxx/providers/Microsoft.Compute/snapshots/BrandoTestVM_snapshot2?api-version=2016-04-30-preview");
                request.Method = "PUT";
                request.Headers["Authorization"] = "Bearer " + token;
                request.ContentType = "application/json";
    
                try
                {
                    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                    {
                        streamWriter.Write(body);
                        streamWriter.Flush();
                        streamWriter.Close();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                //Get the response
               var httpResponse = (HttpWebResponse)request.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    Console.WriteLine(streamReader.ReadToEnd());
                }
    
                Console.ReadLine();
            }
    

    结果:

    1. 我可以选择创建磁盘的 lun 吗?

    您的意思是要使用 azuredeploy 来选择磁盘的 LUN?

    如果这是您的意见,我建议您可以参考以下 json 示例以了解如何构建 VM 的部署内容并选择其 LUN。

    更多细节,你可以参考下面的deploymentTemplate Json(部分):

    "diskArray": [
        {
        "name": "datadisk1",
        "lun": 0,
        "vhd": {
        "uri": "[concat('http://', variables('storageAccountName'),'.blob.core.windows.net/vhds/', 'datadisk1.vhd')]"
        },
        "createOption": "Empty",
        "caching": "[variables('diskCaching')]",
        "diskSizeGB": "[variables('sizeOfDataDisksInGB')]"
        },
      ]
    

    更多详情,您可以参考以下链接: 201-vm-dynamic-data-disks-selection/azuredeploy.json

    【讨论】:

    • 什么是 TenantID?
    【解决方案2】:
    1. 可以使用快照在同一资源组或不同资源组中创建新 VM。
    2. 如果要创建的 VM 位于不同的 RG 中,请确保位置相同。 Azure 仅支持在同一位置。
    3. 拍摄 OS 磁盘的快照 磁盘->单击操作系统磁盘->创建快照 输入名称 选择必须在其中创建快照的资源组。 (如果RG不同,请确保位置相同)
    4. 创建快照后,根据需要更新以下命令并从终端运行 (Azure cli - ref: [link][1])。

    提供订阅subscriptionId=88888-8888-888888-8888-8888888的订阅Id

    提供资源组的名称

    resourceGroupName=RG_name

    提供将用于创建托管磁盘的快照的名称(这是在上一步中创建的)

    snapshotName=OSDISK_snapshot

    提供将要创建的托管磁盘的名称

    osDiskName=MY_OSDISK

    以 GB 为单位提供磁盘大小。它应该大于 VHD 文件大小。

    diskSize=30

    提供托管磁盘的存储类型。 Premium_LRS 或 Standard_LRS。 storageType=Premium_LRS

    将上下文设置为将在其中创建托管磁盘的订阅 ID

    az account set --subscription $subscriptionId

    获取快照 ID

    snapshotId=$(az snapshot show --name $snapshotName --resource-group $resourceGroupName --query [id] -o tsv)

    az disk create -n $osDiskName -g $resourceGroupName --size-gb $diskSize --sku $storageType --source $snapshotId

    • 上述命令将在 RG 中创建托管磁盘。
    • 从 RG 下的列表中选择创建的磁盘,然后单击创建 VM。
    • 输入名称,选择 RG ,选择大小 .... 并点击创建。
    • 确保 NSG 具有所有可用的入站端口 Http - 80, ssh -22
    • 创建虚拟机后,从 RG 的资源列表中选择虚拟机。
    • 向下滚动到“运行命令”并运行以下命令,以防 SSH 和 HTTP 无法访问。 sudo service apace2 重启 sudo service ssh 重启
    • 这应该可以解决从浏览器和终端访问的问题。
    • 如果 ssh 仍然无法正常工作,请运行以下命令 rm /运行/nologin 现在通过 ssh 从终端访问虚拟机。

    【讨论】:

      猜你喜欢
      • 2021-11-14
      • 2018-05-25
      • 1970-01-01
      • 2017-12-31
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      相关资源
      最近更新 更多