lunedì 23 dicembre 2013

VMware: Automating vFRC deployment with PowerCLI

After Increasing VM read performances using vFRC let's discuss about automating vFRC deployment in datacenter using PowerCLI.

A few weeks ago VMware released some brand new cmdlets for automating VSAN and vFRC using PowerCLI, so, after some testing, I decided to come up with a blog post regarding vFRC automation. What's about vFRC cmdlets? They are packed in an additional PowerCLI module that needs to be imported prior to be used. Once imported we are ready to go and as they say "sky is the limit".

At first you need to download cmdlets: Download VSAN & vFRC PowerCLI cmdlets.

As well explained in installation instructions place downloaded module in your reference folder, if you already don't have one create a new folder wherever you like. I used "C:\Users\paolop\Documents\WindowsPowerShell\Modules".

Then we need to add this new folder as an environment variable, so we can import modules from.

Here's the script that will do this for you. Copy these lines over to a text file, edit them accordingly to your environment and save it with .ps1 extension then run it from PowerCLI shell.

 $p = [Environment]::GetEnvironmentVariable("PSModulePath")  
 echo $p #Show your current path to modules  
 $p += ";C:\Users\<YOUR_USER>\Documents\WindowsPowerShell\Modules" #Add your custom location for modules  
 [Environment]::SetEnvironmentVariable("PSModulePath",$p)  

Once environment variable is set we need to import VSAN & vFRC module into PowerCLI shell:

Import-Module VMware.VimAutomation.Extensions

New cmdlets are now available:

Get-HardDiskVFlashConfiguration
Get-VMHostVFlashConfiguration
Get-VsanDisk
Get-VsanDiskGroup
New-VsanDisk
New-VsanDiskGroup
Remove-VsanDisk
Remove-VsanDiskGroup
Set-HardDiskVFlashConfiguration
Set-VMHostVFlashConfiguration

For article purpouse we only consider vFRC related cmdlets:

Get-Command -Module VMware.VimAutomation.Extensions | Format-List


 Name       : Get-HardDiskVFlashConfiguration  
 Definition    : Get-HardDiskVFlashConfiguration [[-HardDisk] <HardDisk[]>] [  
           -Server <VIServer[]>] [-Verbose] [-Debug] [-ErrorAction <Act  
           ionPreference>] [-WarningAction <ActionPreference>] [-ErrorV  
           ariable <String>] [-WarningVariable <String>] [-OutVariable  
           <String>] [-OutBuffer <Int32>]  
 Name       : Get-VMHostVFlashConfiguration  
 Definition    : Get-VMHostVFlashConfiguration [[-VMHost] <VMHost[]>] [-Serve  
           r <VIServer[]>] [-Verbose] [-Debug] [-ErrorAction <ActionPre  
           ference>] [-WarningAction <ActionPreference>] [-ErrorVariabl  
           e <String>] [-WarningVariable <String>] [-OutVariable <Strin  
           g>] [-OutBuffer <Int32>]  
 Name       : Set-HardDiskVFlashConfiguration  
 Definition    : Set-HardDiskVFlashConfiguration [-VFlashConfiguration] <Hard  
           DiskVFlashConfiguration[]> [-CacheSizeGB <Decimal>] [-CacheB  
           lockSizeKB <Int64>] [-Verbose] [-Debug] [-ErrorAction <Actio  
           nPreference>] [-WarningAction <ActionPreference>] [-ErrorVar  
           iable <String>] [-WarningVariable <String>] [-OutVariable <S  
           tring>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm]  
 Name       : Set-VMHostVFlashConfiguration  
 Definition    : Set-VMHostVFlashConfiguration [-RemoveVFlashResource] [-VFla  
           shConfiguration <VMHostVFlashConfiguration[]>] [-SwapCacheRe  
           servationGB <Int64>] [-AddDevice <VMHostDisk[]>] [-AttachExi  
           stingVffs <String>] [-Server <VIServer[]>] [-Verbose] [-Debu  
           g] [-ErrorAction <ActionPreference>] [-WarningAction <Action  
           Preference>] [-ErrorVariable <String>] [-WarningVariable <St  
           ring>] [-OutVariable <String>] [-OutBuffer <Int32>] [-WhatIf  
           ] [-Confirm]  

Let's now delve more specifically into PowerCLI with an sample script that creates a new DC, enable flash pool resource on DC's hosts and create new virtual machines enabling vFRC on their disk.

To do this I've created 3 different scripts:

CreateDC.ps1: that creates a datacenter, a cluster with HA and DRS, and then dinamically adds hosts into DC by reading them from an input file containing host's IP or FQDN.

vFRC.ps1: adds server's local flash drives to vFRC pool.

CreateVMs.ps1: Deploy VMs from an existing template and reserve 1GB of their disk on vFRC pool.

At first we need to create a Settings.xml file used by PowerCLI scripts to read variables from. As you will notice from my code I will use variables coded in an XML file and read by PowerCLI scripts.

Edit this according to your environment.

 <?xml version="1.0" encoding="utf-8"?>  
 <Settings>  
 <vCenterIPorFQDN>10.0.0.127</vCenterIPorFQDN>  
 <vCenterUsername>Administrator@vsphere.local</vCenterUsername>  
 <vCenterPassword>vmware</vCenterPassword>  
 <DatacenterName>My Datacenter</DatacenterName>  
 <DatacenterFolder>Datacenter Folder</DatacenterFolder>  
 <ClusterName>My Cluster</ClusterName>  
 <HostUsername>root</HostUsername>  
 <HostPassword>mypassword</HostPassword>  
 <VmNumber>1</VmNumber>  
 <VmBaseName>vFRC_VM</VmBaseName>  
 <VmTemplate>Windows2k8Template</VmTemplate>  
 <VmDatastore>Datastore</VmDatastore>  
 <CacheSizeGB>1</CacheSizeGB>  
 </Settings>  

Variables name are quite self-explicative, although it may be useful to point out that:

HostUsername: username for hosts that will be added in the cluster. For sake of simplicity I assume all hosts have the same user/password.
HostPassword: password for hosts that will be added in the cluster. For sake of simplicity I assume all hosts have the same user/password.
VmNumber: is the number of VM to deploy from base template (Windows2k8Template in this example)
VmDatastore: datastore name where virtual machine will reside.
CacheSizeGB: how much GB of vFRC will be assigned to virtual machine disk.

Another configuration file required by script is hosts.txt which contains ESXi hosts IP addresses or FQDN.

My hosts.txt contains one host IP address only:

 10.0.0.126  

First PowerCLI script is CreateDC.ps1:

 [xml] $xmlconfigurations=get-content Settings.xml  
 Write-Host "Connecting to" $xmlconfigurations.Settings.vCenterIPorFQDN "vCenter" -foregroundcolor "magenta"  
 Connect-VIServer -Server $xmlconfigurations.Settings.vCenterIPorFQDN -User $xmlconfigurations.Settings.vCenterUsername -Password $xmlconfigurations.Settings.vCenterPassword  
 Write-Host "Creating" $xmlconfigurations.Settings.DatacenterFolder "Folder" -foregroundcolor "magenta"  
 Get-Folder -NoRecursion | New-Folder -Name $xmlconfigurations.Settings.DatacenterFolder  
 Write-Host "Creating" $xmlconfigurations.Settings.DatacenterName "Datacenter and" $xmlconfigurations.Settings.ClusterName "Cluster" -foregroundcolor "magenta"  
 New-Cluster -Location (  
 New-Datacenter -Location $xmlconfigurations.Settings.DatacenterFolder -Name $xmlconfigurations.Settings.DatacenterName  
 ) -Name $xmlconfigurations.Settings.ClusterName -HAEnabled -HAAdmissionControlEnabled:$false -DRSEnabled -DrsAutomationLevel FullyAutomated  
 Get-Content hosts.txt | Foreach-Object { #Read hosts in hosts.txt   
 Write-Host "Adding" $_ "to" $xmlconfigurations.Settings.ClusterName "Cluster" -foregroundcolor "magenta"  
 Add-VMHost $_ -Location $xmlconfigurations.Settings.ClusterName -User $xmlconfigurations.Settings.HostUsername -Password $xmlconfigurations.Settings.HostPassword -RunAsync -force:$true  
 }  

As stated above it will create a new datacenter, a cluster, and will add hosts to it. Nothing particular here, no "new" cmdlets are used here. I provide this code as an example for reading variables from an XML and TXT file. As most of you already have a datacenter properly setup you can safely skip this script and move to next which will bring in vFRC cmdlets.







vFRC.ps1 will create a new vFRC pool by using host local unused SSD diks.

 Write-Host "Importing PowerCLI vFRC cmdlets" -foregroundcolor "magenta"  
 Import-Module VMware.VimAutomation.Extensions #Import module  
 Get-Content hosts.txt | Foreach-Object { #Read hosts in hosts.txt   
 Write-Host "Getting current vFRC configuration for" $_ "host" -foregroundcolor "magenta"  
 $vFlashConfig = Get-VMHostVFlashConfiguration -VMHost $_  
 echo $vFlashConfig  
 Write-Host "Getting" $_ "host SSDs to be used by vFRC" -foregroundcolor "magenta"  
 $vFlashDisk = Get-VMHostDisk -VMHost $_  
 echo $vFlashDisk  
 Set-VMHostVFlashConfiguration -VFlashConfiguration $vFlashConfig -AddDevice $vFlashDisk #Enable vFRC on selected host   
 }  

This is done by using Set-VMHostVFlashConfiguration cmdlet.

Set-VMHostVFlashConfiguration -VFlashConfiguration $vFlashConfig -AddDevice $vFlashDisk





Last script is CreateVMs.ps1 that deploys VMs from a base template, reserve an amount of their disk space in vFRC pool and then place them on a host (DRS will place VM to right host according to resource availability). Host IP address is statically defined in the following script but can be easily read from an external file as explained in scripts above.

 [xml] $xmlconfigurations=get-content Settings.xml  
 Write-Host "Creating" $xmlconfigurations.Settings.NumberOfVM "VMs" -foregroundcolor "magenta"  
 $vmname = $xmlconfigurations.Settings.VmBaseName  
 1..$xmlconfigurations.Settings.NumberOfVM | Foreach {  #Creates VMs on 10.0.0.126 host
 New-VM -VMHost 10.0.0.126 -Name $vmname$_ -Template $xmlconfigurations.Settings.VmTemplate -Datastore $xmlconfigurations.Settings.VmDatastore  
 }  
 Import-Module VMware.VimAutomation.Extensions #Import module  
 Write-Host "Enabling vFRC on" $xmlconfigurations.Settings.VmNumber "VMs" -foregroundcolor "magenta"  
 1..$xmlconfigurations.Settings.NumberOfVM | Foreach {  
 Set-HardDiskVFlashConfiguration -VFlashConfiguration (Get-HardDiskVFlashConfiguration -HardDisk (Get-HardDisk -VM $vmname$_)) -CacheSizeGB $xmlconfigurations.Settings.CacheSizeGB -Confirm:$false  
 }  

vFRC cmdlet used here is:

Set-HardDiskVFlashConfiguration

which enable vFRC on a given virtual machine disk.







Thats' all!!

Nessun commento:

Posta un commento