Skip to content

Instantly share code, notes, and snippets.

@alanrenouf
Created November 15, 2016 06:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanrenouf/89e5df93c48c398976952a2aa4a7aa3b to your computer and use it in GitHub Desktop.
Save alanrenouf/89e5df93c48c398976952a2aa4a7aa3b to your computer and use it in GitHub Desktop.
The All-in-One Virtual SAN Deployment Script
###########################################################
# #
# VMware Virtual SAN -- Making Storage Great Again #
# #
###########################################################
###########################################################
# #
# The All-in-One Virtual SAN Deployment Script #
# #
# by Alan Renouf and Rawlinson #
# #
###########################################################
# -- Infrastructure Configuration Settings Section --
$VCNode = "vcenter.vsan.com"
$VCUserName = "administrator@vsphere.local"
$VCPassword = "password"
$ESXiUserName = "root"
$ESXiPassword = 'password'
$DCName = "vsan-4-life"
$CluName = "vsan-all-flash"
$VDSName = "10G Switch"
$PortGroupRange = 3006 #3001..3007
$DNS = "10.4.90.21", "10.4.90.22"
$NTP = "10.4.90.12", "10.4.90.28"
$VMotionIP = "192.168.21."
$VSANIP = "192.168."
$iSCSIIP = "192.168.22."
$NFSIP = "192.168.23."
$targets = "192.168.22.213"
$cachingSSD = "S630DC-960"
$CapacitySSD = "MICRON_M510DC_MT"
####################################################################################################
# -- Import PowerCLI Modules --
get-module -ListAvailable VMware* | Import-Module | Out-Null
# -- Connect to vCenter --
Connect-viserver $VCNode -user $VCUserName -pass $VCPassword -WarningAction SilentlyContinue
# -- Datacenter/Cluster Configuration --
# -- Create Datacenter --
Write-Host "Creating Datacenter: $DCName" -ForegroundColor Green
$DC = New-Datacenter -Name $DCName -Location (Get-Folder Datacenters)
# -- Create Cluster --
Write-Host "Creating Cluster: $CluName" -ForegroundColor Green
$CLU = New-Cluster -Name $CluName -Location ($DC) -DrsEnabled
# -- Add Hosts to cluster --
09..16 | Foreach {
$num = $_ ;
$newnum = "{0:D2}" -f $num
Write-Host "Adding host w3-octo-sm-$newnum.eng.vmware.com" -ForegroundColor Green
Add-VMHost -Name "w3-octo-sm-$newnum.eng.vmware.com" -Location $CluName -User $ESXiUserName -Password $ESXiPassword -Force -RunAsync | FT | Out-Null
}
# -- Host Configuration --
Sleep 20
$VMHosts = Get-VMHost | Sort Name
# -- Add DNS/NTP and Enable iScsi Settings for the hosts --
Foreach ($vmhost in $vmhosts) {
Write-Host "Configuring DNS and Domain Name on $vmhost" -ForegroundColor Green
Get-VMHostNetwork -VMHost $vmhost | Set-VMHostNetwork -DNSAddress $DNS -Confirm:$false | FT | Out-Null
Write-Host "Configuring NTP Servers on $VMHost" -ForegroundColor Green
Add-VMHostNTPServer -NtpServer $NTP -VMHost $VMHost -Confirm:$false -ErrorAction SilentlyContinue | FT | Out-Null
Write-Host "Configuring NTP Client Policy on $VMHost" -ForegroundColor Green
Get-VMHostService -VMHost $VMHost | where {$_.Key -eq "ntpd"} | Set-VMHostService -policy "on" -Confirm:$false | FT | Out-Null
Write-Host "Restarting NTP Client on $VMHost" -ForegroundColor Green
Get-VMHostService -VMHost $VMHost | where {$_.Key -eq "ntpd"} | Restart-VMHostService -Confirm:$false | FT | Out-Null
Write-Host "Enabling iSCSI on $VMhost" -ForegroundColor Green
Get-VMHostStorage -VMHost $VMHost | Set-VMHostStorage -SoftwareIScsiEnabled $True | FT | Out-Null
}
# -- DVSwitch Configuration --
# -- Create DVSwitch --
Write-Host "Creating VDSwitch: $VDSName" -ForegroundColor Green
$VDS = New-VDSwitch -Name $VDSName -NumUplinkPorts 2 -Location $DC -Mtu 9000 -Version "6.0.0"
# -- Create Portgroups --
$PortGroupRange | Foreach {
Write-Host "Creating PortGroup: VSAN Network $($_)" -ForegroundColor Green
New-VDPortgroup -Name "VSAN Network $($_)" -Vds $vds -VlanId $_ | FT | Out-Null
}
Write-Host "Creating vMotion Network 3021" -ForegroundColor Green
New-VDPortgroup -Name "vMotion Network 3021" -VDSwitch $vds -VlanId 3021 | FT | Out-Null
Write-Host "Creating ISCSI Network 3022" -ForegroundColor Green
New-VDPortgroup -Name "ISCSI Network 3022" -VDSwitch $vds -VlanId 3022 | FT | Out-Null
Write-Host "Creating NFS Network 3023" -ForegroundColor Green
New-VDPortgroup -Name "NFS Network 3023" -VDSwitch $vds -VlanId 3023 | FT | Out-Null
# -- Add Hosts to VDSWitch and Migrate pNIC to VDS (vmnic2/vmnic3) --
Foreach ($vmhost in $VMHosts) {
Write-Host "Adding $VMHost to $VDSName" -ForegroundColor Green
$vds | Add-VDSwitchVMHost -VMHost $vmhost | FT
$vmhostNetworkAdapter = Get-VMHost $vmhost | Get-VMHostNetworkAdapter -Physical -Name vmnic2
Write-Host "Adding $VMHostNetworkAdapter to $VDSName" -ForegroundColor Green
$vds | Add-VDSwitchPhysicalNetworkAdapter -VMHostNetworkAdapter $vmhostNetworkAdapter -Confirm:$false | FT | Out-Null
$vmhostNetworkAdapter = Get-VMHost $vmhost | Get-VMHostNetworkAdapter -Physical -Name vmnic3
Write-Host "Adding $VMHostNetworkAdapter to $VDSName" -ForegroundColor Green
$vds | Add-VDSwitchPhysicalNetworkAdapter -VMHostNetworkAdapter $vmhostNetworkAdapter -Confirm:$false | FT | Out-Null
}
# -- Set DVUplink2 to standby --
$TeamingPolicys = $vds | Get-VDPortgroup VSAN* | Get-VDUplinkTeamingPolicy
Foreach ($Policy in $TeamingPolicys) {
Write-Host "Setting Standby Uplink for $Policy.VDPortGroup" -ForegroundColor Green
$Policy | Set-VDUplinkTeamingPolicy -StandbyUplinkPort "dvUplink2" | FT | Out-Null
}
# -- Create vMotion, iSCSI and VSAN VMKernel Ports --
foreach ($vmhost in $vmhosts) {
$HostIP = ($vmhost | Get-VMHostNetworkAdapter -Name vmk0).ip
$LastO = $HostIP.Split(".")[3]
$CurrentvMotionIP = $vMotionIP + $LastO
$CurrentiSCSIIP = $iSCSIIP + $LastO
$CurrentNFSIP = $NFSIP + $LastO
Write-Host "Adding vMotion Network Adapter to $VMHost with IP of $CurrentvMotionIP" -ForegroundColor Green
New-vmhostnetworkadapter -VMHost $vmhost -PortGroup "vMotion Network 3021" -VirtualSwitch $vds -VMotionEnabled $true -IP $CurrentvMotionIP -SubnetMask "255.255.255.0" | FT | Out-Null
Write-Host "Adding iSCSI Network Adapter to $VMHost with IP of $CurrentISCSIIP" -ForegroundColor Green
New-vmhostnetworkadapter -VMHost $vmhost -PortGroup "ISCSI Network 3022" -VirtualSwitch $vds -IP $CurrentiSCSIIP -SubnetMask "255.255.255.0" | FT | Out-Null
Write-Host "Adding NFS Network Adapter to $VMHost with IP of $CurrentNFSIP" -ForegroundColor Green
New-vmhostnetworkadapter -VMHost $vmhost -PortGroup "NFS Network 3023" -VirtualSwitch $vds -IP $CurrentNFSIP -SubnetMask "255.255.255.0" | FT | Out-Null
Foreach ($VSANNet in (Get-VDPortGroup VSAN*)){
$3rdO = ($VSANNet.Name).Substring(16)
$CurrentVSANIP = $VSANIP + $3rdO + "." + $LastO
Write-Host "Adding $CurrentVSANIP to $($VSANNet.Name) and enabling VSAN traffic" -ForegroundColor Green
$VSANVMK = New-vmhostnetworkadapter -VMHost $vmhost -PortGroup $VSANNet.Name -VirtualSwitch $vds -VsanTrafficEnabled $true -IP $CurrentVSANIP -SubnetMask "255.255.255.0"
}
}
# -- iSCSI Configuration --
# -- Configure iSCSI Targets --
Sleep 20
foreach ($VMHost in $VMHosts) {
Write-host "Configuring $vmhost for iScsi" -ForegroundColor Green
$hba = $VMHost | Get-VMHostHba -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"}
If ($hba) {
foreach ($target in $targets) {
Write-Host "Adding $target to $hba" -ForegroundColor Green
New-IScsiHbaTarget -IScsiHba $hba -Address $target | FT | Out-Null
}
} Else {
Write-Host "No iSCSI hba found on $VMHost" -ForegroundColor Green
}
}
Write-Host "Enabling VSAN in manual mode for Cluster: $CLU" -ForegroundColor Green
$CLU = $CLU | Set-Cluster -VsanEnabled:$true -VsanDiskClaimMode Manual -Confirm:$false -ErrorAction SilentlyContinue
Foreach ($vmh in $vmhosts) {
Write-Host "Finding disks for $($vmh)"
$esxcli = Get-ESXCLI -VMhost $vmh
0..1 | Foreach {
$DiskgroupNum = $_ +1
$Caching = ($esxcli.storage.core.device.list() | Where {$_.model -eq $cachingSSD})[$_]
$Capacity = ($esxcli.storage.core.device.list() | Where {$_.model -eq $capacitySSD})[$_]
Write-Host "Using $($caching.Vendor) - $($caching.Model) for Caching in Disk Group $Diskgroupnum" -foregroundColor Green
Write-Host "Using $($Capacity.Vendor) - $($Capacity.Model) for Capacity in Disk Group $Diskgroupnum" -foregroundColor Green
Write-Host "Tagging $($Capacity.Model) as Capacity"
$capacitytag = $esxcli.vsan.storage.tag.add(($capacity.Device), "capacityFlash")
Write-Host "Adding Storage devices to $($vmhost)"
$adddisks = $esxcli.vsan.storage.add(($capacity.device), ($Caching.device))
if ($adddisks -eq "true") {
Write-Host "Disks added" -ForegroundColor Green
} Else {
Write-Host "Error adding disks: $adddisks" -ForegroundColor Red
}
}
Write-Host "Enable space efficiency sparse swap on $($vmh)" -ForegroundColor Green
Get-AdvancedSetting -Entity $vmh -Name "VSAN.SwapThickProvisionDisabled" | Set-AdvancedSetting -Value 1 -Confirm:$false | out-null
}
If ($CLU.VSANEnabled){
Write-Host "VSAN cluster $($CLU.Name) created in $($CLU.VSANDiskClaimMode) configuration" -ForegroundColor Yellow
Write-Host "The following Hosts and Disk Groups now exist:"
Get-VsanDiskGroup | Select VMHost, Name | FT -AutoSize | Out-Null
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, Virtual SAN not enabled"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment