Перейти на саміт з інновацій:
Дізнайтеся, як перенесення та модернізація до Azure може підвищити продуктивність, стійкість і безпеку вашого бізнесу, що дає змогу повністю охопити ШІ.Зареєструватися зараз
Цей браузер більше не підтримується.
Замініть його на Microsoft Edge, щоб користуватися перевагами найновіших функцій, оновлень безпеки та технічної підтримки.
We use SQL Server 2017 (14.x) with RHEL 7.6 in this tutorial, but it's possible to use SQL Server 2019 (15.x) in RHEL 7 or RHEL 8 to configure high availability. The commands to configure the Pacemaker cluster and availability group resources has changed in RHEL 8, and you'll want to look at the article Create availability group resource and RHEL 8 resources for more information on the correct commands.
In this tutorial, you learn how to:
Create a new resource group, availability set, and Linux virtual machines (VMs)
Enable high availability (HA)
Create a Pacemaker cluster
Configure a fencing agent by creating a STONITH device
Install SQL Server and mssql-tools on RHEL
Configure SQL Server Always On availability group
Configure availability group (AG) resources in the Pacemaker cluster
Test a failover and the fencing agent
This tutorial uses the Azure CLI to deploy resources in Azure.
If you don't have an Azure subscription, create a free account before you begin.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
This article requires version 2.0.30 or later of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed.
Create a resource group
If you have more than one subscription, set the subscription that you want deploy these resources to.
Use the following command to create a resource group <resourceGroupName> in a region. Replace <resourceGroupName> with a name of your choosing. We're using East US 2 for this tutorial. For more information, see the following Quickstart.
Azure CLI
az group create --name<resourceGroupName>--location eastus2
Create an availability set
The next step is to create an availability set. Run the following command in Azure Cloud Shell, and replace <resourceGroupName> with your resource group name. Choose a name for <availabilitySetName>.
Azure CLI
az vm availability-set create \
--resource-group<resourceGroupName> \
--name<availabilitySetName> \
--platform-fault-domain-count2 \
--platform-update-domain-count2
You should get the following results once the command completes:
If you choose a Pay-As-You-Go (PAYG) RHEL image, and configure high availability (HA), you might be required to register your subscription. This can cause you to pay twice for the subscription, as you'll be charged for the Microsoft Azure RHEL subscription for the VM, and a subscription to Red Hat. For more information, see https://access.redhat.com/solutions/2458541.
To avoid being "double billed", use a RHEL HA image when creating the Azure VM. Images offered as RHEL-HA images are also PAYG images with HA repo pre-enabled.
Get a list of virtual machine images that offer RHEL with HA:
For this tutorial, we're choosing the image RedHat:RHEL-HA:7.6:7.6.2019062019 for the RHEL 7 example and choosing RedHat:RHEL-HA:8.1:8.1.2020021914 for the RHEL 8 example.
You can also choose SQL Server 2019 (15.x) preinstalled on RHEL8-HA images. To get the list of these images, run the following command:
If you do use one of the above images to create the virtual machines, it has SQL Server 2019 (15.x) preinstalled. Skip the Install SQL Server and mssql-tools section as described in this article.
Важливо
Machine names must be less than 15 characters to set up availability group. Username can't contain upper case characters, and passwords must have more than 12 characters.
We want to create 3 VMs in the availability set. Replace these values in the following command:
<resourceGroupName>
<VM-basename>
<availabilitySetName>
<VM-Size> - An example would be "Standard_D16_v3"
<username>
<password>
Azure CLI
for i in `seq 13`; do
az vm create \
--resource-group<resourceGroupName> \
--name<VM-basename>$i \
--availability-set<availabilitySetName> \
--size"<VM-Size>" \
--image"RedHat:RHEL-HA:7.6:7.6.2019062019" \
--admin-username"<username>" \
--admin-password"<password>" \
--authentication-type all \
--generate-ssh-keys
done
The above command creates the VMs, and creates a default virtual network for those VMs. For more information on the different configurations, see the az vm create article.
You should get results similar to the following once the command completes for each VM:
The default image that is created with the above command creates a 32GB OS disk by default. You could potentially run out of space with this default installation. You can use the following parameter added to the above az vm create command to create an OS disk with 128GB as an example: --os-disk-size-gb 128.
Connect to VM1 or the other VMs using the following command in Azure Cloud Shell. If you're unable to find your VM IPs, follow this Quickstart on Azure Cloud Shell.
Azure CLI
ssh <username>@publicipaddress
If the connection is successful, you should see the following output representing the Linux terminal:
Output
[<username>@<VM1> ~]$
Type exit to leave the SSH session.
Enable high availability
Важливо
In order to complete this portion of the tutorial, you must have a subscription for RHEL and the High Availability Add-on. If you're using an image recommended in the previous section, you don't have to register another subscription.
It will be easier if you open an SSH session to each of the VMs simultaneously as the same commands will need to be run on each VM throughout the article.
If you're copying and pasting multiple sudo commands, and are prompted for a password, the additional commands will not run. Run each command separately.
To open the Pacemaker firewall ports, run the following commands on each VM:
Update and install Pacemaker packages on all nodes using the following commands:
Примітка
nmap is installed as part of this command block as a tool to find available IP addresses in your network. You don't have to install nmap, but it will be useful later in this tutorial.
Set the password for the default user that is created when installing Pacemaker packages. Use the same password on all nodes.
Bash
sudo passwd hacluster
Use the following command to open the hosts file and set up host name resolution. For more information, see Configure AG on configuring the hosts file.
Bash
sudo vi /etc/hosts
In the vi editor, enter i to insert text, and on a blank line, add the Private IP of the corresponding VM. Then add the VM name after a space next to the IP. Each line should have a separate entry.
Output
<IP1> <VM1>
<IP2> <VM2>
<IP3> <VM3>
Важливо
We recommend that you use your Private IP address in the previous example. Using the Public IP address in this configuration will cause the setup to fail and we don't recommend exposing your VM to external networks.
To exit the vi editor, first hit the Esc key, and then enter the command :wq to write the file and quit.
Create the Pacemaker cluster
In this section, you enable and start the pcsd service, and then configure the cluster. For SQL Server on Linux, the cluster resources aren't created automatically. You need to enable and create the Pacemaker resources manually. For more information, see the article on configuring a failover cluster instance for RHEL.
Enable and start pcsd service and Pacemaker
Run the commands on all nodes. These commands allow the nodes to rejoin the cluster after each node restarts.
On the primary node, run the following commands to set up the cluster.
When running the pcs cluster auth command to authenticate the cluster nodes, you're prompted for a password. Enter the password for the hacluster user created earlier.
Run the following command to check that all nodes are online.
Bash
sudo pcs status
RHEL 7
If all nodes are online, you see output similar to the following example:
Output
Cluster name: az-hacluster
WARNINGS:
No stonith devices and stonith-enabled is not false
Stack: corosync
Current DC: <VM2> (version 1.1.19-8.el7_6.5-c3c624ea3d) - partition with quorum
Last updated: Fri Aug 23 18:27:57 2019
Last change: Fri Aug 23 18:27:56 2019 by hacluster via crmd on <VM2>
3 nodes configured
0 resources configured
Online: [ <VM1> <VM2> <VM3> ]
No resources
Daemon Status:
corosync: active/enabled
pacemaker: active/enabled
pcsd: active/enabled
RHEL 8
Output
Cluster name: az-hacluster
WARNINGS:
No stonith devices and stonith-enabled is not false
Cluster Summary:
* Stack: corosync
* Current DC: <VM2> (version 1.1.19-8.el7_6.5-c3c624ea3d) - partition with quorum
* Last updated: Fri Aug 23 18:27:57 2019
* Last change: Fri Aug 23 18:27:56 2019 by hacluster via crmd on <VM2>
* 3 nodes configured
* 0 resource instances configured
Node List:
* Online: [ <VM1> <VM2> <VM3> ]
Full List of Resources:
* No resources
Daemon Status:
* corosync: active/enabled
* pacemaker: active/enabled
* pcsd: active/enabled
Set expected votes in the live cluster to 3. This command only affects the live cluster, and doesn't change the configuration files.
On all nodes, set the expected votes with the following command:
Bash
sudo pcs quorum expected-votes 3
Configure the fencing agent
To configure a fencing agent, the following instructions are modified for this tutorial. For more information, see create a STONITH device.
Enter a Name like <resourceGroupName>-app. For supported account types select Accounts in this organizational directory only (Microsoft only - Single tenant).
Select Web for Redirect URI, and enter a URL (for example, http://localhost) and select Add. The sign-on URL can be any valid URL. Once done, select Register.
Choose Certificates and secrets for your new App registration, then select New client secret.
Enter a description for a new key (client secret), and then select Add.
Write down the value of the secret. It's used as the password for the service principal.
Select Overview. Write down the Application ID. It's used as the username (login ID in the following steps) of the service principal.
Since we already added a rule to our firewall to allow the HA service (--add-service=high-availability), there's no need to open the following firewall ports on all nodes: 2224, 3121, 21064, 5405. However, if you're experiencing any type of connection issues with HA, use the following command to open these ports that are associated with HA.
Порада
You can optionally add all ports in this tutorial at once to save some time. The ports that need to be opened are explained in their relevant sections that follow. If you would like to add all ports now, add the additional ports: 1433 and 5022.
If you have created the VMs with SQL Server 2019 (15.x) pre-installed on RHEL8-HA then you can skip the below steps to install SQL Server and mssql-tools and start the Configure an Availability Group section after you setup the sa password on all the VMs by running the command sudo /opt/mssql/bin/mssql-conf set-sa-password on all VMs.
Use the below section to install SQL Server and mssql-tools on the VMs. You can choose one of the below samples to install SQL Server 2017 (14.x) on RHEL 7 or SQL Server 2019 (15.x) on RHEL 8. Perform each of these actions on all nodes. For more information, see Install SQL Server on a Red Hat VM.
Install SQL Server on the VMs
The following commands are used to install SQL Server:
For convenience, add /opt/mssql-tools/bin/ to your PATH environment variable. This enables you to run the tools without specifying the full path. Run the following commands to modify the PATH for both login sessions and interactive/non-login sessions:
Enable Always On availability groups and restart mssql-server
Enable Always On availability groups on each node that hosts a SQL Server instance. Then restart mssql-server. Run the following script:
Bash
sudo /opt/mssql/bin/mssql-conf set hadr.hadrenabled 1
sudo systemctl restart mssql-server
Create a certificate
We currently don't support AD authentication to the AG endpoint. Therefore, we must use a certificate for AG endpoint encryption.
Connect to all nodes using SQL Server Management Studio (SSMS) or sqlcmd. Run the following commands to enable an AlwaysOn_health session and create a master key:
Важливо
If you're connecting remotely to your SQL Server instance, you'll need to have port 1433 open on your firewall. You'll also need to allow inbound connections to port 1433 in your NSG for each VM. For more information, see Create a security rule for creating an inbound security rule.
Replace the <password> with your own password.
SQL
ALTEREVENTSESSION AlwaysOn_health ONSERVERWITH (STARTUP_STATE=ON);
GO
CREATEMASTERKEY ENCRYPTION BYPASSWORD = '<password>';
Connect to the primary replica using SSMS or sqlcmd. The following commands create a certificate at /var/opt/mssql/data/dbm_certificate.cer and a private key at var/opt/mssql/data/dbm_certificate.pvk on your primary SQL Server replica:
Replace the <password> with your own password.
SQL
CREATE CERTIFICATE dbm_certificate WITH SUBJECT = 'dbm';
GO
BACKUP CERTIFICATE dbm_certificate
TOFILE = '/var/opt/mssql/data/dbm_certificate.cer'WITHPRIVATEKEY (
FILE = '/var/opt/mssql/data/dbm_certificate.pvk',
ENCRYPTION BYPASSWORD = '<password>'
);
GO
Exit the sqlcmd session by running the exit command, and return back to your SSH session.
Copy the certificate to the secondary replicas and create the certificates on the server
Copy the two files that were created to the same location on all servers that will host availability replicas.
On the primary server, run the following scp command to copy the certificate to the target servers:
Replace <username> and <VM2> with the user name and target VM name that you're using.
Run this command for all secondary replicas.
Примітка
You don't have to run sudo -i, which gives you the root environment. You could just run the sudo command in front of each command as we previously did in this tutorial.
Bash
# The below command allows you to run commands in the root environment
sudo -i
The mv command moves the files or directory from one place to another.
The chown command is used to change the owner and group of files, directories, or links.
Run these commands for all secondary replicas.
Bash
sudo -i
mv /home/<username>/dbm_certificate.* /var/opt/mssql/data/
cd /var/opt/mssql/data
chown mssql:mssql dbm_certificate.*
The following Transact-SQL script creates a certificate from the backup that you created on the primary SQL Server replica. Update the script with strong passwords. The decryption password is the same password that you used to create the .pvk file in the previous step. To create the certificate, run the following script using sqlcmd or SSMS on all secondary servers:
Create the database mirroring endpoints on all replicas
Run the following script on all SQL Server instances using sqlcmd or SSMS:
SQL
CREATE ENDPOINT [Hadr_endpoint]
AS TCP (LISTENER_PORT = 5022)
FOR DATABASE_MIRRORING (
ROLE = ALL,
AUTHENTICATION = CERTIFICATE dbm_certificate,
ENCRYPTION = REQUIRED ALGORITHM AES
);
GO
ALTER ENDPOINT [Hadr_endpoint] STATE = STARTED;
GO
Create the availability group
Connect to the SQL Server instance that hosts the primary replica using sqlcmd or SSMS. Run the following command to create the availability group:
Replace ag1 with your desired Availability Group name.
Replace the <VM1>, <VM2>, and <VM3> values with the names of the SQL Server instances that host the replicas.
In order to join the secondary replicas to the AG, you need to open port 5022 on the firewall for all servers. Run the following command in your SSH session:
The following Transact-SQL commands are used in this step. Run these commands on the primary replica:
SQL
CREATEDATABASE [db1]; -- creates a database named db1
GO
ALTERDATABASE [db1] SETRECOVERYFULL; -- set the database in full recovery model
GO
BACKUPDATABASE [db1] -- backs up the database to diskTO DISK = N'/var/opt/mssql/data/db1.bak';
GO
ALTERAVAILABILITYGROUP [ag1] ADDDATABASE [db1]; -- adds the database db1 to the AG
GO
Verify that the database is created on the secondary servers
On each secondary SQL Server replica, run the following query to see if the db1 database was created and is in a SYNCHRONIZED state:
SQL
SELECT * FROM sys.databases WHEREname = 'db1';
GO
SELECT DB_NAME(database_id) AS'database', synchronization_state_desc FROM sys.dm_hadr_database_replica_states;
If the synchronization_state_desc lists SYNCHRONIZED for db1, this means the replicas are synchronized. The secondaries are showing db1 in the primary replica.
Create availability group resources in the Pacemaker cluster
This article contains references to the term slave, a term Microsoft considers offensive when used in this context. The term appears in this article because it currently appears in the software. When the term is removed from the software, we will remove it from the article.
Create the AG cluster resource
Use one of the following commands based on the environment chosen earlier to create the resource ag_cluster in the availability group ag1.
Create an ordering constraint to ensure that the AG resource is up and running before the IP address. While the colocation constraint implies an ordering constraint, this enforces it.
RHEL 7
Bash
sudo pcs constraint order promote ag_cluster-master then start virtualip
RHEL 8
Bash
sudo pcs constraint order promote ag_cluster-clone then start virtualip
To verify the constraints, run the following command:
We're ready for testing. Re-enable stonith in the cluster by running the following command on Node 1:
Bash
sudo pcs property set stonith-enabled=true
Check cluster status
You can check the status of your cluster resources using the following command:
Output
[<username>@VM1 ~]$ sudo pcs status
Cluster name: az-hacluster
Stack: corosync
Current DC: <VM3> (version 1.1.19-8.el7_6.5-c3c624ea3d) - partition with quorum
Last updated: Sat Dec 7 00:18:38 2019
Last change: Sat Dec 7 00:18:02 2019 by root via cibadmin on VM1
3 nodes configured
5 resources configured
Online: [ <VM1> <VM2> <VM3> ]
Full list of resources:
Master/Slave Set: ag_cluster-master [ag_cluster]
Masters: [ <VM2> ]
Slaves: [ <VM1> <VM3> ]
virtualip (ocf::heartbeat:IPaddr2): Started <VM2>
rsc_st_azure (stonith:fence_azure_arm): Started <VM1>
Daemon Status:
corosync: active/enabled
pacemaker: active/enabled
pcsd: active/enabled
You can also specify an additional option so that the temporary constraint that was created to move the resource to a desired node is disabled automatically, and you don't have to perform steps 2 and 3 in the following instructions.
Another alternative to automate steps 2 and 3, which clear the temporary constraint in the resource move command itself, is by combining multiple commands in a single line.
Адмініструйте інфраструктуру баз даних SQL Server для хмарних, локальних і гібридних реляційних баз даних за допомогою пропозицій реляційних баз даних Microsoft PaaS.