Sql Server Linux(Redhat) Distributed Availability Group Setup — step by step

This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Tech Community.

This article describes how to create a SQL Server Distributed Availability Group (AG) for high availability on Linux(Redhat/Centos).

 

 

Here is the topology of the DAG environment

Datacenter 1

Cen1:192.168.1.61 (Primary replica)

Cen2:192.168.1.62

Cen3:192.168.1.63

Availability group name:AG1

ag1-listener:192.168.1.65

SQL Server: 2017 CU17

 

Datacenter 2

Cen4:192.168.1.71 (Forwarder)

Cen5:192.168.1.72

Availability group name:AG1

ag2-listener:192.168.1.75

SQL Server: 2017 CU17

 

Please make use following ports are opened on all the servers in both datacenters.

TCP:5022,2224,3123,21064

UDP:5405

 

Section 1:Create Availability group AG1 in datacenter1

  1. Updates /etc/hosts on all the nodes Datacenter1, add IP , replica name and AG Listener name.

Here is a screenshot. All servers in the datacenter1 should have same setting.

clipboard_image_0.png

  1. Run following bash commands on all servers in datacenter1 to enable Alwayson Feature

sudo /opt/mssql/bin/mssql-conf set hadr.hadrenabled 1

sudo systemctl restart mssql-server

clipboard_image_1.png

  1. Run folloiwng T-SQL to Enable Alwayson Health check event session on all the servers in datacenter1

ALTER EVENT SESSION AlwaysOn_health ON SERVER WITH (STARTUP_STATE=ON);

  1. Create a certificate, run the query on primary replica in datacenter1(cen1 in this case)

use master

go

CREATE MASTER KEY ENCRYPTION BY PASSWORD = '**<Master_Key_Password>**';

go

CREATE CERTIFICATE dbm_certificate WITH SUBJECT = 'dbm';

BACKUP CERTIFICATE dbm_certificate

   TO FILE = '/var/opt/mssql/data/dbm_certificate.cer'

   WITH PRIVATE KEY (

           FILE = '/var/opt/mssql/data/dbm_certificate.pvk',

           ENCRYPTION BY PASSWORD = '**<Private_Key_Password>**'

       );

  1. Copy the certificate to rest of the servers in datacenter1(cen2,cen3 in this case)

cd /var/opt/mssql/data

scp dbm_certificate.* root@**<nodeName>**:/var/opt/mssql/data/

clipboard_image_2.png

  1. Give permission to the mssql user to access the certificate files in rest of the servers(cen2,cen3 in this case)

cd /var/opt/mssql/data

chown mssql:mssql dbm_certificate.*

 

  1. Create the certificate on rest of the servers by restoring the certificate backup(cen2,cen3 in this case)

CREATE MASTER KEY ENCRYPTION BY PASSWORD = '**<Master_Key_Password>**';

CREATE CERTIFICATE dbm_certificate

   FROM FILE = '/var/opt/mssql/data/dbm_certificate.cer'

   WITH PRIVATE KEY (

   FILE = '/var/opt/mssql/data/dbm_certificate.pvk',

   DECRYPTION BY PASSWORD = '**<Private_Key_Password>**'

           );

 

  1. Create endpoint on all servers in datacenter1. The port 5022 is used.

CREATE ENDPOINT [Hadr_endpoint]

   AS TCP (LISTENER_PORT = 5022)

   FOR DATABASE_MIRRORING (

   ROLE = WITNESS,

   AUTHENTICATION = CERTIFICATE dbm_certificate,

ENCRYPTION = REQUIRED ALGORITHM AES

);

ALTER ENDPOINT [Hadr_endpoint] STATE = STARTED;

 

  1. Create AG with 3 synchronous replicas in datacenter1. Run following query on primary replica (cen1 in this case)

CREATE AVAILABILITY GROUP [ag1]

     WITH (DB_FAILOVER = ON, CLUSTER_TYPE = EXTERNAL)

     FOR REPLICA ON

         N'cen1'

                     WITH (

               ENDPOINT_URL = N'tcp://cen1:5022',

               AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,

               FAILOVER_MODE = EXTERNAL,

               SEEDING_MODE = AUTOMATIC

               ),

         N'cen2'

           WITH (

               ENDPOINT_URL = N'tcp://cen2:5022',

               AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,

               FAILOVER_MODE = EXTERNAL,

               SEEDING_MODE = AUTOMATIC

               ),

         N'cen3'

           WITH (

               ENDPOINT_URL = N'tcp://cen3:5022',

               AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,

               FAILOVER_MODE = EXTERNAL,

               SEEDING_MODE = AUTOMATIC

               )

go        

ALTER AVAILABILITY GROUP [ag1] GRANT CREATE ANY DATABASE;--grant create any database permission

 

 

Please make sure the port 5022 is in the firewall exceptoin list, else you may run into following error messages

2019-09-11 16:10:18.560        spid40s        Error: 8471, Severity: 16, State: 2.

2019-09-11 16:10:18.560        spid40s        An SNI call failed during a Service Broker/Database Mirroring transport operation. SNI error '10022(An invalid argument was supplied.)'.

2019-09-11 16:10:18.570        spid40s        Error: 9642, Severity: 16, State: 1.

2019-09-11 16:10:18.570        spid40s        An error occurred in a Service Broker/Database Mirroring transport connection endpoint, Error: 8471, State: 2. (Near endpoint role: Initiator, far endpoint address: '1')

 

 

  1. You will see below message in errorlog file of primary replica. The timeout happens because the secondary replicas have not joined the AG group

2019-09-05 20:00:44.530 spid62       The state of the local availability replica in availability group 'ag1' has changed from 'NOT_AVAILABLE' to 'RESOLVING_NORMAL'. The state changed because the local availability replica is joining the availability group. For more information, see the SQL

2019-09-05 20:00:44.570 spid62       Always On: The local replica of availability group 'ag1' is preparing to transition to the primary role. This is an informational message only. No user action is required.

2019-09-05 20:00:44.610 spid62       The state of the local availability replica in availability group 'ag1' has changed from 'RESOLVING_NORMAL' to 'PRIMARY_PENDING'. The state changed because the availability group is coming online. For more information, see the SQL Server error log or clu

2019-09-05 20:00:44.620 Server       The state of the local availability replica in availability group 'ag1' has changed from 'PRIMARY_PENDING' to 'PRIMARY_NORMAL'. The state changed because the local replica has completed processing Online command from Windows Server Failover Clustering (WS

2019-09-05 20:00:54.630 spid39s     A connection timeout has occurred while attempting to establish a connection to availability replica 'cen2' with id [6C1CD374-CB97-477A-B8DB-589D94A0FCB6]. Either a networking or firewall issue exists, or the endpoint address provided for the replica is no

2019-09-05 20:00:54.650 spid40s     A connection timeout has occurred while attempting to establish a connection to availability replica 'cen3' with id [D8E1CE2A-10CF-409B-98B1-9D3B35FEE91A]. Either a networking or firewall issue exists, or the endpoint address provided for the replica is no

 

  1. Join the AG group, run the following T-SQL queries on all the secondary servers in datacenter1(cen2,cen3 in this case)

ALTER AVAILABILITY GROUP [ag1] JOIN WITH (CLUSTER_TYPE = EXTERNAL);

ALTER AVAILABILITY GROUP [ag1] GRANT CREATE ANY DATABASE;

clipboard_image_3.png

  1. You will see below message in errorlog file of primary replica. Connection established with two secondary replicas.

2019-09-05 20:01:23.820 spid11s     A connection for availability group 'ag1' from availability replica 'cen1' with id [03D58956-AAC2-4A1D-975E-3F3B788752A2] to 'cen2' with id [6C1CD374-CB97-477A-B8DB-589D94A0FCB6] has been successfully established. This is an informational message only. N

2019-09-05 20:02:04.950 spid31s     A connection for availability group 'ag1' from availability replica 'cen1' with id [03D58956-AAC2-4A1D-975E-3F3B788752A2] to 'cen3' with id [D8E1CE2A-10CF-409B-98B1-9D3B35FEE91A] has been successfully established. This is an informational message only. N

  1. Create database and add to AG group on primary replica.(cen1 in this case).

CREATE DATABASE [db1];

ALTER DATABASE [db1] SET RECOVERY FULL;

BACKUP DATABASE [db1]    TO DISK = N'/var/opt/mssql/data/db1.bak';

BACKUP log [db1] TO DISK = N'/var/opt/mssql/data/db1.trn';

Go

ALTER AVAILABILITY GROUP [ag1] ADD DATABASE [db1];

 

The database db1 will created in the rest of the servers by auto-seeding.

clipboard_image_4.png

  1. Create SQL login pacemaker on all servers in datacenter1

CREATE LOGIN [pacemakerLogin] with PASSWORD= N'ComplexP@$$w0rd!'

ALTER SERVER ROLE [sysadmin] ADD MEMBER [pacemakerLogin]

 

 

The Availability group has been created. Unlike SQL Server in Windows, SQL Server Linux does not create any resource in Linux, you need to create these resource manually. Step 15~29 will cover the detail:

 

  1. Install SQL Server resource agent for SQL Server. Run the following bash command on all servers in datacenter1.

sudo yum install mssql-server-ha

  1. Install pacemaker.Run the following bash command on all servers in datacenter1.

sudo yum install pacemaker pcs fence-agents-all resource-agents

  1. Set the password for the default user that is created when installing Pacemaker and Corosync packages. Run following command on all servers in datacenter1(This command asks for password. The password should be same for the servers in same datacenter)

sudo passwd hacluster

clipboard_image_5.png

  1. To allow nodes to rejoin the cluster after the reboot, enable and start pcsd service and Pacemaker. Run the following commands on all servers in datacenter1.

sudo systemctl enable pcsd

sudo systemctl start pcsd

sudo systemctl enable pacemaker

clipboard_image_6.png

 

  1. Create a cluster in datacenter1. Run following commands on any one of the nodes in datacenter1(just on one server, and just one time). The Password1 should be replaced with the password specified in step 16.

sudo pcs cluster auth cen1 cen2 cen3 -u hacluster -p Password1

sudo pcs cluster setup --name cluster1 cen1 cen2 cen3

sudo pcs cluster start --all

sudo pcs cluster enable --all

clipboard_image_7.png

  1. Check the cluster status

pcs cluster status

clipboard_image_8.png

  1. Because the node level fencing configuration depends heavily on your environment, disable it for this tutorial (it can be configured later. In my test, if this value is not set, you can;'t failover, and IP resource may fail). The following script disables node level fencing(run it on any nodes in datacenter1)

sudo pcs property set stonith-enabled=false

  1. On all SQL Server Linux servers in datacenter1 , save the credentials for the SQL Server login.(Use the same user name and password created in step14)

echo 'pacemakerLogin' >> ~/pacemaker-passwd

echo 'ComplexP@$$w0rd!' >> ~/pacemaker-passwd

sudo mv ~/pacemaker-passwd /var/opt/mssql/secrets/passwd

sudo chown root:root /var/opt/mssql/secrets/passwd

sudo chmod 400 /var/opt/mssql/secrets/passwd # Only readable by root

  1. Create availability group resource at cluster level in datacenter1, run following command on any one of the nodes in datacenter1(just on one node, and just one time)

Here are the resource and constraint before creating the resource(no resource/constraint)

clipboard_image_9.png

 

Here is the command to create cluster resource.

sudo pcs resource create ag_cluster1 ocf:mssql:ag ag_name=ag1 meta failure-timeout=60s master notify=true

-------------note begin------------------

It creates a resource called ag_cluster1-master(it add the '-master' keyword automatically to the name)

And bind the resource to availability group 'ag1'

And it set the current primary replica as Master, the rest of nodes are Slaves(it check the status of AG group and make the decision

Please note, it does not return any error if you put an incorrect Availability group name (in this case, the ag1 is used, make sure you put correct name)

Internally, pacemaker issues T-SQL queries to SQL Server to check if the Ag exists

-----------note end------------------

clipboard_image_10.png

Please note. Pacemaker starts to connect the SQL Server instance using the login 'pacemakerLogin' created in step 14. If the step 14 or step 22 is skipped, the AG resource in both SQL and Cluster will fail.

  1. Create virtual IP resource. The resource name is 'virtualip1', and IP Address is 192.168.1.65 in this demonstration. This is the IP Address for the AG Listener.

sudo pcs resource create virtualip1 ocf:heartbeat:IPaddr2 ip=192.168.1.65

The resource is will start automatically after it' created

clipboard_image_11.png

 

  1. Create Availability group listener for Availability group ag1. Run following T-SQL query in primary replica(cen1 in this case). The ip address should be as same as the IP in step 24.

ALTER AVAILABILITY GROUP [ag1]  

ADD LISTENER 'ag1-listener' ( WITH IP (( '192.168.1.65','255.255.255.0') ) , PORT = 1433);  

1)It's fine if you run step 25 before the step 24, however, you can't use the IP Address to connect SQL Server until the resource in Pacemaker is created.

2)There is a bug in DMV sys.availability_group_listener_ip_addresses showing the state is offline

 

  1. Add colocation constraint. Please note, without the constraint, the ip resource does not failover when the AG resource failover. Hence we need to create a constraint to guarantee that the IP will failover when cluster resource failover. Run following command on any servers in datacenter1(just on one server, and just one time)

sudo pcs constraint colocation add virtualip1 ag_cluster1-master INFINITY with-rsc-role=Master

clipboard_image_12.png

  1. Add ordering constraint

sudo pcs constraint order promote ag_cluster1-master then start virtualip1

clipboard_image_13.png

  1. To update the property value to 2 minutes run. Run it on any node

sudo pcs property set cluster-recheck-interval=2min

sudo pcs property set start-failure-is-fatal=true

pcs resource update ag_cluster1 meta failure-timeout=60s

  1. -----note-- The order of 'ordering constraint' matters-----------

I have one customer who had a wrong order constraint. In his case, he had following constraint that promoted virtualip first, then start node. This will make the resource fail(may happen, and may caused failover issue)

sudo pcs constraint order promote virtualip1 then start ag_cluster1-master##wrong one

 

 

The availability group ag1 and the listener in Datacenter are fully functional right now. The following section will cover the forwarder.

 

Section 2:Create Availability group AG2 in datacenter2

  1. Updates /etc/hosts on all the nodes Datacenter2, add IP , replica name and AG Listener name.

Here is an screenshot. All servers in the datacenter2 should have same setting.

clipboard_image_14.png

  1. Run following commands on all servers in datacenter2 to enable Alwayson Feature

sudo /opt/mssql/bin/mssql-conf set hadr.hadrenabled 1

sudo systemctl restart mssql-server

clipboard_image_15.png

  1. Run folloiwng T-SQL to Enable Alwayson Health check event session on all the servers in datacenter2

ALTER EVENT SESSION AlwaysOn_health ON SERVER WITH (STARTUP_STATE=ON);

  1. Remember the certificate backup files created in step4 in section 1? Copy them to all the servers in datacenter2(cen4,cen5 in this case)

cd /var/opt/mssql/data

scp dbm_certificate.* root@**<nodeIPAddress>**:/var/opt/mssql/data/

clipboard_image_16.png

  1. Give permission to the mssql user to access the certificate files in all servers in datacenter2(cen4,cen5 in this case)

cd /var/opt/mssql/data

chown mssql:mssql dbm_certificate.*

clipboard_image_17.png

  1. Create the certificate in all the servers in datacenter2, by restoring the certificate backup(cen4,cen5 in this case)

use master

go

CREATE MASTER KEY ENCRYPTION BY PASSWORD = '**<Master_Key_Password>**';

go

CREATE CERTIFICATE dbm_certificate

   FROM FILE = '/var/opt/mssql/data/dbm_certificate.cer'

   WITH PRIVATE KEY (

   FILE = '/var/opt/mssql/data/dbm_certificate.pvk',

   DECRYPTION BY PASSWORD = '**<Private_Key_Password>**'

           );

 

  1. Create endpoint on all servers in datacenter2(cen4,cen5 in this case)

CREATE ENDPOINT [Hadr_endpoint]

   AS TCP (LISTENER_PORT = 5022)

   FOR DATABASE_MIRRORING (

   ROLE = WITNESS,

   AUTHENTICATION = CERTIFICATE dbm_certificate,

ENCRYPTION = REQUIRED ALGORITHM AES

);

ALTER ENDPOINT [Hadr_endpoint] STATE = STARTED;

  1. Create AG with2 synchronous replicas in datacenter2. Run following T-SQL query on primary replica (cen4 in this case)

CREATE AVAILABILITY GROUP [ag2]  

WITH (DB_FAILOVER = ON, CLUSTER_TYPE = EXTERNAL)

FOR  

REPLICA ON N'cen4' WITH (ENDPOINT_URL = N'TCP://cen4:5022',  

   FAILOVER_MODE = EXTERNAL,  

   AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,    

   SEEDING_MODE = AUTOMATIC),  

N'cen5' WITH (ENDPOINT_URL = N'TCP://cen5:5022',  

   FAILOVER_MODE = EXTERNAL,  

   AVAILABILITY_MODE = SYNCHRONOUS_COMMIT,  

   SEEDING_MODE = AUTOMATIC);  

go

ALTER AVAILABILITY GROUP [ag2] GRANT CREATE ANY DATABASE;

  1. Join the AG group, run the following queries on all the secondary servers in datacenter2( cen5 in this case)

ALTER AVAILABILITY GROUP [ag2] JOIN WITH (CLUSTER_TYPE = EXTERNAL);

ALTER AVAILABILITY GROUP [ag2] GRANT CREATE ANY DATABASE;

 

The Availability group has been created. I'll create the resources in Linux

 

  1. Install SQL Server resource agent for SQL Server. Run the following commands on all servers in datacenter2.

sudo yum install mssql-server-ha

  1. Install pacemaker.Run the following commands on all servers in datacenter2.

sudo yum install pacemaker pcs fence-agents-all resource-agents

  1. Set the password for the default user that is created when installing Pacemaker and Corosync packages. Run following command on all servers in datacenter2(This command asks for password. The password should be same for the servers in same datacenter)

sudo passwd hacluster

clipboard_image_18.png

  1. To allow nodes to rejoin the cluster after the reboot, enable and start pcsd service and Pacemaker. Run the following commands on all servers in datacenter1.

sudo systemctl enable pcsd

sudo systemctl start pcsd

sudo systemctl enable pacemaker

clipboard_image_19.png

  1. Create cluster, run it on one of the nodes(just on one node). The Password2 should be replaced with the password specified in step 13.

sudo pcs cluster auth cen4 cen5 -u hacluster -p Password2

sudo pcs cluster setup --name cluster2 cen4 cen5

sudo pcs cluster start --all

sudo pcs cluster enable --all

 

clipboard_image_20.png

  1. Because the node level fencing configuration depends heavily on your environment, disable it for this tutorial (it can be configured later. In my test, if this value is not set, you can;'t failover, and IP resource may fail). The following script disables node level fencing:

sudo pcs property set stonith-enabled=false

From <https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-availability-group-cluster-rhel?view=sql-server-2017>

  1. Create availability group resource at cluster level, on one of nodes in DC2(only one time).

sudo pcs resource create ag_cluster2 ocf:mssql:ag ag_name=ag2 meta failure-timeout=60s master notify=true

clipboard_image_21.png

  1. Create virtual IP resource. The resource name is 'virtualip2'. Please replace with valid value

sudo pcs resource create virtualip2 ocf:heartbeat:IPaddr2 ip=192.168.1.75

clipboard_image_22.png

  1. Create Availability group listener for Availability group ag1. Run following T-SQL query in primary replica(cen1 in this case). The ip address should be as same as the IP in step 17.

ALTER AVAILABILITY GROUP [ag1]  

ADD LISTENER 'ag1-listener' ( WITH IP (( '192.168.1.75','255.255.255.0') ) , PORT = 1433);  

 

  1. Add colocation constraint. Please note, without the constraint, the ip resource does not failover when the AG resource failover. Hence we need to create a constraint to guarantee that the IP will failover when cluster resource failover. Run following command on any servers in datacenter2(just on one server, and just one time)

sudo pcs constraint colocation add virtualip2 ag_cluster2-master INFINITY with-rsc-role=Master

clipboard_image_23.png

  1. Add ordering constraint. Run following command on any servers in datacenter2(just on one server, and just one time)

sudo pcs constraint order promote ag_cluster2-master then start virtualip2

clipboard_image_24.png

  1. To update the property value to 2 minutes run. Run it on any node

sudo pcs property set cluster-recheck-interval=2min

sudo pcs property set start-failure-is-fatal=true

pcs resource update ag_cluster1 meta failure-timeout=60s

 

Section 3:Create Distribution Availability group

 

  1. Create distributed availability group in datacenter1. Run following query in primary replica in datacenter1( cen1 in this case)

 

CREATE AVAILABILITY GROUP [distributedag]

   WITH (DISTRIBUTED)  

   AVAILABILITY GROUP ON

     'ag1' WITH  

     (  

         LISTENER_URL = 'tcp://ag1-listener:5022',  

         AVAILABILITY_MODE = ASYNCHRONOUS_COMMIT,  

         FAILOVER_MODE = MANUAL,  

         SEEDING_MODE = AUTOMATIC  

     ),  

     'ag2' WITH  

     (  

         LISTENER_URL = 'tcp://ag2-listener:5022',  

         AVAILABILITY_MODE = ASYNCHRONOUS_COMMIT,  

         FAILOVER_MODE = MANUAL,  

         SEEDING_MODE = AUTOMATIC  

     );  

 

  1. Join the distributed availability group . Run following query in the forwarder server(cen4 in this case)

ALTER AVAILABILITY GROUP [distributedag]  

   JOIN  

   AVAILABILITY GROUP ON

     'ag1' WITH  

     (  

         LISTENER_URL = 'tcp://ag1-listener:5022',  

         AVAILABILITY_MODE = ASYNCHRONOUS_COMMIT,  

         FAILOVER_MODE = MANUAL,  

         SEEDING_MODE = AUTOMATIC  

     ),  

     'ag2' WITH  

     (  

         LISTENER_URL = 'tcp://ag2-listener:5022',  

         AVAILABILITY_MODE = ASYNCHRONOUS_COMMIT,  

         FAILOVER_MODE = MANUAL,  

         SEEDING_MODE = AUTOMATIC  

    );  

 

 

     The databases in AG group in datacenter1 will be propagated to servers in datacenter2 by auto-seeding after step 2 is complete.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.