Friday, February 15, 2013

PowerCLI - multiple vMotion to prepare host for maintenance

One of the clusters I work on has been recently upgraded to new servers with 128 MB of RAM. This makes the cluster RAM pool huge compared to the actual need. The VMs are distributed more or less equally among  hosts in the cluster. During maintenance periods I have to offload all VMs to another host which would automatically be taken care of by DRS when "Enter maintenance mode" command is issued on a host. However, when the host is back online nobody brings back the VMs. Load on other hosts is still too small for DRS to bother migrating anything. To solve my issue and not to leave a host completely empty, I use a small script in PowerCLI instead of "Enter maintenance mode" command.

The work flow I follow is: offload the host with the script, put manually the host in maintenance mode, do my thing, bring the host back online and finally load back the host using the script.
The script takes as input 3 parameters: source host, destination host and type of migration. "Out" migration represents offloading of a particular host, while "in" migration represents loading the host. For "in" to work, a csv file named vms_source-hostname.csv must exist. The file is created automatically by "out" migration in the path from were the script is run and it contains the names of powered on vms. Source host and destination host must remain the same for both "out" and "in" migrations.

Usage:
  • offloading (out) - vMotions all powered on VMs from vmhost1 to vmhost2
./Mass-vMotion -srcHost vmhost1.example.com -dstHost vmhost2.example.com -migType out
  •  loading (in) - vMotions all powered on VMs that belonged to vmhost1, from vmhost2 to vmhost1 (it gets its input from a file creted by a previous "out" run of the script)
 ./Mass-vMotion -srcHost vmhost1.example.com -dstHost vmhost2.example.com -migType in
Changing the migration type means changing migType parameter from "out" to "in". The script runs with -RunAsync parameter set and it saturates the bandwidth.
  
Param(
[Parameter(Mandatory=$True,Position=0)][string][ValidateNotNullOrEmpty()] $srcHost,
[Parameter(Mandatory=$True,Position=1)][string][ValidateNotNullOrEmpty()] $dstHost,
[Parameter(Mandatory=$True,Position=2)][string][ValidateNotNullOrEmpty()] $migType
)

if ((get-vmhost $srcHost -ErrorAction "SilentlyContinue") -and (get-vmhost $dstHost -ErrorAction "SilentlyContinue")) {
if ($migType -eq "out") ### OUT type migration
{
echo "Offloading powered on VMs from $srcHost to $dstHost"
$expfile = "vms_${srcHost}.csv"
get-vmhost $srcHost
get-vm
Where {$_.ExtensionData.Runtime.PowerState -eq "poweredOn"}
Select Name
Export-CSV $expfile
get-vmhost $srcHost
get-vm
Where {$_.ExtensionData.Runtime.PowerState -eq "poweredOn"}
Move-Vm -Destination $dstHost -RunAsync
}
elseif ($migType -eq "in") ### IN type migration
{
echo "Bringing back VMs from $dstHost to $srcHost"
$expfile = "vms_${srcHost}.csv"
if ( Test-Path $expfile)
{
$vmfile=Import-Csv $expfile
foreach ($vm in $vmfile) {$vmn=$vm.Name; Move-Vm -VM $vmn -Destination $srcHost -RunAsync}
Remove-Item $expfile
}
else { echo "VM file $expfile does not exist" }
}
else { Write-Warning "Please input correct migration type: 'out' or 'in'" }
}
else { Write-Warning "Host does not exist! Values entered are srcHost: $srcHost and dstHost: $dstHost" }

No comments: