Question

I have been trying to write a powershell program that checks to see if a cluster exists. If it doesn't then it creates it and adds itself to it. If another computer wakes up, it checks to see if the cluster exists and if it does, then it adds itself to the cluster.

I'm having trouble trying to get a reference to the cluster object from the cluster ip address. Every node knows its address and the cluster address. I want to avoid every node having a list of all the other nodes in its cluster.

I'm finding that I need to sight the non-cluster ip address to get-nlbcluster to work. Specifying the cluster ip address just errors.

Is there any way I can do this, without having to update this list on every node each time I add or remove nodes from the cluster. I suppose I also want to avoid the situation where a node wakes up and has to poll each of the machines in the "master" list looking for one that is up in order to add itself to the cluster.

Was it helpful?

Solution

The following script can be run on all nodes in your cluster, if the cluster doesn't exist then create it, otherwise just add the current computer to the existing cluster. All you need to do is ensure that all your computers in the cluster have a dedicated card with the same name. In the example below the network card is named 'NLB'.

Import-Module ServerManager

# Interface cards should be named the same and have a fixed IP
$interfaceName = "NLB"
$clusterName = "NLB-Cluster"
$clusterIpAddress = "1.2.3.0"
$clusterSubnet = "255.0.0.0"

# Install Network Load Balancing and Tools
Write-Host "Install Network Load Balancing and Tools"
Add-WindowsFeature NLB, RSAT-NLB
Import-Module NetworkLoadBalancingClusters

# If the cluster hasn't been created yet then create it
if (!(Get-NlbCluster -HostName $clusterIpAddress -ErrorAction SilentlyContinue))
{
    Write-Host "Creating NLB Cluster: $clusterName" -ForegroundColor yellow 

    # Create Cluster (default unicast)
    New-NlbCluster -InterfaceName $interfaceName -ClusterName $clusterName -ClusterPrimaryIP $clusterIpAddress -SubnetMask $clusterSubnet 

    # Remove defaults
    Write-Host "Removing default port rules" -ForegroundColor yellow 
    Get-NlbClusterPortRule | Remove-NlbClusterPortRule -Force

    # Create port rules
    Get-NlbCluster | Add-NlbClusterPortRule -StartPort 80 -EndPort 80 -Protocol TCP -Affinity None | Out-Null
    Get-NlbCluster | Add-NlbClusterPortRule -StartPort 443 -EndPort 443 -Protocol TCP -Affinity None | Out-Null 
}
else
{
    Get-NlbCluster 
}

# if this node isn't already a member of a cluster then add it
if(!(Get-NlbClusterNode -HostName $env:COMPUTERNAME))
{
    # Add node to cluster
    Write-Host "Adding node to cluster: $clusterName" -ForegroundColor yellow 
    Get-NlbCluster -HostName $clusterIpAddress | Add-NlbClusterNode -NewNodeName $env:COMPUTERNAME -NewNodeInterface $interfaceName
}
else
{
    Get-NlbClusterNode
}

OTHER TIPS

Does this help? I made it a while ago but never got a chance to completely test it:

#Add a new node to NLB cluster
#Tested with Windows Server 2008 R2 only
#Requires WSManCredSSP Server Role Enabled on cluster Host
Function join-NlbCluster {
    Param(
        [Parameter(Mandatory=$true)]
        $clusterHostname,
        [Parameter(Mandatory=$true)]
        $newNodename,
        [Parameter(Mandatory=$true)]
        $newNodeinterfaceName,
        [Parameter(Mandatory=$true)]
        $userName,
        [Parameter(Mandatory=$true)]
        $password
        )
    Write-Verbose "Verifiying if the remote node has NLB installed"
    If (!((Get-OSFeature -computerName $newNodename -featureName NLB).Installed)) {
        Write-Error "NLB feature is not installed on $newNodename. Cannot continue."
        return $false
    }
    $cmdBlock = "Import-Module networkLoadBalancingClusters
    `$nlbCluster = Get-nlbCluster -HostName $clusterHostName
    if (`$nlbCluster) {
        `$newNode = Add-NlbClusterNode -InputObject `$nlbCluster -NewNodeName $newNodename -NewNodeInterface `"$newNodeinterfaceName`"
        if (`$newNode) {
            Write-Host `"New node is added to cluster`"
            return `$newNode
        } else {
            Write-Host `"Error Creating the NLB Cluster`"
            return `$false
        }
    } else {
        Write-Host `"No NLB cluster found on $clusterHostname`"
        return `$false
    }"

    Write-Verbose $cmdBlock
    $scriptBlock = $ExecutionContext.InvokeCommand.NewScriptBlock($cmdBlock)
    try {
        Write-Verbose "Creating new NLB Cluster"
        Invoke-Command -ComputerName $clusterHostName -ScriptBlock $scriptBlock -HideComputerName -Authentication Credssp -Credential (Get-PSCredential -userName $userName -Password $password)
    }
    catch {
        Write-Verbose $_
        return $false
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top