Creating a manual VSAN Cluster with PowerCLI

In my previous post I showed how to create a datacenter, cluster, add hosts and create an automatic VSAN cluster allowing for the local SSDs and HDDs to be added to a new VSAN datastore, check it out here.

But what if you wanted to create a manual VSAN cluster and select the disks to add to the VSAN Diskgroup to create the datastore, this gives more control and allows for a precisely configured environment making the most of local resources and perhaps saving other disks for further use and alternate features.

In the below code you can see that instead of automatic we can easily set the cluster to manual and add the disks ourselves, with my code I have selected the disks I want to use as all blank SSDs and all HDDs but you could easily use PowerCLI to define the make, model, size or other factors to use before creating the diskgroup and manually adding the SSDs and HDDs.

Output

SNAGHTML15bb0de

The script

import-module VMware.VimAutomation.Extensions
Connect-VIServer 172.16.78.191 -User root -Password vmware
$Datacenter = "DC01"
$Cluster = "VSAN Cluster"
$ESXHosts = "172.16.78.129", "172.16.78.130"
$ESXUser = "root"
$ESXPWD = "vmware"
$VMKNetforVSAN = "Management Network"

# If doesnt exist create the datacenter
If (-Not ($NewDatacenter = Get-Datacenter $Datacenter -ErrorAction SilentlyContinue)){ 
	Write-Host "Adding $Datacenter"
	$NewDatacenter = New-Datacenter -Name $Datacenter -Location (Get-Folder Datacenters) 
}

# Create the initial cluster
if (-Not ($NewCluster = Get-Cluster $Cluster -ErrorAction SilentlyContinue)) { 
	Write-Host "Adding $Cluster"
	$NewCluster = New-Cluster -Name $Cluster -Location $NewDatacenter 
}

# For each of our hosts
$ESXHosts | Foreach {
	Write-Host "Adding $($_) to $($NewCluster)"
	# Add them to the cluster
	$AddedHost = Add-VMHost -Name $_ -Location $NewCluster -User $ESXUser -Password $ESXPWD -Force
	# Check to see if they have a VSAN enabled VMKernel
	$VMKernel = $AddedHost | Get-VMHostNetworkAdapter -VMKernel | Where {$_.PortGroupName -eq $VMKNetforVSAN }
	$IsVSANEnabled = $VMKernel | Where { $_.VsanTrafficEnabled}
	# If it isnt Enabled then Enable it
	If (-not $IsVSANEnabled) {
		Write-Host "Enabling VSAN Kernel on $VMKernel"
		$VMKernel | Set-VMHostNetworkAdapter -VsanTrafficEnabled $true -Confirm:$false | Out-Null
	} Else {
		Write-Host "VSAN Kernel already enabled on $VmKernel"
		$IsVSANEnabled | Select VMhost, DeviceName, IP, PortGroupName, VSANTrafficEnabled
	}
}
# Enable VSAN on the cluster and set to Automatic Disk Claim Mode
Write-Host "Enabling VSAN on $NewCluster"
$VSANCluster = $NewCluster | Set-Cluster -VsanEnabled:$true -VsanDiskClaimMode Manual -Confirm:$false -ErrorAction SilentlyContinue

$ESXHosts | Foreach {
	Write-Host "Finding disks for $($_)"
	# Find the blank SSDs for the current host
	$disks = Get-VMHost $_ | Get-VMHostDisk
	$SSDs = $disks | Where { $_.scsilun.extensiondata.ssd }
	$BlankSSDs = $SSDs | Where { -not $_.Extensiondata.Layout.Partition[0].partition }
	Write-Host "Blank SSDs"
	$BlankSSDsArray = ""
	$BlankSSDs | Foreach { $BlankSSDsArray += $_.scsilun.CanonicalName }
	$BlankSSDsArray
	
	# Find the blank Magnetic disks for the current host
	$HDDs = $disks | Where { -not $_.scsilun.extensiondata.ssd }
	$BlankHDDs = $HDDs | Where { -not $_.Extensiondata.Layout.Partition[0].partition }
	Write-Host "Blank HDDs"
	$BlankHDDsArray = ""
	$BlankHDDs | Foreach { $BlankHDDsArray += $_.scsilun.CanonicalName }
	$BlankHDDsArray
	
	New-VsanDiskGroup -VMHost $_ -SSDCanonicalName $BlankSSDsArray -DataDiskCanonicalName $BlankHDDsArray | Out-Null
}

If ($VSANCluster.VSANEnabled){
	Write-Host "VSAN cluster $($VSANCLuster.Name) created in $($VSANCluster.VSANDiskClaimMode) configuration"
	Write-Host "The following Hosts and Disk Groups now exist:"
	Get-VsanDiskGroup | Select VMHost, Name | FT -AutoSize
	Write-Host "The following VSAN Datastore now exists:"
	Get-Datastore | Where {$_.Type -eq "vsan"} | Select Name, Type, FreeSpaceGB, CapacityGB
} Else {
	Write-Host "Something went wrong, VSAN not enabled"
}

4 thoughts on “Creating a manual VSAN Cluster with PowerCLI”

  1. Hey Alan,

    I know this is an old example, but should
    $BlankSSDsArray = “”
    be
    $BlankSSDsArray = @()

    and

    $BlankHDDsArray = “”
    be
    $BlankHDDsArray = @()

    I was testing the above and using “” resulted in the individual disk names being concatenated together without spaces.

    Also, does checking for the lack of partition 0 still mean a blank disk? I had a VSAN just setup on 6.2 and none of them had partition 0 but were already claimed (partition 1 and 2 i believe).

    Thanks,
    -Chris

Leave a Reply