如何为 Windows 基于服务器小型磁盘的 VMSS 或 Service Fabric 集群扩展 OS 磁盘?

How can I extend OS disks for Windows Server smalldisk based VMSS or Service Fabric clusters?

当我们使用 -smalldisk 镜像部署 Service Fabric 集群 VMSS 时

ARM 模板参数:

"vmImageOffer": {
  "value": "WindowsServerSemiAnnual"
},
"vmImageSku": {
  "value": "Datacenter-Core-1803-with-Containers-smalldisk"
},
"vmImageVersion": {
  "value": "1803.0.20180613"
}

ARM 模板:

"storageProfile": {
  "imageReference": {
    "publisher": "[parameters('vmImagePublisher')]",
    "offer": "[parameters('vmImageOffer')]",
    "sku": "[parameters('vmImageSku')]",
    "version": "[parameters('vmImageVersion')]"
},

没有足够的 space (<30GB) 用于在 SF 集群上托管容器映像。

如何在部署时增加磁盘大小(使用 ARM 模板)?

向 Service Fabric/VMSS ARM 模板添加了 3 个参数:

...
"scriptLocation": {
  "type": "string",
  "metadata": {
    "description": "Location of custom extension scripts on storage account container"
  }
},
"scriptStorageAccount": {
  "type": "string",
  "metadata": {
    "description": "Name of custom extension scripts storage account"
  }
},
"scriptStorageAccountKey": {
  "type": "string",
  "metadata": {
    "description": "Key to custom extension scripts storage account"
  }
},
...

这些参数在上传自定义扩展脚本文件和调用 New-AzureRmResourceGroupDeployment 的 PowerShell 脚本中填充。

...
$StorageAccountName = "mydeploymentstorage"
$StorageContainerName = "ext"
$ArtifactStagingDirectory = ".\ExtensionScripts"
...
# transfer Extension script to Storage    $StorageAccount = (Get-AzureRmStorageAccount | Where-Object{$_.StorageAccountName -eq $StorageAccountName})
$StorageAccountContext = $StorageAccount.Context
New-AzureStorageContainer -Name $StorageContainerName -Context $StorageAccountContext -Permission Container -ErrorAction SilentlyContinue *>&1
$ArtifactFilePaths = Get-ChildItem $ArtifactStagingDirectory -Recurse -File | ForEach-Object -Process {$_.FullName}
foreach ($SourcePath in $ArtifactFilePaths) {
    Write-Host "transfering" $SourcePath
    $BlobName = $SourcePath.Substring($SourcePath.LastIndexOf("\")+1)
    Set-AzureStorageBlobContent -File $SourcePath -Blob $BlobName -Container $StorageContainerName -Context $StorageAccountContext -Force -ErrorAction Stop
}

# prepare and pass script parameters
$DynamicParameters = New-Object -TypeName Hashtable
$DynamicParameters["scriptLocation"] = $StorageAccountContext.BlobEndPoint + $StorageContainerName
$DynamicParameters["scriptStorageAccount"] = $StorageAccountName
$DynamicParameters["scriptStorageAccountKey"] = ($StorageAccount | Get-AzureRmStorageAccountKey).Value[0]
...
# start deployment
New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) ` `
    -ResourceGroupName $ResourceGroupName `
    -TemplateFile $TemplateFile `
    -TemplateParameterFile $TemplateParametersFile `
    @DynamicParameters `
    -Verbose

storageProfile 中增加了磁盘大小:

...
      "storageProfile": {
        "imageReference": {
          "publisher": "[parameters('vmImagePublisher')]",
          "offer": "[parameters('vmImageOffer')]",
          "sku": "[parameters('vmImageSku')]",
          "version": "[parameters('vmImageVersion')]"
        },
        "osDisk": {
          "caching": "ReadWrite",
          "createOption": "FromImage",
          "diskSizeGB": 128,
          "managedDisk": {
            "storageAccountType": "[parameters('storageAccountType')]"
          }
        }
      }
...

将自定义脚本扩展添加到 VMSS extensionProfile:

...
    "virtualMachineProfile": {
      "extensionProfile": {
        "extensions": [
...
          {
            "name": "[concat(parameters('vmNodeType0Name'),'_SetupNode')]",
            "properties": {
              "publisher": "Microsoft.Compute",
              "type": "CustomScriptExtension",
              "typeHandlerVersion": "1.9",
              "autoUpgradeMinorVersion": true,
              "settings": {
                "fileUris": [
                  "[concat(parameters('scriptLocation'),'/setupNode.ps1')]"
                ]
              },
              "protectedSettings": {
                "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File CreateDisk.ps1",
                "storageAccountName": "[parameters('scriptStorageAccount')]",
                "storageAccountKey": "[parameters('scriptStorageAccountKey')]"
              }
            }
          }
        ]
...

创建脚本 setupNode.PS1 以调整 OS 磁盘的分区大小

# resize Partition
$size = (Get-PartitionSupportedSize -DiskNumber 0 -PartitionNumber 1) 
Resize-Partition -DiskNumber 0 -PartitionNumber 1 -Size $size.SizeMax