Real World Solutions

Here are some examples of how you can use Hyper-V Replica to solve problems that you might encounter.

Enabling Replication for Lots of Virtual Machines

Challenge

The company you are working for has completed an upgrade from Windows Server 2008 R2 to Windows Server 2012 and has converted a large number of virtual machines from VMware vSphere. This has been done so that the company can use Hyper-V Replica as a DR solution. There are 300 virtual machines to configure. You need to complete the following:

  • Document the DR plan for the virtual machines.
  • Enable HVR by using the network for the initial data copy.
  • Do this as quickly as possible, while trying to limit the impact on the network.

Solution

You can rule out using the GUI if there are 300 virtual machines to configure. The best solution is to use Microsoft Excel to document the required configuration for each virtual machine and save the document as a CSV file. There will be one line per virtual machine. The CSV file can then be read in, one line at a time, to configure HVR for each virtual machine. A time delay can be built into the script to slow down the process.

Create a CSV file that looks like the following example. It will serve two purposes. First, it will document the DR plan for each virtual machine. Second, it will be used by a PowerShell script to enable Hyper-V Replica.

VM,ReplicaHost,Port,Authentication,History,VSSFreq
VM01,host3.demo.internal,80,Kerberos,0,0
VM02,host3.demo.internal,80,Kerberos,15,2
VM03,host3.demo.internal,80,Kerberos,7,3

Note that if History is 0, there will be no recovery points, and if VSSFreq is 0, application-consistent snapshots will not be created. The following script will read this CSV file and implement HVR for you:

#Configure sleep timer
$SleepInSeconds = 300
Import-CSV C:ScriptsReplica.csv | foreach {
#Will there be recovery points?
if ($_.History -eq 0)
    {
    #No recovery points
    Enable-VMReplication -VMName $_.VM -ReplicaServerName $_.ReplicaHost `
-ReplicaServerPort $_.Port -AuthenticationType $_.Authentication
    }
else
{
    #Will VSS be used?
    if ($_.History -eq 0)
        {
        #No VSS
        Enable-VMReplication -VMName $_.VM -ReplicaServerName $_.ReplicaHost `
-ReplicaServerPort $_.Port -AuthenticationType $_.Authentication `
-RecoveryHistory $_.History
        }
    else
        {
        #VSS
        Enable-VMReplication -VMName $_.VM -ReplicaServerName $_.ReplicaHost `
-ReplicaServerPort $_.Port -AuthenticationType $_.Authentication `
-RecoveryHistory $_.History -VSSSnapshotFrequency $_.VSSFreq
        }
}

Start-VMInitialReplication -VMName $_.VM
Write-Host "$_.VM :: Replication configured"
Write-Host "Sleeping for $SleepInSeconds seconds"
Start-Sleep -s $SleepInSeconds
}

This Script Is a Starting Point
You could take this script and extend it to do much more for you, such as configuring failover TCP/IP settings for IPv4 and IPv6, or excluding VHDs from replication.

Running a Planned Failover

Challenge

You have 300 virtual machines running in your production site. There is news of a hurricane bringing floods that will affect the area. You need to invoke your DR plan as quickly as possible so that the following can occur:

  • Employees can evacuate with their families.
  • The company will still be operational in the DR site, which is a safe, remote location.

How will you do this work as quickly as possible?

Solution

There are two parts to the planned failover. The first part is to initiate the planned failover on the production hosts. The following script will find each replicating source virtual machine, power it down, and initiate a planned failover. The script should be run on each host in the production site:

Get-VM | Where {$_.ReplicationMode -eq "Primary"} | ForEach-Object {
Stop-VM $_.Name -Force
Start-VMFailover -VMName $_.Name -Prepare -Confirm:$false
}
Write-Host "First half of planned failover is complete. Please continue in `
DR site"

The second part of the process has to be run on each host in the DR site. This script will complete the failover, reverse replication, and start up every replica virtual machine found on the host:

Get-VM | Where {$_.ReplicationMode -eq "Replica"} | ForEach-Object {
Start-VMFailover -VMName $_.Name -Confirm:$false
Set-VMReplication -reverse -VMName $_.Name
Start-VM $_.Name
}
Write-Host "Planned failover and replication reverse is complete."

Order Virtual Machine Failover
This is a simple example of a failover that might suffice for many sites. You might need to order the startup of virtual machines in the DR site. One way you could do this is to use the Notes attribute of each virtual machine, using a system such as the High/Medium/Low failover priority of Failover Clustering. You could order the startup of virtual machines based on this value.
A service provider probably wouldn’t want to start every virtual machine and might have a prewritten script to find and start each of a customer’s virtual machines in a specific order.
Another option might be to create a comma separated values (CSV) file that lists the name of each virtual machine in the order in which it must be started. This file could be available to the replica hosts. The DR site script would import that file and perform Start-VMFailover, Set-VMReplication, and Start-VM for each virtual machine in the desired failover order.
Import-CSV C:ScriptsOrderedFailover.csv | foreach {
Start-VMFailover -VMName $_.VM -Confirm:$false
Set-VMReplication -reverse -VMName $_.VM
Start-VM $_.VM
}
Write-Host "Planned ordered failover and replication reverse is complete."

Scripting an Ordered Unplanned Failover

Challenge

You have been asked to create a solution that will orchestrate the unplanned failover of virtual machines in the DR site using a PowerShell script. The script must start up each virtual machine in order and record if the virtual machine was able to successfully start up within a predetermined amount of time. This time out will vary from one virtual machine to another. There could be large number of virtual machines running on several hosts.

Solution

This solution is going to use a PowerShell script. The script will

1. Read a CSV file. This file will record each virtual machine in the order it is to be started, what host it can be found on, and the time that the script will wait for the virtual machine to start.
2. Attempt to start the virtual machine if it is found to be in a healthy state.
3. Verify that the virtual machine starts up within the timeout by checking that the guest OS integration components respond to a heartbeat check from the host’s management OS.

The following is an example CSV file, saved as C:ScriptsFailover.txt:

Host,VM,Timeout
Host2,VM01,10
Host2,VM02,60
Host2,VM03,10

This is the script:

Function Check-VM ($CheckHost, $CheckVM, [int]$CheckTimeout)
{
#Set this variable so it works in the following while loop
$TheVM = $CheckVM
Write-Output "Checking $TheVM Now"
Write-Output "Timeout is $CheckTimeout seconds"
#Configure a time out
$LoopTimeout = new-timespan -Seconds $CheckTimeout 
$SW = [Diagnostics.StopWatch]::StartNew()
#Keep checking the VM while the time out hasn't expired
While ($SW.elapsed -lt $LoopTimeout){

    #Check the VM's integration component heartbeat    
    $HeartBeat = Get-VMIntegrationService -ComputerName $CheckHost -VMName `
$TheVM -Name Heartbeat -ErrorAction Continue
    If ($HeartBeat.PrimaryStatusDescription -Eq "OK")
        {
        Write-Output "$TheVM is running"
        #The VM is running so we can break out of the loop
        Break
        }
    Else
        {
        Write-Output "Waiting on $TheVM ... "
        #The VM is not yet running so sleep 5 seconds before checking again
        sleep 5
        }
}
#The script only gets here if the time out has expired
if ($HeartBeat.PrimaryStatusDescription -Ne "OK")
    {
    If ((Get-VM $TheVM -ComputerName Host2).State -EQ "Running") `
{ Stop-VM -ComputerName $CheckHost $TheVM -Force }
    #The VM is not responding to the integration heartbeat so we time out and
     stop the VM
    write-output "$TheVM Timed out"
    }
#End of function
}
#The unplanned failover script starts here
cls
#Define the comma seperated values (CSV) file
$CSVFile = "C:ScriptsFailover.txt"
#In this script, there are 3 columns in the CSV file:
# - Host: The name of the host/cluster that the VM replica is on
# - VM: The name of the virtual machine being failed over
# - Timeout: How long (in seconds) should the script wait for the VM to be online
Import-CSV $CSVFile | ForEach {
    $FailHost = $_.Host
    $FailVM = $_.VM
    $FailTimeout = $_.TimeOut
    If ((Get-VM -ComputerName $FailHost $FailVM).ReplicationMode -EQ "Replica") 
        {
        Write-Output "Failing over $FailVM on $FailHost now"
        Start-VMFailover -ComputerName $FailHost -VMName $FailVM -Confirm:$false
        Start-VM -ComputerName $FailHost -VMName $FailVM
        Check-VM $FailHost $FailVM $FailTimeout
        }
    else
        {
        Write-Output "$FailVM on $FailHost is not ready for failover"
        }
}

This script could be made more powerful by adding some functionality such as the following:

  • Performing further health checks on the virtual machine before attempting to start it up
  • Adding virtual machine dependencies into the CSV, and verifying that those other virtual machines are running before starting the dependent virtual machine
  • Recording all activity in a log file for easy troubleshooting
  • Using a PowerShell workflow to start up several virtual machines at once to reduce the RTO
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.162.201