Use Case 4: Database specific load balancing
-
Enable the load balancing feature, and configure a load balancing virtual server of type MSSQL or MySQL.
-
Configure the services that host the database, and bind the services to the virtual server. The monitor needs valid user credentials to log on to the database server, so you must configure a database user account on each of the servers and then add the user account to the NetScaler appliance.
-
Then, you configure an MSSQL-ECV or MYSQL-ECV monitor and bind the monitor to each service.
-
Finally, you must test the configuration to ensure that it is working as intended. Before you perform these configuration tasks, make sure you understand how database specific load balancing works.
How database specific load balancing works
Enable load balancing
Enable load balancing by using the CLI
enable ns feature LB
show ns feature
> enable ns feature LoadBalancing
Done
> show ns feature
Feature Acronym Status
------- ------- ------
1) Web Logging WL OFF
2) Surge Protection SP ON
3) Load Balancing LB ON
.
.
.
24) NetScaler Push push OFF
Done
Enable load balancing by using the GUI
Configure a load balancing virtual server for database specific load balancing
Configure a load balancing virtual server for database specific load balancing using the CLI
add lb vserver <name> <serviceType> <ipAddress> <port> -dbsLb ENABLED
show lb vserver <name>
Configure services
Configure database users
nsconfig file.
Add a database user by using the CLI
add db user <username> - password <password>
add db user nsdbuser -password dd260427edf
Add a database user by using the GUI
Reset the password of a database user by using the CLI
set db user <username> -password <password>
set db user nsdbuser -password dd260538abs
Reset the password of database users by using the GUI
Remove a database user by using the CLI
rm db user <username>
rm db user nsdbuser
Remove a database user by using the GUI
Configure a monitor to retrieve the names of active databases
select name from sys.databases where state=0
Configure a monitor to retrieve the names of all the active databases hosted on a service by using the CLI
add lb monitor <monitorName> <type> -userName <string> -sqlQuery <text> -evalRule <expression> -storedb ENABLED
show lb monitor <monitorName>
Configure a monitor to retrieve the names of all the active databases hosted on a service by using the GUI
-
Navigate to Traffic Management > Load Balancing > Monitors and configure a monitor of type MSSQL-ECV or MYSQL-ECV.
-
In Special Parameters, specify a user name, query, and a rule. For example, for MSSQL-ECV, the query must be "select name from sys.databases where state=0"), and a rule must be MSSQL.RES.TYPE.NE(ERROR). For MYSQL-ECV, the query must be "show databases" and a rule must be MYSQL.RES.TYPE.NE(ERROR).
Availability groups deployment support for MSSQL
| Service | List of Active Databases on the Service |
|---|---|
| S1 | DB1, DB2, DB3, DB4 |
| S2 | DB3, DB4 |
| S3 | DB3, DB4 |
| S4 | DB1, DB2 |
| S5 | DB1, DB2 |
| Availability Group | Databases | Services representing the Servers in the Availability Group |
|---|---|---|
| AV1 | DB1, DB2 | S1, S4, S5 |
| AV2 | DB3, DB4 | S1, S2, S3 |
-
A READ query for AV1 is load balanced between S4 and S5. S1 is the primary for AV1.
-
A WRITE query for AV1 is directed to L1.
-
A READ query for AV2 is load balanced between S1 and S3. S2 is the primary for AV2.
-
A WRITE query for AV1 is directed to L2.
Sample configuration
-
Configure load balancing and content switching virtual servers.
-
add lb vserver lbwrite -dbslb enabled -
add lbvserver lbread MSSQL -dbslb enabled -
add csvserver csv MSSQL 1.1.1.10 1433
-
-
Configure two listener services, one for each availability group, and five services S1 through S5 representing databases DB1 through DB4.
-
add service L1 1.1.1.11 MSSQL 1433 -
add service L2 1.1.1.12 MSSQL 1433 -
add service s1 1.1.1.13 MSSQL 1433 -
add service s2 1.1.1.14 MSSQL 1433 -
add service s3 1.1.1.15 MSSQL 1433 -
add service s4 1.1.1.16 MSSQL 1433 -
add service s5 1.1.1.17 MSSQL 1433
-
-
Bind the services to the load balancing virtual servers.
-
bind lbvserver lbwrite L1 -
bind lbvserver lbwrite L2 -
bind lbvserver lbread s1 -
bind lbvserver lbread s2 -
bind lbvserver lbread s3 -
bind lbvserver lbread s4 -
bind lbvserver lbread s5
-
-
Configure database users.
-
add db user nsdbuser1 -password dd260427edf -
add db user nsdbuser2 -password ccd1234xyzw
-
-
Configure two monitors, monitor_L1 and monitor_L2 for each listener service, to retrieve the list of active databases in that availability group. Add a monitor, monitor1 to retrieve the list of databases for the secondary database server instance.
-
add lb monitor monitor_L1 MSSQL-ECV -userName user1 -sqlQuery "SELECT name FROM sys.databases a INNER JOIN sys.dm_hadr_availability_replica_states b ON a.replica_id=b.replica_id INNER JOIN sys.availability_group_listeners c on b.group_id = c.group_id INNER JOIN sys.availability_group_listener_ip_addresses d on c.listener_id = d.listener_id WHERE b.role = 1 and d.ip_address like '1.1.1.11'" -evalRule "MSSQL.RES.TYPE.NE(ERROR)” –storedb ENABLED -
add lb monitor monitor_L2 MSSQL-ECV -userNameuser1 -sqlQuery "SELECT name FROM sys.databases a INNER JOIN sys.dm_hadr_availability_replicca_states b ON a.replica_id=b.replica_id INNER JOIN sys.availability_group_listeners c on b.group_id = c.group_id INNER JOIN sys.availability_group_listener_ip_addresses d on c.listener_id = d.listener_id WHERE b.role = 1 and d.ip_address like '1.1.1.12'" -evalRule "MSSQL.RES.TYPE.NE(ERROR)" -storedb ENABLED -
add lb monitor monitor1 MSSQL-ECV -userNameuser1 -sqlQuery "SELECT name FROM sys.databases a INNER JOIN sys.dm_hadr_availability_replica_states b ON a.replica_id=b.replica_id WHERE b.role = 2" -evalRule "MSSQL.RES.TYPE.NE(ERROR)" -storedb ENABLED
-
-
Configure read and write policies.
-
add cs policy pol_write -rule "MSSQL.REQ.QUERY.TEXT.CONTAINS("insert")" -
add cs policy pol_read -rule "MSSQL.REQ.QUERY.TEXT.CONTAINS("select")"
-
-
Bind the policies to the content switching virtual server.
-
bind csvserver csv -targetLBVserver lbwrite -policyName pol_write -priority 11 -
bind csvserver csv -targetLBVserver lbread -policyName pol_read -priority 12
-
-
Bind monitors to the services. Bind monitors to services L1 and L2 to get the list of active databases for the availability group for which it is the listener. Bind monitors to all the services that are bound to the read-only virtual server.
-
bind service L1 -monitorName monitor_L1 -
bind service L2 -monitorName monitor_L2 -
bind service s1 -monitorName monitor1 -
bind service s2 -monitorName monitor1 -
bind service s3 -monitorName monitor1 -
bind service s4 -monitorName monitor1 -
bind service s5 -monitorName monitor1
-
Configuration examples for MSSQL virtual server
add lb vserver DBSpecificLB1 MSSQL 192.0.2.10 1433 -dbsLb ENABLED
Done
show lb vserver DBSpecificLB1
DBSpecificLB1 (192.0.2.10:1433) - MSSQL Type: ADDRESS
. . .
DBS_LB: ENABLED
Done
add lb monitor mssql-monitor1 MSSQL-ECV -userName user1 -sqlQuery "select name from sys.databases where state=0" -evalRule "MSSQL.RES.TYPE.NE(ERROR)" -storedb EN
Done
show lb monitor mssql-monitor1
1) Name.......: mssql-monitor1 Type......: MSSQL-ECV
...
Special parameters: Database.....:""
User name.....:"user1"
Query..:select name from sys.databases where state=0 EvalRule...:MSSQL.RES.TYPE.NE(ERROR)
Version...:70 STORE_DB...:ENABLED
Done
Configuration examples for MySQL virtual server
add lb vserver DBSpecificLB1 MYSQL 192.0.2.10 3306 -dbsLb ENABLED
Done
show lb vserver DBSpecificLB1
DBSpecificLB1 (192.0.2.10:3306) - MYSQL Type: ADDRESS
. . .
DBS_LB: ENABLED
Done
add service msservice1 5.5.5.5 MYSQL 3306
add lb monitor mysql-monitor1 MYSQL-ECV -userName user1 -sqlQuery "show databases" -evalRule "MYSQL.RES.TYPE.NE(ERROR)" -storedb ENABLED
Done
show lb monitor mysql-monitor1
1) Name.......: mysql-monitor1 Type......: MYSQL-ECV State....: ENABLED
...
Special parameters: Database.....:""
User name.....:"user1" Query..:show databases
EvalRule...:MYSQL.RES.TYPE.NE(ERROR) STORE_DB...:ENABLED
Done