Advertisement
Guest User

Untitled

a guest
Nov 15th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.15 KB | None | 0 0
  1. ####################################################################################
  2. # #
  3. # VMware Virtual SAN -- All Flash Automated Deployment for Stretched Cluster #
  4. # #
  5. ####################################################################################
  6.  
  7. ####################################################################################
  8. # #
  9. # The All-in-One and Ultimate Virtual SAN Streched Cluster Config. Script #
  10. # #
  11. # by Alan Renouf and Rawlinson #
  12. # #
  13. ####################################################################################
  14.  
  15. # -- Add all VMware PowerCLI modules --
  16.  
  17. get-module -ListAvailable VMware* | Import-Module | Out-Null
  18.  
  19. # -- Infrastructure Settings for scripts --
  20.  
  21. $VCNode = "vcenter-fqdn-or-ip"
  22. $VCUserName = "administrator@vsphere.local"
  23. $VCPassword = "password"
  24. $ESXiUserName = "root"
  25. $ESXiPassword = 'password'
  26. $DCName = "octo-sabu"
  27. $CluName = "hci-stretched-cluster"
  28. $VDSName = "10G Switch"
  29. $PrimaryPortGroup = 3001
  30. $SecondaryPortgroup = 3003
  31. $WitnessPortgroup = 3002
  32. $DNS = "10.142.7.21", "10.142.7.22"
  33. $NTP = "10.132.249.12", "10.132.249.28"
  34. $VMotionIP = "192.168.21."
  35. $VSANIP = "192.168."
  36. $cachingSSD = "S630DC-960"
  37. $CapacitySSD = "MICRON_M510DC_MT"
  38. $witness = "VSAN-witness-fqdn-or-ip"
  39. $witnessIP = "192.168.2.68"
  40. $WitnessSNM = "255.255.255.0"
  41.  
  42. # -- Static Routes --
  43.  
  44. $PrimarySR = "192.168.1.0"
  45. $PrimaryGW = "192.168.1.253"
  46.  
  47. $SecondarySR = "192.168.3.0"
  48. $SecondaryGW = "192.168.3.253"
  49.  
  50. $WitnessSR = "192.168.2.0"
  51. $WitnessGW = "192.168.2.253"
  52.  
  53. # -- Connect to vCenter --
  54.  
  55. Connect-viserver $VCNode -user $VCUserName -pass $VCPassword -WarningAction SilentlyContinue
  56.  
  57. # -- Datacenter/Cluster Configuration --
  58.  
  59. # -- Create Datacenters --
  60.  
  61. Write-Host "Creating Datacenter: $DCName" -ForegroundColor Green
  62. $DC = New-Datacenter -Name $DCName -Location (Get-Folder Datacenters)
  63.  
  64. # -- Adding and configuring the Virtual SAN Witness Appliance to vCenter
  65.  
  66. Write-Host "Creating Witness Datacenter: witness-$($DCName)" -ForegroundColor Green
  67. $WDC = New-Datacenter -Name "Witness-$($DCName)" -Location (Get-Folder Datacenters)
  68. Write-Host "Adding Witness host $Witness" -ForegroundColor Green
  69. Add-VMHost -Name $witness -Location $WDC -User $ESXiUserName -Password $ESXiPassword -Force | Out-Null
  70. Write-Host "Adding Witness IP Address" -ForegroundColor Green
  71. $WVMK = get-vmhost $witness | Get-VMHostNetworkAdapter | Where { $_.dhcpEnabled -eq $true}
  72. $WVMK | Set-VMHostNetworkAdapter -IP $witnessIP -SubnetMask $WitnessSNM -Confirm:$false | Out-Null
  73.  
  74. # -- Create Cluster --
  75.  
  76. Write-Host "Creating Cluster: $CluName" -ForegroundColor Green
  77. $CLU = New-Cluster -Name $CluName -Location ($DC) -DrsEnabled
  78.  
  79. # -- Add Hosts to cluster --
  80.  
  81. 09..16 | Foreach {
  82. $num = $_ ;
  83. $newnum = "{0:D2}" -f $num
  84. Write-Host "Adding host hostname-$newnum.vmware.com" -ForegroundColor Green
  85. Add-VMHost -Name "hostname-$newnum.vmware.com" -Location $CluName -User $ESXiUserName -Password $ESXiPassword -Force | Out-Null
  86. }
  87.  
  88. # -- Host Configuration --
  89.  
  90. $VMHosts = Get-VMHost | Sort Name
  91.  
  92. # -- Add DNS/NTP and Enable iScsi Settings for the hosts --
  93.  
  94. Foreach ($vmhost in $vmhosts) {
  95. Write-Host "Configuring DNS and Domain Name on $vmhost" -ForegroundColor Green
  96. Get-VMHostNetwork -VMHost $vmhost | Set-VMHostNetwork -DNSAddress $DNS -Confirm:$false | Out-Null
  97.  
  98. Write-Host "Configuring NTP Servers on $VMHost" -ForegroundColor Green
  99. Add-VMHostNTPServer -NtpServer $NTP -VMHost $VMHost -Confirm:$false -ErrorAction SilentlyContinue | FT | Out-Null
  100.  
  101. Write-Host "Configuring NTP Client Policy on $VMHost" -ForegroundColor Green
  102. Get-VMHostService -VMHost $VMHost | where {$_.Key -eq "ntpd"} | Set-VMHostService -policy "on" -Confirm:$false | FT | Out-Null
  103.  
  104. Write-Host "Restarting NTP Client on $VMHost" -ForegroundColor Green
  105. Get-VMHostService -VMHost $VMHost | where {$_.Key -eq "ntpd"} | Restart-VMHostService -Confirm:$false | FT | Out-Null
  106.  
  107. }
  108.  
  109.  
  110. # -- Network Configuration --
  111.  
  112.  
  113. # -- Create DVSwitch --
  114.  
  115. Write-Host "Creating VDSwitch: $VDSName" -ForegroundColor Green
  116. $VDS = New-VDSwitch -Name $VDSName -NumUplinkPorts 2 -Location $DC -Mtu 9000 -Version "6.0.0"
  117.  
  118. # -- Create Portgroups --
  119.  
  120. Write-Host "Creating PortGroup: VSAN Network $PrimaryPortGroup" -ForegroundColor Green
  121. New-VDPortgroup -Name "VSAN Network $PrimaryPortGroup" -Vds $vds -VlanId $PrimaryPortGroup | Out-Null
  122. Write-Host "Creating PortGroup: VSAN Network $SecondaryPortGroup" -ForegroundColor Green
  123. New-VDPortgroup -Name "VSAN Network $SecondaryPortGroup" -Vds $vds -VlanId $SecondaryPortGroup | Out-Null
  124. Write-Host "Creating PortGroup: VSAN Network $WitnessPortGroup" -ForegroundColor Green
  125. New-VDPortgroup -Name "VSAN Network $WitnessPortGroup" -Vds $vds -VlanId $WitnessPortGroup | Out-Null
  126. Write-Host "Creating vMotion Network 3021" -ForegroundColor Green
  127. New-VDPortgroup -Name "vMotion Network 3021" -VDSwitch $vds -VlanId 3021 | Out-Null
  128.  
  129.  
  130. # -- Add Hosts to VDSWitch and Migrate pNIC to VDS (vmnic2/vmnic3) --
  131.  
  132. Foreach ($vmhost in ($DC | Get-VmHost)) {
  133. Write-Host "Adding $VMHost to $VDSName" -ForegroundColor Green
  134. $vds | Add-VDSwitchVMHost -VMHost $vmhost | Out-Null
  135. $vmhostNetworkAdapter = Get-VMHost $vmhost | Get-VMHostNetworkAdapter -Physical -Name vmnic2
  136. Write-Host "Adding $VMHostNetworkAdapter to $VDSName" -ForegroundColor Green
  137. $vds | Add-VDSwitchPhysicalNetworkAdapter -VMHostNetworkAdapter $vmhostNetworkAdapter -Confirm:$false | Out-Null
  138. $vmhostNetworkAdapter = Get-VMHost $vmhost | Get-VMHostNetworkAdapter -Physical -Name vmnic3
  139. Write-Host "Adding $VMHostNetworkAdapter to $VDSName" -ForegroundColor Green
  140. $vds | Add-VDSwitchPhysicalNetworkAdapter -VMHostNetworkAdapter $vmhostNetworkAdapter -Confirm:$false | Out-Null
  141. }
  142.  
  143. # -- Set DVUplink2 to standby --
  144.  
  145. $TeamingPolicys = $vds | Get-VDPortgroup VSAN* | Get-VDUplinkTeamingPolicy
  146. Foreach ($Policy in $TeamingPolicys) {
  147. Write-Host "Setting Standby Uplink for $($Policy.VDPortGroup)" -ForegroundColor Green
  148. $Policy | Set-VDUplinkTeamingPolicy -StandbyUplinkPort "dvUplink2" | Out-Null
  149. }
  150.  
  151.  
  152. # -- Create vMotion VMKernel Ports for all hosts in DC --
  153.  
  154. foreach ($vmhost in ($DC | Get-VmHost)) {
  155. $HostIP = ($vmhost | Get-VMHostNetworkAdapter -Name vmk0).ip
  156. $LastO = $HostIP.Split(".")[3]
  157. $VSANNet = Get-VDPortGroup "VSAN Network $PrimaryPortGroup"
  158. $3rdO = ($VSANNet.Name).Substring(16)
  159. $CurrentvMotionIP = $vMotionIP + $LastO
  160. Write-Host "Adding vMotion Network Adapter to $VMHost with IP of $CurrentvMotionIP" -ForegroundColor Green
  161. New-vmhostnetworkadapter -VMHost $vmhost -PortGroup "vMotion Network 3021" -VirtualSwitch $vds -VMotionEnabled $true -IP $CurrentvMotionIP -SubnetMask "255.255.255.0" | Out-Null
  162.  
  163. }
  164.  
  165. #Adding Static Routes for Virtual SAN Streched Cluster
  166.  
  167. # -- Primary site --
  168.  
  169. 09..12 | Foreach {
  170. $num = $_
  171. $newnum = "{0:D2}" -f $num
  172. $VMHost = "hostname-$newnum.vmware.com"
  173. $HostIP = (Get-VMHost $vmhost | Get-VMHostNetworkAdapter -Name vmk0).ip
  174. $LastO = $HostIP.Split(".")[3]
  175. $VSANNet = Get-VDPortGroup "VSAN Network $PrimaryPortGroup"
  176. $3rdO = ($VSANNet.Name).Substring(16)
  177. $CurrentVSANIP = $VSANIP + $3rdO + "." + $LastO
  178. Write-Host "Adding $CurrentVSANIP to $($VSANNet.Name) and enabling VSAN traffic" -ForegroundColor Green
  179. $VSANVMK = New-vmhostnetworkadapter -VMHost $vmhost -PortGroup $VSANNet.Name -VirtualSwitch $vds -VsanTrafficEnabled $true -IP $CurrentVSANIP -SubnetMask "255.255.255.0"
  180. Write-Host "Adding Static Routes for primary Site to host hostname-$newnum.vmware.com" -ForegroundColor Green
  181. New-VMHostRoute -VMHost $VMHost -Destination $SecondarySR -Gateway $PrimaryGW -PrefixLength 24 -Confirm:$false | Out-Null
  182. New-VMHostRoute -VMHost $VMHost -Destination $WitnessSR -Gateway $PrimaryGW -PrefixLength 24 -Confirm:$false | Out-Null
  183. if (-not $1PrimaryIP) {
  184. $1PrimaryIP = (Get-VMHost $VMHost | Get-VMHostNetworkAdapter -Name $VMKnet).IP
  185. }
  186. }
  187.  
  188. # -- Secondary Site --
  189.  
  190. 13..16 | Foreach {
  191. $num = $_ ;
  192. $newnum = "{0:D2}" -f $num
  193. $VMHost = "hostname-$newnum.vmware.com"
  194. $HostIP = (Get-VMHost $vmhost | Get-VMHostNetworkAdapter -Name vmk0).ip
  195. $LastO = $HostIP.Split(".")[3]
  196. $VSANNet = Get-VDPortGroup "VSAN Network $SecondaryPortGroup"
  197. $3rdO = ($VSANNet.Name).Substring(16)
  198. $CurrentVSANIP = $VSANIP + $3rdO + "." + $LastO
  199. Write-Host "Adding $CurrentVSANIP to $($VSANNet.Name) and enabling VSAN traffic" -ForegroundColor Green
  200. $VSANVMK = New-vmhostnetworkadapter -VMHost $vmhost -PortGroup $VSANNet.Name -VirtualSwitch $vds -VsanTrafficEnabled $true -IP $CurrentVSANIP -SubnetMask "255.255.255.0"
  201. Write-Host "Adding Static Routes for Secondary Site to host hostname-$newnum.vmware.com" -ForegroundColor Green
  202. New-VMHostRoute -VMHost $VMHost -Destination $PrimarySR -Gateway $SecondaryGW -PrefixLength 24 -Confirm:$false | Out-Null
  203. New-VMHostRoute -VMHost $VMHost -Destination $WitnessSR -Gateway $SecondaryGW -PrefixLength 24 -Confirm:$false | Out-Null
  204. if (-not $1SecondaryIP) {
  205. $1SecondaryIP = (Get-VMHost $VMHost | Get-VMHostNetworkAdapter -Name $VMKnet).IP
  206. }
  207. }
  208.  
  209. # -- witness Site --
  210.  
  211. Write-Host "Adding Static Routes to Witness" -ForegroundColor Green
  212. New-VMHostRoute -VMHost $witness -Destination $PrimarySR -Gateway $WitnessGW -PrefixLength 24 -Confirm:$false | Out-Null
  213. New-VMHostRoute -VMHost $witness -Destination $SecondarySR -Gateway $WitnessGW -PrefixLength 24 -Confirm:$false | Out-Null
  214.  
  215.  
  216. # -- communication validation between all Hosts and networks --
  217.  
  218. # -- primary site --
  219.  
  220. 09..12 | Foreach {
  221. $num = $_ ;
  222. $newnum = "{0:D2}" -f $num
  223. $VMHost = "hostname-$newnum.vmware.com"
  224. $VMKnet = (Get-VMHostNetworkAdapter -VMHost $vmhost -PortGroup "VSAN*").Name
  225. Write-Host "Pinging $SecondarySR from $VMHost on $vmknet..." -ForegroundColor Green
  226. $esxcli = Get-ESXCLI -VMhost $vmhost
  227. $ping = $esxcli.network.diag.ping(2,$null,$null,$1SecondaryIP,$vmknet,$null,$null,$null,$null,$null,$null,$null,$null) | select -expand Summary
  228. if ($ping.Recieved -ge 1) {
  229. Write-Host "Ping of Secondary: $1SecondaryIP Tested OK" -ForegroundColor Yellow
  230. } Else {
  231. Write-Host "Ping of Secondary: $1SecondaryIP Failed" -ForegroundColor Red
  232. }
  233. Write-Host "Pinging $WitnessSR from $VMHost on $VMKnet..." -ForegroundColor Green
  234. $ping = $esxcli.network.diag.ping(2,$null,$null,$WitnessIP,$vmknet,$null,$null,$null,$null,$null,$null,$null,$null) | select -expand Summary
  235. if ($ping.Recieved -ge 1) {
  236. Write-Host "Ping of Witness: $WitnessSR Tested OK" -ForegroundColor Yellow
  237. } Else {
  238. Write-Host "Ping of Witness: $WitnessSR Failed" -ForegroundColor Red
  239. }
  240. }
  241.  
  242. # -- secondary site --
  243.  
  244. 13..16 | Foreach {
  245. $num = $_ ;
  246. $newnum = "{0:D2}" -f $num
  247. $VMHost = "hostname-$newnum.vmware.com"
  248. $VMKnet = (Get-VMHostNetworkAdapter -VMHost $vmhost -PortGroup "VSAN*").Name
  249. Write-Host "Pinging $PrimarySR from $VMHost on $vmknet..." -ForegroundColor Green
  250. $esxcli = Get-ESXCLI -VMhost $vmhost
  251. $ping = $esxcli.network.diag.ping(2,$null,$null,$1PrimaryIP,$vmknet,$null,$null,$null,$null,$null,$null,$null,$null) | select -expand Summary
  252. if ($ping.Recieved -ge 1) {
  253. Write-Host "Ping of Primary: $1PrimaryIP Tested OK" -ForegroundColor Yellow
  254. } Else {
  255. Write-Host "Ping of Primary: $1PrimaryIP Failed" -ForegroundColor Red
  256. }
  257. Write-Host "Pinging $WitnessSR from $VMHost on $vmknet..." -ForegroundColor Green
  258. $ping = $esxcli.network.diag.ping(2,$null,$null,$WitnessIP,$vmknet,$null,$null,$null,$null,$null,$null,$null,$null) | select -expand Summary
  259. if ($ping.Recieved -ge 1) {
  260. Write-Host "Ping of Witness: $WitnessSR Tested OK" -ForegroundColor Yellow
  261. } Else {
  262. Write-Host "Ping of Witness: $WitnessSR Failed" -ForegroundColor Red
  263. }
  264. }
  265.  
  266. # -- witness site --
  267.  
  268. $VMKnet = (Get-VMHostNetworkAdapter -VMHost $witness -PortGroup "VSAN*").Name
  269. Write-Host "Pinging $PrimarySR from $witness on $vmknet..." -ForegroundColor Green
  270. $esxcli = Get-ESXCLI -VMhost $witness
  271. $Ping = $esxcli.network.diag.ping(2,$null,$null,$1PrimaryIP,$vmknet,$null,$null,$null,$null,$null,$null,$null,$null) | select -expand Summary
  272. if ($ping.Recieved -ge 1) {
  273. Write-Host "Ping of Primary: $1PrimaryIP Tested OK" -ForegroundColor Yellow
  274. } Else {
  275. Write-Host "Ping of Primary: $1PrimaryIP Failed" -ForegroundColor Red
  276. }
  277. Write-Host "Pinging $SecondarySR from $witness on $vmknet..." -ForegroundColor Green
  278. $ping = $esxcli.network.diag.ping(2,$null,$null,$1SecondaryIP,$vmknet,$null,$null,$null,$null,$null,$null,$null,$null) | select -expand Summary
  279. if ($ping.Recieved -ge 1) {
  280. Write-Host "Ping of Secondary: $1SecondaryIP Tested OK" -ForegroundColor Yellow
  281. } Else {
  282. Write-Host "Ping of Secondary: $1SecondaryIP Failed" -ForegroundColor Red
  283. }
  284.  
  285. # -- enable Virtual SAN
  286.  
  287. Write-Host "Enabling VSAN in manual mode for Cluster: $CLU" -ForegroundColor Green
  288. $CLU = $CLU | Set-Cluster -VsanEnabled:$true -VsanDiskClaimMode Manual -Confirm:$false -ErrorAction SilentlyContinue
  289.  
  290. # -- claim and tag caching and capacity flash devices
  291.  
  292. Foreach ($vmh in ($DC | Get-VmHost)) {
  293. Write-Host "Finding disks for $($vmh)"
  294. $esxcli = Get-ESXCLI -VMhost $vmh
  295. 0..1 | Foreach {
  296. $DiskgroupNum = $_ +1
  297. $Caching = ($esxcli.storage.core.device.list() | Where {$_.model -eq $cachingSSD})[$_]
  298. $Capacity = ($esxcli.storage.core.device.list() | Where {$_.model -eq $capacitySSD})[$_]
  299. Write-Host "Using $($caching.Vendor) - $($caching.Model) for Caching in Disk Group $Diskgroupnum" -foregroundColor Green
  300. Write-Host "Using $($Capacity.Vendor) - $($Capacity.Model) for Capacity in Disk Group $Diskgroupnum" -foregroundColor Green
  301. Write-Host "Tagging $($Capacity.Model) as Capacity"
  302. $capacitytag = $esxcli.vsan.storage.tag.add(($capacity.Device), "capacityFlash")
  303. Write-Host "Adding Storage devices to $($vmhost)"
  304. $adddisks = $esxcli.vsan.storage.add(($capacity.device), ($Caching.device))
  305. if ($adddisks -eq "true") {
  306. Write-Host "Disks added" -ForegroundColor Green
  307. } Else {
  308. Write-Host "Error adding disks: $adddisks" -ForegroundColor Red
  309. }
  310. }
  311. Write-Host "Enable space efficiency sparse swap on $($vmh)" -ForegroundColor Green
  312. Get-AdvancedSetting -Entity $vmh -Name "VSAN.SwapThickProvisionDisabled" | Set-AdvancedSetting -Value 1 -Confirm:$false | out-null
  313.  
  314. }
  315.  
  316. # -- Virtual SAN cluster configuration validation
  317.  
  318. If ($CLU.VSANEnabled){
  319. Write-Host "VSAN cluster $($CLU.Name) created in $($CLU.VSANDiskClaimMode) configuration" -ForegroundColor Yellow
  320. Write-Host "The following Hosts and Disk Groups now exist:"
  321. Get-VsanDiskGroup | Select VMHost, Name | FT -AutoSize
  322. Write-Host "The following VSAN Datastore now exists:"
  323. Get-Datastore | Where {$_.Type -eq "vsan"} | Select Name, Type, FreeSpaceGB, CapacityGB
  324. } Else {
  325. Write-Host "Something went wrong, Virtual SAN not enabled"
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement