ScriptVMware

Consolidating datacenters

There is truly many ways and approaches to do server/datacenter consolidation… What i’m sharing here is just one way to do it, for one customer. But it has been done in so many ways for other customers – simple p2v and v2v from customer site in to the datacenter and onto more advanced approaches where some of the data is mirrored in via SAN replication/mirror and the rest is p2v or v2v’ed into datacenter.

Like i said there are many ways to do this… What is this project, well quite simple it’s all about moving 800 VMs from one datacenter to another, in this case I was so lucky that the datacenters are relatively close to each other which made it possible, to extend the network and stretch the storage over to the customers site, which again greatly simplifies the setup and how we’re able move the VMs from one site to the other.

The below graph hopefully helps to simplify how this is done. On both sites of there’s deployed various vSphere stack components with a minimum requirement of a vCenter installation. The customer was already using the vSwtich which was one the reasons that this approach was chosen. The customers vlans where extended into the datacenter where they were translated to suited vlans at the datacenter, next the storage from the datacenter were extended/streched into the customer site and datastores were presented to the customers vSphere clusters. Lastly hosts in the datacenter were brought up and configured to match the customers ESX version and settings. See my post on HP BIOS settings in order to get the must out of your host hardware.┬áThese hosts were then presented to the customers vCenter.

  • Now we have customer network extended to datacenter.
  • Datacenter storage streched to customer site.
  • Datacenter Hosts in a cluster at the customer site.

 

Now it was just a matter of creating the network (will blog about that in an up coming post) and moving the VMs. The last part were done with the script below in order to standalize the moving of VMs and save time and mouse clicks.

 

try
{
    Start-Transcript -path "c:\temp\Server$(Get-Date -f ddMM-yy_HHmm).txt"
Get-date

# Adding PowerCLI core snapin
if (!(get-pssnapin -name VMware.VimAutomation.Core -erroraction silentlycontinue)) {
add-pssnapin VMware.VimAutomation.Core
}

$VIServer = "vCENTER"
$VIConnection = Connect-VIServer $VIServer
if (-not $VIConnection.IsConnected) {
Write-Host "Unable to connect to vCenter, please ensure you have altered the vCenter server address correctly "
Write-Host " to specify a username and password edit the connection string in the file $GlobalVariables"
break
}
[console]::ForegroundColor = "RED"
$FromCluster = Read-Host 'Which cluster do you want to migrate FROM?'
[console]::ForegroundColor = "GREEN"
$ToCluster = Read-Host 'Which cluster do you want to migrate TO?'
[console]::ResetColor()
$list = Get-Content C:\Temp\MoveVM\ServerListe.txt; $list = $list -replace "`t|`n|`r",""
$OFS = "`r`n`r`n"

#DO NOT MODIFY BELOW THIS LINE
# Code in the migrateVMConfig function migrates only the VM config, swp, etc. files
# This was taken from a post on the VMware Community forums by Luc Dekens (LucD)
# relevant thread: http://communities.vmware.com/thread/299279/
# Luc's blog: http://www.lucd.info/
# MOD: By Michael Ryom
function migrateVMConfig($vm, $destinationDatastore) {
#MOD: No need for
#$vmName = $vm
$tgtConfigDS = $destinationDatastore

#MOD: No need for
#$vm = Get-VM -Name $vmName
    $hds = Get-HardDisk -VM $vm

$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec
#Added where part cause storage is shared between datacenters and clusters
$spec.datastore = (Get-Datastore -Name $tgtConfigDS | where {$_.datacenter -match $vm.VMHost.Parent.ParentFolder.Parent.Name}).Extensiondata.MoRef
$hds | %{
   $disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
   $disk.diskId = $_.Extensiondata.Key
   $disk.datastore = $_.Extensiondata.Backing.Datastore
   $spec.disk += $disk
}

$vm.Extensiondata.RelocateVM_Task($spec, "defaultPriority")
}

#Get VM list from cluster
$VMs = Get-Cluster $FromCluster | Get-VM $list | sort

foreach($VM in $VMs){
#If CD mounted, unmount cd
$VM.name
write-host -----------------
$VM
Write-host Checking for mount points...
Get-CDDrive $VM | Where { $_.IsoPath.Length -gt 0 -OR $_.HostDevice.Length -gt 0 } | Set-CDDrive -NoMedia -Confirm:$False

#svMotion prep
#If data is on one datastore move all
if($VM.DatastoreIdList.Count -eq 1){
Write-host Moving all disks...
#$VM.name
#Do svMotion
(Measure-Command {Move-VM -VM $VM -Datastore (Get-random (Get-VMHost $vm.VMHost.name | Get-Datastore | where {$_.Name -like "*Staging*" -and $_.FreeSpaceMB -gt ($vm.ExtensionData.Summary.Storage.Committed/1MB + 100000)})) -DiskStorageFormat Thin}).TotalSeconds
Write-Host VMsize :
($vm.ExtensionData.Summary.Storage.Committed/1MB)
}
#Data is on multiple datastores move one VMDK at a time
elseif($VM.DatastoreIdList.Count -gt 1){
$i = 0
#$VM.name
Write-host Moving one disk at a time...
foreach($HD in (Get-harddisk $VM)){
$HD.name
#Do svMotion
(Measure-Command {Move-HardDisk -HardDisk $HD -Datastore (Get-random (Get-VMHost $vm.VMHost.name | Get-Datastore | where {$_.Name -like "*Staging*" -and $_.FreeSpaceMB -gt ($HD.ExtensionData.CapacityInKB/1024 + 10000)})) -StorageFormat Thin -Confirm:$false}).TotalSeconds
Write-host HDsize :
($HD.ExtensionData.CapacityInKB/1024)
Write-Host HDUsedSize :
((($vm.extensiondata.LayoutEx.File | where {$_.type -match "diskExtent"}).size)[$i]/1MB)
$i++
}
#Move all non VMDK's to new location
Write-Host Move config :
(Measure-Command {migrateVMConfig $vm (Get-Datastore -id (Get-HardDisk $vm | where {$_.Name -match "Hard Disk 1"}).ExtensionData.Backing.Datastore).name}).TotalSeconds
}
Write-Host Move VM :
(Measure-Command {Move-VM $VM -Destination $ToCluster}).TotalSeconds

Get-date
}}

finally
{
    [console]::ResetColor()
if($VIServer -eq ($global:DefaultVIServers | %{$_.Name})){
Disconnect-VIServer -force -confirm:$false
}
Stop-Transcript
}

One thought on “Consolidating datacenters

Leave a Reply

Your email address will not be published. Required fields are marked *