Developer | Gamer | Bogan
 

Configuring Windows NLB with PowerShell (aka. my AutoSPInstaller bit)

For the benefit of anyone who doesn’t know what NLB is, I’m talking about Windows Network Load Balancing. Basically it is a software based load balancing tool that has been built in to Windows Server for many, many moons. It is a reasonably common thing to see in a SharePoint environment as a way of balancing load across multiple web front end servers. What I'm covering here is how you can use PowerShell to do the setup and config for you, and how I've tied it in to AutoSPInstaller.

So my brief intro to NLB aside, one of the things I have been doing a bit of work with lately is around automating my SharePoint installations with PowerShell. I wasn’t particularly fond of the idea of starting from scratch, so I went and grabbed myself a copy of the AutoSPInstaller script from CodePlex which worked a treat. Next on my to-do list though was to do a few things to extend it, top of the list was to add my NLB configuration to the mix. So I wrote it, did some basic testing and then uploaded them as a patch to the CodePlex project (which will hopefully get included in an upcoming release) and though I would write a quick post to explain the  script and how it can be configured. So first up, the function itself:

# ===================================================================================
# FUNC: Configure network Load Balancing
# DESC: Configures Windows NLB based on the XML configuration file
# ===================================================================================
function ConfigureNLB([xml]$xmlinput)
{
    if ($xmlinput.Configuration.LoadBalancing -ne $null)
    {
        Write-Host "Begining NLB Configuration" -ForegroundColor White

    $fqdn = $xmlinput.Configuration.LoadBalancing.FQDN
        $interfaceName = $xmlinput.Configuration.LoadBalancing.InterfaceName
        $virtualIp = $xmlinput.Configuration.LoadBalancing.VirtualIP
        $subnet = $xmlinput.Configuration.LoadBalancing.SubnetMask
        $mode = $xmlinput.Configuration.LoadBalancing.Mode

        ## install the NLB feature
        Write-Host "Ensuring NLB feature is installed ..." -ForegroundColor White
        dism /online /enable-feature /featurename:NetworkLoadBalancingFullServer
        Write-Host "NLB installation complete" -ForegroundColor White

    Write-Host "Attempting to locate existing cluster ..."
    Import-Module NetworkLoadBalancingClusters

        $alreadyConfigured = $true
        try
        {
            $localCluster = Get-NlbCluster .
        }
        catch
        {
            $alreadyConfigured = $false
        }

        if ($alreadyConfigured -eq $false)
        {
            $cluster = Get-NlbCluster -HostName $fqdn -ErrorAction SilentlyContinue

            if ($cluster -eq $null)
            {
                ## The cluster does not exist, create a new cluster
                Write-Host "No cluster found - creating new NLB cluster..." -ForegroundColor White
                New-NlbCluster -ClusterName $fqdn -InterfaceName $interfaceName -ClusterPrimaryIP $virtualIp -SubnetMask $subnet -OperationMode $mode
                Write-Host "New NLB cluster created" -ForegroundColor White
            }
            else
            {
                ## The cluster already exists, add a new node
                Write-Host "Cluster found - adding node to existing NLB Cluster..."  -ForegroundColor White
                Get-NlbCluster -HostName $fqdn | Add-NlbClusterNode -NewNodeName $env:COMPUTERNAME -NewNodeInterface $interfaceName
                Write-Host "Successfully added new node to NLB cluster" -ForegroundColor White
            }
            Write-Host -ForegroundColor White "Finished configuring load balancing"
        WriteLine
        }
        else
        {
            Write-Host -ForegroundColor White "Load balancing has already been configured. No action taken"
        }
    }
    else
    {
        Write-Host -ForegroundColor White "Load balancing configuration not found, skipping NLB setup"
    WriteLine
    }
}

Now to explain it. The first thing we do is look for some NLB configuration info. This is passed through the main config XML file for AutoSPInstaller,but could be any XML file really. If we find the config,we go and continue, otherwise we bail. Next we use the dism command line tool to install the NLB components to the server – the great thing about this is that if it is already installed it will just simply run and not throw any errors, awesome. After that we get to the NLB specific bits.

We start by importing the NLB specific powershell commands, using the Import-Module cmdlet, calling the “NetworkLoadBalancingClusters” commands. Now at this point if you want a full list of all the NLB specific commands just drop a “get-help nlb” to see the full list.

Now without doing a line by line run through of the script, here is a summary of what we are doing:

  • Check to see if the cluster already exists anywhere on the network
  • If it doesn’t exist, create it now with the appropriate parameters, then finish up
  • If it does exist, the next check is to see if the local server is part of a cluster already (this is the “Get-NlbCluster .” command, with the theory being that if it is part of a cluster already, the config has already been done and we can finish up, and if it hasn’t we can just add the current server to the existing cluster

The last thing I’ll include here is the line of XML I added to the config file:

<LoadBalancing InterfaceName="Load Balanced NIC"
       FQDN="fsintranet"
       VirtualIP="192.168.137.130"
       SubnetMask="255.255.255.0"
       Mode="Multicast" />

Now if you want to take this script away from the AutoSPInstaller context - just look at the main part of the function and swap your own values into the lines where I set the values such as $fqdn, $subnet and those. The only real link into AutoSPInstaller is that reading of that XML from the config file, the rest stands on its own.

And that’s it! I hope it helps people, and I hope it gets rolled in to an upcoming AutoSPInstaller release.

sharepointwindows-serverPowerShellNLB
Posted by: Brian Farnhill
Last revised: 24 Jan, 2012 11:14 AM History

Comments

Martin
Martin
27 Jun, 2011 07:00 AM @ version 0

If you are already using powershell, why use DISM to install a feature (line 19) and not use the ServerManager module?

Import-Module ServerManager Add-WindowsFeature NLB, RSAT-NLB

BrianFarnhill
BrianFarnhill
28 Jun, 2011 01:33 PM @ version 0

Because I didn''t even think of that :-) Great idea, definitely a better way to do it!

Your Comments

Used for your gravatar. Not required. Will not be public.
Posting code? Indent it by four spaces to make it look nice. Learn more about Markdown.

Preview