Global Site
Displaying present location in the site.
June 14th, 2024
Machine translation is used partially for this article. See the Japanese version for the original article.
Introduction
This time, we will introduce how to use rewriting of the hosts file for switching the connection destination to the HA cluster in the Windows environment on-premises. With this method, you can connect to the HA cluster using a virtual host name.
In EXPRESSCLUSTER, we recommend using a dedicated group resources for switching the connection destination to the HA cluster. However, if you cannot use these group resources, please consider the method of switching the connection destination by rewriting the hosts file in this article.
In the method of rewriting the hosts file introduced this time, the hosts files of all clients accessing the HA cluster are rewritten from EXPRESSCLUSTER using WinRM.
This allows you to switch the connection destination to the HA cluster by virtual host name without using the dedicated group resources for switching the connection destination.
In this article, we will introduce examples of EXPRESSCLUSTER settings and common WinRM settings.
Contents
1. Use Cases
Please consider switching the connection destination to the HA cluster by rewriting the hosts file when you cannot use any dedicated group resources for switching the connection destination (floating IP resources, virtual IP resources, dynamic DNS resources) for reasons such as the following.
- The floating IP resources cannot be used to build an HA cluster across different segments.
- The virtual IP resources cannot be used because the router connecting different segments does not support RIP.
- The dynamic DNS resources cannot be used because a dynamic DNS server cannot be prepared.
We Have Summarized Methods of Switching the Connection Destination of EXPRESSCLUSTER X on On-premises
Furthermore, there are some precautions when switching the connection destination by rewriting the hosts file.
- In order to execute commands from the cluster server to the client via WinRM, it is necessary to ensure that there are no security requirements issues.
- As WinRM uses the client’s information (IP address, username, password), it will be necessary to modify the WinRM configuration and scripts each time a client is added.
2. Procedure of Rewriting the Hosts File
The procedure of rewriting the hosts file is as follows:
- a. A failure occurs and causes a failover from the active server to the standby server.
- b. Using WinRM from the EXPRESSCLUSTER's application resources, send a command to execute the script file that rewrites the hosts file of all clients with accessing the HA cluster.
- c. On each client, execute the script file and rewrite the name resolution destination of the virtual host name in the client's hosts file to the standby server.
3. Preparation
This time, we will use TCP port 5985, the default HTTP port for WinRM, for the connection method with WinRM. For details on how to configure WinRM, please refer to the following.
Windows Remote Management
3.1 For the Client
3.1.1 WinRM Configurations
At the client, launch PowerShell with administrator privileges.
On the client, execute the following commands to configure WinRM.
The above command performs the following settings:
- 1. Starts the WinRM service, and sets the service startup type to auto-start.
- 2. IConfigures a listener for the ports that send and receive WS-Management protocol messages using either HTTP or HTTPS on any IP address.
- 3. Defines exceptions for the Windows firewall of the WinRM service and opens ports for HTTP (5985) or HTTPS (5986).
- * If you are using a public firewall, only the "Local subnet" can be allowed as a remote address, so you need to change the setting to allow the client's IP address separately.
3.1.2 Initial Entry Settings for the Hosts File
The hosts file is stored in the following path.
Open the hosts file in a text editor and add the pair of the active server's IP address and the virtual host name you want to use.
Example)
3.1.3 Preparation of a Script for Rewriting the Hosts File
We prepare a script for rewriting the entries in the hosts file on the client. In this article, we create the following path on the client to store the script file.
Following is an example of a script.
- * Please note that the following script is just an example, and we do not guarantee its operation.
■rewritehosts_client.ps1
#For client
#Information about the server1
$ACTIVE_IP="192.168.0.10"
$ACTIVE_HOST_NAME="server01"
#Information about the server2
$STANDBY_IP="192.168.1.20"
$STANDBY_HOST_NAME="server02"
#Virtual host name
$VIRTUAL_HOST_NAME="vhost.exapmle.com"
#The path where the hosts file is stored
$DIR="C:\Windows\System32\drivers\etc"
#Rewriting the entries in the hosts file
if ($Args[0] -match $ACTIVE_HOST_NAME) {
$BEFORE_LINE="${STANDBY_IP}\s*${VIRTUAL_HOST_NAME}"
$AFTER_LINE ="${ACTIVE_IP} ${VIRTUAL_HOST_NAME}"
} elseif ($Args[0] -match $STANDBY_HOST_NAME) {
$BEFORE_LINE="${ACTIVE_IP}\s*${VIRTUAL_HOST_NAME}"
$AFTER_LINE ="${STANDBY_IP} ${VIRTUAL_HOST_NAME}"
} else {
exit 1
}
$RET=Select-String -Path $DIR\hosts -Pattern $AFTER_LINE
if( $RET -ne $null ) { exit 0 }
if((Test-Path $DIR\hosts_tmp) -eq "True"){
Remove-Item $DIR\hosts_tmp
}
$data = Get-Content $DIR\hosts -Encoding UTF8
$data = $data | ForEach-Object { $_ -replace "$BEFORE_LINE","$AFTER_LINE"}
$data | Out-File $DIR\hosts_tmp -Encoding UTF8
Copy-Item -Path $DIR\hosts_tmp -Destination $DIR\hosts -Force
#Check if the active server's entry is included in the rewritten hosts file
$RET=Select-String -Path $DIR\hosts -Pattern $AFTER_LINE
if( $RET -ne $null ) { exit 0 } else { exit 1 }
3.2 For the Cluster Server
3.2.1 WinRM Configurations
At each cluster server, launch PowerShell with administrator privileges.
On each cluster server, execute the following commands to register the client as trusted hosts in WinRM.
- * When adding a client, you need to register it as trusted hosts in WinRM along with the IP address of an existing client. The following is an example of adding a new client.
By executing the command below, you can check the IP addresses registered with the trusted hosts.
3.2.2 Preparing Script to Execute Script on the Client side
Preparing a script that will execute script for rewriting a client’s hosts file using WinRM from a cluster server. In this article, we create the following path on each cluster server to store the script file.
Following is an example of a script.
- * Please note that the following script is just an example, and we do not guarantee its operation.
■rewritehosts_cluster.ps1
#For cluster server
#Information about the client that rewrite the hosts file
$ClientsInfo = @{
client1 = @{
ip = "172.16.1.11"
user = "Administrator"
pass = "Password0"
}
client2 = @{
ip = "172.16.1.12"
user = "Administrator"
pass = "Password0"
}
}
#Path of rewriting the hosts file script stored on the client
$ScriptPath = "C:\EXPRESSCLUSTER\client\rewritehosts_client.ps1"
#Execute script on client using WinRM
$hosts = hostname
[string]$cmd = $ScriptPath + " " + $hosts + ';$?'
[scriptblock]$RemoteScript = [scriptblock]::Create($cmd)
foreach($ClientInfo in $ClientsInfo.Values){
$pass = ConvertTo-SecureString -AsPlainText -Force $ClientInfo["pass"]
$psc = New-Object System.Management.Automation.PSCredential($ClientInfo["user"],$pass)
$RET=Invoke-Command $ClientInfo["ip"] -ScriptBlock $RemoteScript -Credential $psc
if ($RET -eq $null -or -! $RET) {
$msg="Failed to rewrite the hosts file for the client (IP="+$ClientInfo["ip"]+")."
clplogcmd -m $msg -l ERR
}
}
exit 0
4. HA Cluster Building Procedure
We introduce the HA cluster building procedure.
4.1 HA Cluster Configuration
This time, we will build a "Mirror disk type HA cluster using hosts file rewriting" in the on-premises environment. The configuration of the HA cluster to be built is as follows.
4.2 On-premises Environment Settings
This time, we will use IIS (Internet Information Services) for the application to be clustered.
The installation methods of EXPRESSCLUSTER X and IIS for the active server and the standby server are omitted.
We will build the environment with the following settings.
■Network / Hardware
- Network
- - Network address 1 : 192.168.0.0/24
- - Network address 2 : 192.168.1.0/24
- Cluster server
- - server01
- - IP address : 192.168.0.10
- - Disk 1(For OS) : 30GiB
- - Disk 2(For mirror disk) : 5GiB
- - Cluster partition : E drive, 1024MB, RAW
- - Data partition : F drive, 4093MB, NTFS
- - server02
- - IP address : 192.168.1.20
- - Disk 1(For OS) : 30GiB
- - Disk 2(For mirror disk) : 5GiB
- - Cluster partition : E drive, 1024MB, RAW
- - Data partition : F drive, 4093MB, NTFS
■OS / EXPRESSCLUSTER
- Windows Server 2022
- EXPRESSCLUSTER X 5.2 for Windows (Internal Ver. 13.20)
4.3 EXPRESSCLUSTER Settings
WinRM needs to be run by a user with administrator privileges, so this time we will set the Administrator user to EXPRESSCLUSTER.
The EXPRESSCLUSTER settings is as follows.
■EXPRESSCLUSTER settings
- Cluster properties
- - Account
- - User Name : Administrator
- Failover group(failover)
- - Application resource(appli_rewritehosts)
- - Mirror disk resource(md)
- - Service resource(service_IIS)
- Monitor resources
- - Mirror disk monitor resource(mdw)
- - User mode monitor resource(userw)
The application resource controls the execution of script on the cluster server to the hosts file rewriting script on the client.
The settings of the application resource is as follows.
Resource name | Tab | Setting item | Value |
---|---|---|---|
Application resource (appli_rewritehosts) |
Dependency | Follow the default dependency | Off |
Dependent Resources | No Dependent Resources | ||
Details | Resident Type | Non-Resident | |
Start Path | powershell.exe | ||
Details - Tuning - Parameter | Exec User | Administrator | |
Details - Tuning - Start | Option Parameter | -file "C:\EXPRESSCLUSTER\cluster\rewritehosts_cluster.ps1" |
The service resource controls the start/stop of IIS. This time, we will set up the dependencies so that the connection switch is completed before IIS starts.
The settings of the service resource is as follows.
Resource name | Tab | Setting item | Value |
---|---|---|---|
Service resource (service_IIS) |
Dependency | Follow the default dependency | Off |
Dependent Resources | Mirror disk resource(md), Application resource(appli_rewritehosts) | ||
Details | Service Name | World Wide Web Publishing Service |
5. Checking the Operation
Check that the client's hosts file is changing before and after failover, and that the client can connect to the virtual hostname.
- 1. Start the failover group on server01.
- 2. Open the client's hosts file and check that the IP address for the virtual host name resolution is server01.
- 3. Check that you can access IIS using the virtual host name from the client.
- 4. Manually move the failover group from server01 to server02 using Cluster WebUI.
- 5. Open the client's hosts file and check that the IP address for the virtual host name resolution is server02.
- 6. Check that you can access IIS using the virtual host name from the client again.
- * If the client fails to access the virtual host name, please check the alert log of Cluster WebUI. In the script of this article, if the client's hosts file is not rewritten, it will be output to the alert log.
Conclusion
This time, we introduced how to use rewriting of the hosts file for switching the connection destination to the HA cluster in the Windows environment on-premises.
In EXPRESSCLUSTER, we recommend using a dedicated group resources for switching the connection destination to the HA cluster. However, if you cannot use these group resources, please consider the method of switching the connection destination by rewriting the hosts file in this article.
In addition, although we focused on the configuration method on-premise this time, if your environment can use hosts files and WinRM, this configuration can be used in the cloud and other locations.
If you consider introducing the configuration described in this article, you can perform a validation with the trial module of EXPRESSCLUSTER. Please do not hesitate to contact us if you have any questions.