Just in case some apocalyptic event might occur on one of your servers (Like lets say, a Robot comes from the future and destroys it..) it might be a good thing to create a cluster for the most valuable servers in your data center (Databases, Hyper-V hosts etc…). It will allow you to have data highly available, redundant, and you will be able to plan maintenance tasks without disrupting the users accessing the data. In this blog post I will go through the basic terminology, how does a cluster work in general, and go through some of the tasks that you might want to do on your clustered servers by showing some real life examples. As a bonus, there is a final question that can be fun to answer at the very end of this post. Cheers!
I have been working with Clusters and powershell for a small moment now, and I thought I would share the knowledge I have gained here with you guys. I think this can help others, and perhaps create discussions in order to improve scripts, methods and processes.
I will cover the following points in this blog post:
- Basic cluster concepts and vocabulary
- Gathering information
- Work on VM’s
- Work on cluster and nodes
I will be sharing some parts of a of a script I have been writing in order to demonstrate how easy it became to work with clusters nowadays.
But first thing first, let’s talk about the basic concepts of clusters, and try to bring everybody on the same level of knowledge concerning vocabulary, and cluster concepts.
Failover clustering powershell: The needed vocabulary
Failover Cluster:
The wikipedia definition of cluster is the following:
A cluster is a small group or bunch of something.
Microsoft’s definition resumes it a bit better in our sys engineer words:
A failover cluster is a group of independent computers that work together to increase the availability of applications and services. The clustered servers (called nodes) are connected by physical cables and by software. If one of the cluster nodes fails, another node begins to provide service (a process known as failover). Users experience a minimum of disruptions in service.
In his most simplest configuration, the cluster has generally at least one node, but can have more of course.
A cluster is actually nothing more then a simple computer object in the ActiveDirectory which will abstracts any of the nodes that are part of the cluster configuration.
In other words, the when you will query the cluster, the data will actually be re-routed to one of the nodes that it contains. Having this type of configuration is usefull for high availability scenarios.
The data (let’s say Hyper-V virtual machines) can be moved from one node to another without disrupting the users using these virtual machines (or very very shortly). This allow to answer scenarios like the necessity to reboot a server after the installation of Windows Updates, or the need to shutdown the node for hardware maintenance while being able to guarantee the availability of the resources hosted on the server (the virtual machines).
Node:
The nodes are the pilar components that will host the service that the Cluster is offering. For example, highly available VM’s will be hosted on Hyper-V hosts. Each one of these hosts that is added into the higly available cluster is then a node.
Quorom:
The Quorom is actually the key component of a cluster. It actually focueses all the votes of each node, and allows the cluster to be higly available by providing the information of who is currently still online. In case if one of the nodes loses his connection, for an wanted or unwanted reason, the cluster will failover to another node. (More information will be provided later in this series).
VM:
A VM is simply a Virtual Machine. Nothing more complicated then that 🙂
Know your tools:
This is PowerShell disitrct, so we primarly focus on the Powershell side of things. But the graphical interface should not be ignored. It actually helps to be able to see what we actually do, and is of great help to learn better.
Failover cluster Manager is a great tool which will help you accomplish a lot!
But we are more keen in automating things over here, so let’s jump right into action and have short overview of the basic cmdlets.
What are the main cmdlets to manage failover clustering powershell in Windows 2012 R2?
In order to manipulate clusters you will have to load the failover clustering powershell module. If the failover clustering powershell module is not loaded, it will not work. Also, if you are working on HyperV clusters, you will also need to work with the Hyper-v powershell module.
If the Failover Clustering powershell module is not present yet, you can install the role with the following command:
1 Install-WindowsFeature -Name Failover-Clustering
Get-Cluster:
Returns information on a specefic cluster. Will return the local host information if no name is specified and if local host is a cluster node.
Get-ClusterNode:
Returns information about a specific cluster node.
Get-VM:
This commdlet is actually from HyperV PowerShell module, but it will become super handy if you want to automate tasks on your virtual machines that are located in your cluster.
Keep in mind that not all the properties are displayed when calling Get-VM. To have them all available, call them with the following syntax:
1
|
get-vm -ComputerName $ClusterNodeName | fl *
|
[/stextbox]
Mind map:
I have synsteized more graphically in a mind map the cmdlets and their different usages. It think that having a graphical representation of things really is a time saver, and it also really helpfull for memorization.
Failover clustering powershell: Some examples from the field:
In order to illustrate the automation possibilities of the hyper clusters, I will comment a script I needed to write that would shutdown a cluster node for maintenance reasons.
The complexity of this script resided in the fact that the nodes needed to be shutdown in a specific order. But the VM’s hosted on these nodes needed also to respect a specific order to shut them down (the databases needed to be shutdown last). In my case the clusters had ‘only‘ 2 nodes, and the script would be launched locally from of these nodes.
Once the VM’s were all shutdown or in a saved-state, I needed to set the cluster shared volumes in maintenance mode, turn off the quorum, and at the very end, stop the cluster node.
How to get all the nodes of a cluster using PowerShell?
In order to retrieve all the nodes present in a cluster, I have used the following code:
1
2
|
Import-Module FailoverCluster -Force
$Cluster = get-cluster -name $env:computername
|
If this is executed on a cluster node, the Cluster variable will contain an object with the current cluster information.
In order to get all the nodes that a present in this cluster, I used the code here under:
1
|
$Nodes = Get-ClusterNode -Cluster $Cluster.Name
|
How to get All the VM’s present on a node from HyperV failover cluster with powershell?
In order to find all the virtual machines that are present on a specefic HyperV cluster node, I used the following code:
1
2
3
4
5
6
7
8
9
10
11
12
|
$Nodes = Get-ClusterNode -Cluster $Cluster.Name
write-log “Cluster $($Cluster.Name) contains $($Nodes.count) nodes.”
foreach ($node in $nodes){
Write-Log “Cluster Node $($Cluster.Name) contains the following VMs”
$Vms = get-vm -ComputerName $Node.Name
Foreach ($Vm in $Vms){
write-Log “$Vm.Name”
}
}
|
How to get the VM state on a failover cluster using powershell
In my specific case, I needed to identify the state of a specific machine, and if it was running, I needed to Save the state of the virtual machine using “Save-VM“. See the code below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
write-log “Starting state changes operations on node: $($node.Name)”
Foreach ($vm in $VMs){
if ($vm.Name -like $DataServerName){
write-log “The DATA server $($vm.Name) is skipped until all machines have been saved or are stopped.”
}else{
switch ($vm.state){
“Running”{
write-log “Saving VM $($VM.name) state.”
try{
Save-VM -ComputerName $Node.Name -Name $vm.name -ErrorAction Stop
write-log “–> Successfully saved”
}catch{
write-log $_
}
}
“Stopped”{
write-log “The VM $($VM.name) is already in stoped state. No action done.”
“Saved”{
write-log “The VM $($VM.name) is already in saved state.”
}
default{
Write-log “The current state $($vm.state) could not be defined. Skipping VM.”
continue
}
}#End switch
}#End If *data*
}#End foreach VM
|
Failover clustering: How to stop a cluster resource (the Quorum) using PowerShell?
Once all the VM’s are in in saved or shutdown state, we can start to work with the fun stuff: Quorum and CSV’s.
In order get a cluster quorum and to stop it, I used the following code:
1
2
3
4
5
6
7
8
|
##Quorum operations
write-log “Starting Quorum operations”
$Quorum = Get-ClusterQuorum -ErrorAction stop
write-log “Attempting to set Quorum $($Quorum.QuorumResource) offline.”
Stop-ClusterResource -Name $Quorum.QuorumResource.name
|
Failover clustering: How to work with Cluster shared volumes and PowerShell ?
In order to get all the clustered shared volumes that are present on a cluster we will use the following code:
1
2
3
|
$CSVs = Get-ClusterSharedVolume -Cluster $Cluster
write-log “There are $($CSVs.count) clustershared volumes identified on $($Cluster).”
|
Failover clustering: How to set Clustered share volumes in maintenance mode using Powershell?
Once all the VMs are either shutdown, or in a saved state, we can put the Cluster Shared volumes in maintenance mode with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
##Cluster shared volume operations
write-log “Starting operations: Setting cluster share volumes into maintenance mode.”
$CSVs = Get-ClusterSharedVolume -Cluster $Cluster
write-log “There are $($CSVs.count) clustershared volumes identified on $($Cluster).”
foreach ($csv in $CSVs){
try{
if (!($csv.name -eq $Quorum.QuorumResource.name)){
Suspend-ClusterResource -Name $csv.name -force -ErrorAction stop | Out-Null
write-log “Successfully set clusterSharedVolume $($csv.name) into maintenance mode.”
}
}catch{
write-log $_
}
}
|
Failover clustering: How to set a Quorum offline using PowerShell?
Our last step that we will need to do prior to turning down the cluster node is to set the Quorum offline, which is achieved with the following code:
1
2
3
4
5
6
7
8
|
##Quorum operations
write-log “Starting Quorum operations”
$Quorum = Get-ClusterQuorum -ErrorAction stop
write-log “Attempting to set Quorum $($Quorum.QuorumResource) offline.”
Stop-ClusterResource -Name $Quorum.QuorumResource.name -erroraction Stop | Out-Null
|
Failover clustering: How to stop a cluster resource using powershell?
Now that everything is off, in a saved state, or in maintenance mode, we can go ahead and attempt to turn the cluster node off.
1
2
3
4
5
6
7
|
##Cluster operations
$ClusterNodeToshutdown = Get-ClusterNode | where {$_.name -ne $env:COMPUTERNAME}
write-log “Attempting to stop the cluster $($Cluster.Name).”
Stop-Cluster -Cluster $Cluster.Name -Force -ErrorAction stop
write-log “The cluster resource $($Cluster.Name) has been successfully stopped.”
|
Failover clustering: How to stop a cluster node using powershell?
In order to shutdown the cluster node, we first have to be sure that we will not shutdown the server from where the script is executed, and that we can go ahead and stop the computer using a simple “Stop-Computer“.
1
2
3
4
5
6
7
|
$ClusterNodeToshutdown = Get-ClusterNode | where {$_.name -ne $env:COMPUTERNAME}
##Shutting down other cluster Node
write-log “Attempting to shutdown the cluster node $($ClusterNodeToshutdown.Name).”
Stop-Computer -ComputerName $ClusterNodeToshutdown.name -Force
|
Once all of that is done, we prompt the engineer that he can manually shutdown the cluster node with the following code:
1
2
3
4
|
write-log “Successfully stopped $($ClusterNodeToshutdown.Name).”
write-log “************************************”
write-log “***The cluster node $($env:computername) is ready to shut down. ****”
write-log “************************************”
|
The final question:
While writing this article, one question popped up that I couldn’t answer and where I would like to have your help (Use the comments section below):
Do Terminators use PowerShell ? If they do, which cmdlets do you think they would use?
Failover clustering powershell: External References:
Hyper-V failover clustering Lab –> http://channel9.msdn.com/events/TechEd/Europe/2014/CDP-H202
Configure Quorum –> http://technet.microsoft.com/en-us/library/jj612870.aspx
Technet failover clustering overview –> http://technet.microsoft.com/en-us/library/hh831579.aspx
Technet failover clustering powershell cmdlet reference –> http://technet.microsoft.com/fr-FR/library/hh847239(v=wps.620).aspx
Technet failover clustering cmdlet reference (list by task type) –> http://technet.microsoft.com/en-us/library/ee619761(WS.10).aspx
Technet High availability chapter –> http://technet.microsoft.com/en-us/library/cc754482.aspx
MVA video courses on high availability –> http://www.microsoftvirtualacademy.com/training-courses/failover-clustering-in-windows-server-2012-r2
Leave A Comment