Vous êtes sur la page 1sur 42

Installing and

configuring Always
On Availability
Groups (Windows)
Information in this document, including URL and other Internet website references, is subject to change without
notice. Unless otherwise noted, the companies, organizations, products, domain names, email addresses, logos,
people, places, and events depicted in examples herein are fictitious. No association with any real company,
organization, product, domain name, email address, logo, person, place, or event is intended or should be inferred.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under
copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or
transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any
purpose, without the express written permission of Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering
subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the
furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other
intellectual property.

© 2017 Microsoft Corporation. All rights reserved.

Microsoft, Windows, Windows Server, PowerShell, and SQL Server are either registered trademarks or trademarks of
the Microsoft group of companies.

All other trademarks are property of their respective owners.


Contents

Introduction .............................................................................................................. 3
Objectives.................................................................................................................. 4
Overview of failover clustering and Always On Availability Groups ............ 6
Configuring the sample application .................................................................. 28
Executing a manual failover ................................................................................ 33
Monitoring availability groups ............................................................................ 35
Summary and additional information ............................................................... 39
Appendix ................................................................................................................. 40

Installing and configuring Always On Availability Groups (Windows)


Introduction
Data availability is important. When databases become unavailable, it has a significant effect on an
organization’s ability to service customers or carry out day-to-day processes. The Always On Availability
Groups features in Microsoft SQL Server are designed to ensure a database is available even when the
primary server goes down.
Always On Availability Groups is a high-availability and disaster recovery solution that provides an enterprise-
level alternative to database mirroring. Introduced in SQL Server 2012, Always On Availability Groups
maximizes the availability of a set of user databases for an enterprise. An availability group supports a failover
environment for a discrete set of user databases, known as availability databases, that failover together.
An availability group supports one set of read-write primary databases and between one and eight sets of
corresponding secondary databases. Optionally, secondary databases can be made available for read-only
access and/or some backup operations. An availability group defines a set of two or more failover partners,
known as availability replicas.

Installing and configuring Always On Availability Groups (Windows) 3


Objectives
This lab exercise will help you to understand concepts that are central to configuring and managing one or
more Always On availability groups in SQL Server 2017.
This lab will demonstrate how to create and deploy an Always On Availability Group implementation on
instances of SQL Server 2017 that reside on different Windows Server Failover Clustering (WSFC) nodes
within a single WSFC failover cluster. The lab contains information about configuring a server instance,
creating an availability group, configuring the availability group for client connections, managing availability
groups, and monitoring availability groups.

Lab details and prerequisites


Before completing this exercise, you should already have a solid understanding of Always On Availability
Groups. For more information, see Always On Availability Groups (SQL Server).
For this lab, we have already created the WSFC cluster with SQL Server.
For more information about the prerequisite list, restrictions, and recommendations, see the Always On
Availability Groups page.

Installing and configuring Always On Availability Groups (Windows) 4


Understanding the environment
Following are the architecture details of the lab environment. The environment includes these components:
• A virtual network containing multiple subnets, including a front-end and a back-end subnet.

• Three computers running Windows Server on which SQL Server is installed. These computers are
deployed to the back-end subnet and joined to the domain.

• One Windows Server 2016 domain controller. The domain controller has the following applications
installed:
o SQL Server Management Studio (SSMS) to connect to SQL Server.
o Visual Studio (VS) Code to modify application configuration.
o Node.js application to read data.
Note: It is not a best practice to run SSMS on a domain controller, or log on to a domain controller to modify
source code or administer a database. This configuration is for lab purposes only.
Domain
Controller
WIN-AGDC
AG Node AG Node AG Node
WIN-AGNODE01 WIN-AGNODE02 WIN-AGNODE03
SSMS
Node.js
SQL Server SQL Server SQL Server
MyExpenses

aoag.sqlserver.labs Domain Network 10.0.1.0

Clustering Network 10.0.2.0

iSCSI Network 10.0.3.0

Installing and configuring Always On Availability Groups (Windows) 5


Overview of failover clustering and Always On
Availability Groups
Availability groups require a cluster manager. In Windows, failover clustering provides the cluster manager. In
Linux, you can use Pacemaker. As part of the SQL Server Always On offering, Always On Failover Cluster
Instances uses Windows Server Failover Clustering (WSFC) functionality to provide local high availability
through redundancy at the server-instance level—a failover cluster instance (FCI). An FCI is a single instance
of SQL Server that’s installed across Windows Server Failover Clustering (WSFC) nodes, and possibly across
multiple subnets.
It’s important to note the that Always On Availability Groups feature is not the same as SQL Server Clustering.
In addition to requiring WSFC, SQL Server failover clusters require shared storage and a separate installation
process beyond the installation of the stand-alone SQL Server instances.
Always On Availability Groups relies on the WSFC cluster to monitor and manage the current roles of the
availability replicas that belong to a given availability group and to determine how a failover event affects the
availability replicas. A WSFC resource group is created for every availability group you create. The WSFC
cluster monitors this resource group to evaluate the health of the primary replica. The quorum for Always On
Availability Groups is based on all nodes in the WSFC cluster, regardless of whether a given cluster node
hosts any availability replicas.

Installing and configuring Always On Availability Groups (Windows) 6


Configuring SQL Server for availability groups
This section will walk you through the steps for configuring Always On Availability Groups with cluster manager
to provide high availability. Availability groups have already been enabled on all three instances of SQL Server
in the prebuilt lab environment. If you’re creating your own environment, you’ll need to follow the steps in the
appendix titled “Enable Always On Availability Groups.”

Creating the availability group


We’re now ready to configure an availability group. We’ll create the availability group on the instance of SQL
Server that hosts the databases that will be added to the availability group.
At this point, at least one of the databases we want to include in the availability group needs to be present and
fully backed up. For the purposes of this lab, the Expense database to be used in the MyExpenses sample
application has been created on the primary node (WIN-AGNODE01), and a full backup has been created. If
you’re building your own environment or find that the Expense database is missing, please see “Setting up the
application database” in the appendix for instructions for creating and populating the required database.
Minimally, create the initial primary replica on the instance of SQL Server on which you create the availability
group. You can specify from one to four secondary replicas. For information about availability group and replica
properties, see CREATE AVAILABILITY GROUP (Transact-SQL).
To create an availability group, you can use any of the following tools:
• New Availability Group Wizard

• Transact-SQL

• SQL Server PowerShell


In this lab, we’ll be using the New Availability Group Wizard.
We recommend you to read the prerequisites, restrictions, and recommendations before attempting to create
your first availability group.
SSMS is installed on the Windows Server 2016 domain controller (Win-AGDC). We’ll be using the SSMS
installed on this server to create the availability group by using the New Availability Group Wizard.
Using the New Availability Group Wizard on the Windows Server 2016 domain controller
1. In Object Explorer, connect to the server instance that hosts the primary replica. For this lab, this will be
WIN-AGNODE01. Select Windows Authentication.

Installing and configuring Always On Availability Groups (Windows) 7


1. Here you can see the Expenses database that we will use in the New Availability Group Wizard.

2. Expand the AlwaysOn High Availability node and the Availability Groups node.

3. To launch the New Availability Group Wizard, right-click AlwaysOn High Availability and select New
Availability Group Wizard.

Installing and configuring Always On Availability Groups (Windows) 8


4. The first time you run this wizard, an Introduction page appears. To bypass this page in the future, you
can select Do not show this page again. After reading the page, click Next.

5. On the Specify Availability Group Options page, enter the name of the new availability group in the
Availability group name field. This name must be a valid SQL Server identifier that is unique on the
cluster and in your domain. The maximum length for an availability group name is 128 characters. We'll
use MyExpenseAG.
6. Next, specify the cluster type. Here we’ll choose WSFC. For details, see Specify Availability Group
Options Page.

Installing and configuring Always On Availability Groups (Windows) 9


7. The grid on the Select Databases page lists user databases on the connected server instance that are
eligible to become the availability databases. Select one or more of the listed databases to add to the
new availability group. These databases will be the initial primary databases. For each listed database,
the Size column displays the database size, if it’s known. The Status column indicates whether a given
database meets the prerequisites for availability databases. If you change a database to make it
eligible, click Refresh to update the databases grid. If the database contains a database master key,
enter the password for the database master key in the Password column. For this lab, the primary
database for the MyExpenses application has been created, populated, and fully backup up. Select the
Expenses database.

Installing and configuring Always On Availability Groups (Windows) 10


8. On the Specify Replicas page, specify and configure replicas for the new availability group. This page
contains four tabs. The following table describes them.

Replicas Use this tab to specify each instance of SQL Server that will host a secondary
replica. Note that the server instance to which you are currently connected
must host the primary replica.
In this lab, we’ll be using our second and third nodes (WIN-AGNODE02 and
WIN-AGNODE03) as secondary replicas. To configure this, select Add
Replica, type WIN-AGNODE01, and click Connect. Follow the same steps to
add WIN-AGNODE02 and WIN-AGNODE03.

Installing and configuring Always On Availability Groups (Windows) 11


Select Automatic Failover for both nodes, and set Readable Secondary to
Yes for all nodes.

Installing and configuring Always On Availability Groups (Windows) 12


Endpoints Use this tab to verify any existing database mirroring endpoints. If an endpoint
is missing on a server instance whose service accounts use Windows
Authentication, you can use this tab to create the endpoint automatically.

Installing and configuring Always On Availability Groups (Windows) 13


Backup Use this tab to specify your backup preference for the availability group and
Preferences your backup priorities for the individual availability replicas. For this lab, select
Prefer Secondary.

Listener Use this tab to create an availability group listener. By default, the wizard does
not create a listener. We’ll create a listener later in the lab.

Installing and configuring Always On Availability Groups (Windows) 14


9. On the Select Initial Data Synchronization page, choose how you want your new secondary
databases to be created and joined to the availability group. We’ll use the Automatic seeding option.
This will configure SQL Server to automatically create the secondary replicas for every database in the
group. Automatic seeding requires that the data and log file paths are the same on every SQL Server
instance participating in the group.

Installing and configuring Always On Availability Groups (Windows) 15


Following are the other options for new secondary databases:

• Full database and log backup. Select this option if your environment meets the requirements for
automatically starting initial data synchronization.

• Join only. If you have manually prepared secondary databases on the server instances that will
host the secondary replicas, you can select this option. The wizard will join the existing secondary
databases to the availability group.

• Skip initial data synchronization. Select this option if you want to perform your own database and
log backups of your primary databases. For more information, see Start Data Movement on an
Always On Secondary Database (SQL Server).

Installing and configuring Always On Availability Groups (Windows) 16


10. The Validation page verifies whether the values you specified meet the requirements of the New
Availability Group Wizard. Select Previous to return to an earlier page and change one or more values.
Then select Next to return to the Validation page, and select Re-run Validation. We haven’t done any
listener configuration yet, so the wizard is showing warning. We can ignore the warning for now.

11. On the Summary page, review your choices for the new availability group. To make a change, select
Previous to return to the relevant page. After making the change, select Next to return to the
Summary page.

Installing and configuring Always On Availability Groups (Windows) 17


12. The Progress page displays the progress of the steps for creating the availability group (configuring
endpoints, creating the availability group, and joining the secondary replica to the group).

Installing and configuring Always On Availability Groups (Windows) 18


13. When these steps complete, the Results page displays the result of each step. If all the steps succeed,
the new availability group is completely configured. If any of the steps result in an error, you might need
to manually complete the configuration or use a wizard for the failed step. For information about the
cause of a given error, select the associated Error link in the Result column. When the wizard
completes, select Close.

As an alternative to using the New Availability Group Wizard, you can use Transact-SQL or SQL Server
PowerShell cmdlets. For more information, see Create an Availability Group (Transact-SQL) or Create
an Availability Group (SQL Server PowerShell).

14. In the Object Explorer, expand AlwaysOn High Availability, and then expand Availability Groups.
You should now see the new availability group in this container. Right-click MyExpenseAg (Primary)
and select Show Dashboard.

Installing and configuring Always On Availability Groups (Windows) 19


You can now see the created secondary replica on WIN-AGNODE03.

Overview of availability group listeners


You can provide client connectivity to the database of a given availability group by creating an availability
group listener. An availability group listener is a virtual network name (VNN) to which clients can connect to
access a database in a primary or secondary replica of an Always On availability group. An availability group
listener enables a client to connect to an availability replica without knowing the name of the physical instance
of SQL Server to which the client is connecting. The client connection string doesn’t need to be modified to
create a connection to the current location of the current primary replica.
Creating or configuring availability group listeners
1. In Object Explorer, connect to the server instance that hosts the primary replica of the availability group,
and then select the server name to expand the server tree.
2. Expand the AlwaysOn High Availability node and the Availability Groups node so you can see
MyExpenseAG (Primary).

Installing and configuring Always On Availability Groups (Windows) 20


3. To create a listener, right-click MyExpenseAG (Primary) and select Add Listener. This opens the
New Availability Group Listener dialog box. For more information, see Add Availability Group
Listener (Dialog Box).

4. Specify the name of the listener and port. Here we specified MyExpenseList for the Listener DNS
Name and 1433 for the Port. In the Network Mode list, select Static IP. Specify 10.0.2.125 as IP.
Select Add and specify the IP from the available IP range. Here we have used 10.0.2.125. Select OK.
It will take some time for the listener to be created.

Installing and configuring Always On Availability Groups (Windows) 21


You can see that the listener is created.

Installing and configuring Always On Availability Groups (Windows) 22


Registering the AG listener’s DNS name
To enable the application to access data by using the listener’s DNS name, we need to add a DNS entry for
the listener. Alternatively, we can specify the IP address when configuring the MyExpenses application. To add
a new DNS entry:
1. In Server Manager, select Tools, and then select DNS.

Installing and configuring Always On Availability Groups (Windows) 23


2. In DNS Manager, in the left-hand pane, expand DNS, WIN-AGDC, and Forward Lookup Zones, and
then select aoag.sqlserver.labs.

3. On the Action menu, select New Host (A or AAAA).

Installing and configuring Always On Availability Groups (Windows) 24


4. Enter MyExpenseList for the listener name and 10.0.2.125 for the IP address, ensure that the Create
associated pointer (PTR) record and Allow any authenticated user to update DNS records with
the same owner name boxes are cleared, and select Add Host.

5. Right-click the domain node in the tree (aoag.sqlserver.labs) and then select Reload.

Installing and configuring Always On Availability Groups (Windows) 25


Installing and configuring Always On Availability Groups (Windows) 26
6. Select Yes to continue.

7. Right-click the WIN-AGDC node in the tree and select Clear Cache.

The AG listener should now be reachable after we configure the application.

Installing and configuring Always On Availability Groups (Windows) 27


Configuring the sample application
Next, to demonstrate continuity in the event of failover, we’ll configure the sample application to connect to our
data.

Configuring and launching the app


We will be using a sample application with database called MyExpenses. To ensure high availability on the
sample application, we’ll connect to the availability group listener as opposed to a specific SQL Server . This
application is available at https://github.com/Microsoft/developer-immersion-data.
This app runs on Node.js, which has been pre-installed for this lab.

Setting up the sample application project


1. In Visual Studio (VS) Code, open the web app folder by selecting File, Open Folder (Ctrl+K, Ctrl+O)
and navigating to C:\HOL Files\AlwaysonAg\MyExpense.Web.
2. First, we’ll restore the npm packages before running the application. Press CTRL+SHIFT+P or the F1
key in VS Code.
3. In the Command Palette, type task and select Tasks: Run Task.

Installing and configuring Always On Availability Groups (Windows) 28


4. Execute the Prepare procedure for the application.

5. You’ll need to update the database connection details for the app as well. Open
the ./server/config/server.config.js file and update the login password and port information accordingly.
User name: sa
Password: Pass@word1
Port: 1433
Change the value for host to the DNS name of the listener: MyExpenseList.

Installing and configuring Always On Availability Groups (Windows) 29


6. Press F5 in VS Code to run the application.
You can see it executing and listening on port 8000:

7. Open a browser and go to http://localhost:8000.

Installing and configuring Always On Availability Groups (Windows) 30


Installing and configuring Always On Availability Groups (Windows) 31
8. Select SIGN IN to ensure that the app shows the data.

NOTE: The application is not authenticated.

Installing and configuring Always On Availability Groups (Windows) 32


Executing a manual failover
We can perform a planned manual failover without data loss on an Always On availability group by using SQL
Server Management Studio, Transact-SQL, or Microsoft PowerShell in SQL Server 2017. An availability group
fails over at the level of an availability replica. A planned manual failover, like any Always On Availability Group
failover, transitions a secondary replica to a primary role and, concurrently, transitions the former primary
replica to the secondary role. In this lab, we’ll manually trigger a failover by using Transact-SQL.
1. Connect to the server instance that hosts the target secondary replica (win-agnode03).
Use the ALTER AVAILABILITY GROUP statement, as follows:

ALTER AVAILABILITY GROUP MyExpenseAG FAILOVER;


GO
This statement manually fails over the MyExpenseAG availability group to the connected secondary
replica. This will cause the failover to execute once communication is lost with the primary node.

Installing and configuring Always On Availability Groups (Windows) 33


2. Return to SSMS and check the status of the availability group by expanding AlwaysOn High
Availability, right-clicking Availability Groups, and then selecting Show Dashboard. You can see
that the secondary replica (win-agnode03) becomes the primary because of the failover.

3. In a web browser, refresh the sample application page. Navigate to a couple pages to confirm that the
app is still operating.

Next, we’ll explore how to monitor availability groups.

Installing and configuring Always On Availability Groups (Windows) 34


Monitoring availability groups
To monitor the properties and state of an Always On availability group, you can use the following tools:
• System Center Monitoring pack for SQL Server. The Monitoring pack for SQL Server is the
recommended solution for monitoring availability groups, availability replicas, and availability
databases. Monitoring features that are particularly relevant to Always On availability groups
include the following:

• Automatic discoverability of availability groups, availability replicas, and availability databases


from among hundreds of computers. This enables you to easily keep track of your Always On
Availability Groups inventory.

• Fully capable System Center Operations Manager alerting and ticketing. These features provide
detailed knowledge that enables faster resolution of problems.

• A custom extension to Always On Health monitoring that uses Policy Based management
(PBM).

• Health roll ups from availability databases to availability replicas.

• Custom tasks that manage Always On availability groups from the System Center Operations
Manager console.
For more information, see System Center Monitoring Pack for SQL Server.

• Transact-SQL. Always On Availability Groups catalog and dynamic management views provide
information about your availability groups and their replicas, databases, listeners, and WSFC
cluster environment. For more information, see Monitor Availability Groups (Transact-SQL).

Installing and configuring Always On Availability Groups (Windows) 35


• To monitor Always On availability groups, SERVERPROPERTY built-in function:

SELECT SERVER PROPERTY (‘IsHadrEnabled’);


SELECT SERVER PROPERTY (‘HadrManagerStatus’);

This Returns server property information about whether Always On Availability Groups is enabled
and, if so, whether it has started on the server instance.

• To monitor the WSFC cluster that hosts a local server instance that is enabled for Always On
Availability Groups, use the following view:

sys.dm_hadr_cluster

If the WSFC node that hosts an instance of SQL Server with Always On Availability Groups
enabled has WSFC quorum, sys.dm_hadr_cluster returns a row that exposes the cluster
name and information about the quorum. If the WSFC node has no quorum, no rows are
returned.

Installing and configuring Always On Availability Groups (Windows) 36


• To monitor the availability groups for which the server instance hosts an availability replica, use the
following view:

sys.availability_groups

This returns a row for each availability group for which the local instance of SQL Server hosts
an availability replica. Each row contains a cached copy of the availability group metadata.

Installing and configuring Always On Availability Groups (Windows) 37


• SQL Server Management Studio. The Object Explorer Details pane displays basic information
about the availability groups hosted on the instance of SQL Server to which you are connected. See
Use Object Explorer Details to Monitor Availability Groups for more information. Properties dialog
boxes enable you to view the properties of availability groups, replicas, and listeners. In some
cases, you can also change property values in properties dialog boxes. See View Availability Group
Properties and View Availability Replica Properties for more information.

Installing and configuring Always On Availability Groups (Windows) 38


Summary and additional information
The Always On Availability Groups feature is one part of a comprehensive solution for high availability and
disaster recovery. In this lab, we explored availability groups in a single Windows Server failover cluster,
maintaining continuity for a data application by using automatic failover and synchronous commit, with our
application data connection pointed to an availability group listener. This configuration is one of several options
for ensuring that mission-critical applications remain available to users or customers in the face of possible
outages.

You can find more information on Always On Availability Groups on Microsoft’s documentation site. And
download the latest version of SQL Server from the SQL Server downloads page at Microsoft.com

Installing and configuring Always On Availability Groups (Windows) 39


Appendix
Enabling Always On Availability Groups
The Always On Availability Groups feature must be enabled on every instance of SQL Server 2017 that is to
participate in an availability group. Before you begin, please visit Enabling Always On Availability Groups
Prerequisites to read the prerequisite requirements.
Steps to enable Always On Availability Groups
1. Connect to the WSFC node that hosts the SQL Server instance on which you want to enable Always
On Availability Groups.
2. On the Start menu, point to All Programs, point to Microsoft SQL Server 2016, point to
Configuration Tools, and then select SQL Server Configuration Manager.
3. In SQL Server Configuration Manager, select SQL Server Services, right-click SQL Server
(<instance name>), where <instance name> is the name of a local server instance for which you want
to enable Always On Availability Groups, and click Properties.
4. Select the Always On High Availability tab.
5. Verify that the Windows failover cluster name field contains the name of the local failover cluster. If
this field is blank, this server instance currently does not support Always On Availability Groups. This
could be because the local computer is not a cluster node, because the WSFC cluster has been shut
down, or because your edition of SQL Server 2016 doesn’t support Always On Availability Groups.
6. Select the Enable Always On Availability Groups check box, and then select OK.
SQL Server Configuration Manager saves your change. You must manually restart the SQL Server service.
This enables you to choose a restart time that is best for your business requirements. When the SQL Server
service restarts, Always On will be enabled, and the IsHadrEnabled server property will be set to 1.
You can also use PowerShell to enable Always On Availability Groups.

Repeat steps 1 through 6 for each SQL Server in your cluster.


Setting up the application database
The repository for MyExpenses app includes a data generation tool that will create the Expenses database and
populate it with sample data. Follow these steps to set up the database:
1. Open the Node.js command prompt from the Start menu. Change the directory to C:\HOL
Files\src\Expenses.SQLLinux\MyExpenses.DataGenerator, type npm install and press Enter. This
will download the dependency modules for the data generator app.
2. Open the project folder in VS Code. Select File, select Open Folder (Ctrl+K, Ctrl+O), and navigate to
/home/labuser/HandsOnLabs/Apps/developer-immersion-data-master/source/
Expenses.SQLLinux/MyExpenses.DataGenerator.

3. In the explorer panel, expand the config sub folder and select the file named db.config.js.

Installing and configuring Always On Availability Groups (Windows) 40


4. You’ll need to supply the SA password you provided in provisioning the instance of SQL Server.

5. The default port in the sample db.config.js file expects TCP port 1433. In db.config.js, change this value
to 1433.
6. Return to the Node.js command prompt and enter node app.js.

7. You’ll be presented with 2 data load options. Choose option 1. The process will take a few seconds to
run. It will create a database named Expenses and populate the data needed to run the app.

Installing and configuring Always On Availability Groups (Windows) 41

Vous aimerez peut-être aussi