Create-NLBCluster

Network Load Balancing (NLB) is an optional feature in Windows Server® 2008 that load balances network traffic (sent to a cluster virtual IP address) among multiple servers in an NLB cluster. NLB is particularly useful for ensuring that stateless applications, such as a Web server running Internet Information Services (IIS), are scalable by adding additional servers as the load increases.

Network Load Balancing (NLB) didn’t get a chance to have a new mmc to manage it in Windows 2008; we still have to use the old one inherited from Windows 2003. No way! Now we have a complete set of cmdlets, let’s use them.

Param(
    [Parameter(Mandatory=$true, HelpMessage="Interface cards should be named the same on all nodes wannabe and have a fixed IP.")]
    $InterfaceName,
    [Parameter(Mandatory=$true)]
    $ClusterName,
    [Parameter(Mandatory=$true, HelpMessage="Virtual IP that the cluster will use to load-balance the requests between all nodes.")]
    $ClusterIpAddress,
    [Parameter(Mandatory=$true, HelpMessage="Virtual IP subnet mask that the cluster will use.")]
    $ClusterSubnet,
    [Parameter(Mandatory=$true, HelpMessage="The list of the nodes to add in the cluster.")]
    [string[]]$NodesList,
    [Parameter(Mandatory=$true, HelpMessage="The list of the ports (TCP) to load-balance between all nodes.")]
    [string[]]$PortsList
)

    Try {
        Import-Module NetworkLoadBalancingClusters
        }
    Catch {
        $_.Exception.Message
        Exit
        }

if (!(Get-NlbCluster -HostName $ClusterIpAddress -ErrorAction SilentlyContinue))
{
    Write-Host "`nCreating NLB Cluster..." -ForegroundColor Yellow
    Write-Host "Cluster Name: $ClusterName"
    Write-Host "Cluster VIP: $ClusterIpAddress"

    Try {
        New-NlbCluster -InterfaceName $InterfaceName -ClusterName $ClusterName -ClusterPrimaryIP $ClusterIpAddress -SubnetMask $ClusterSubnet -OperationMode Multicast
        }
    Catch {
        $_.Exception.Message
        Exit
        }
    Write-Host "`nNLB Cluster $ClusterName created successfully.`n" -ForegroundColor Green
    
    Write-Host "Removing default port rules..." -ForegroundColor Yellow 
    Try {
        Get-NlbClusterPortRule | Remove-NlbClusterPortRule -Force
        }
    Catch {
        $_.Exception.Message
        Exit
        }
    Write-Host "Default port rules removed successfully.`n" -ForegroundColor Green

    ForEach ($Port in $PortsList) {
        Write-Host "Adding the new port rules..." -ForegroundColor Yellow 
        Try {
            Get-NlbCluster | Add-NlbClusterPortRule -StartPort $Port -EndPort $Port -Protocol TCP -Affinity None | Out-Null
            Write-Host "Port rule StartPort TCP ${Port}-${Port}) added successfully." -ForegroundColor Green
            }
        Catch {
            $_.Exception.Message
            Exit
            } 
    } # EndOf ForEach ($Port in $PortsList)
}
else { Get-NlbCluster }

ForEach ($Node in $NodesList) {
        Write-Host "`nAdding the node(s) in the cluster $ClusterName..." -ForegroundColor Yellow 
        if(!(Get-NlbClusterNode -HostName $Node -ErrorAction SilentlyContinue))
        {
        Try {
            Get-NlbCluster -HostName $ClusterIpAddress | Add-NlbClusterNode -NewNodeName $Node -NewNodeInterface $InterfaceName
            Write-Host "Node $Node added successfully." -ForegroundColor Green
            }
        Catch {
            $_.Exception.Message
            Exit
            }
        } # EndOf if(!(Get-NlbClusterNode -HostName $Name))
    } # EndOf ForEach ($Node in $NodesList)

Leave a Reply

Scroll to Top