多租户资源隔离使用示例
操作场景
本章节介绍了多租户资源隔离示例,您可以按照本文的内容自行测试,以快速了解和使用多租户资源隔离功能。
测试环境
- 使用的ECS实例和TaurusDB版集群均位于同一地域和同一可用区。
- TaurusDB版集群节点规格8核32GB,节点数量为2(一个主节点和一个只读节点)。
- 使用默认参数模板,并开启多租户模式。
- ECS实例规格为32U,ECS的规格需大于集群规格,防止压测时ECS的性能达到瓶颈,影响多租户的资源隔离功能。
创建资源配置信息
使用高权限账号root连接数据库实例,并创建多种资源配置信息,用于对租户资源进行限制。
create resource_config cfg_1 MAX_CPU 4 MIN_CPU 1; create resource_config cfg_2 MAX_CPU 2 MIN_CPU 1;
资源配置信息创建完成后,您可以通过查询__taurus_sys__.tenant_resource_configs表来查看当前所有的配置资源。
SELECT * FROM __taurus_sys__.tenant_resource_configs;
创建租户
使用高权限账号连接数据库实例,分别创建tenant_1和tenant_2两个租户,且在创建租户时与指定的资源配置信息绑定。
create tenant tenant_1 RESOURCE_CONFIG cfg_1;
create tenant tenant_2 RESOURCE_CONFIG cfg_2;
- 创建租户tenant_1时,将此租户与资源配置cfg_1绑定。此租户CPU资源限制min_cpu为1,在后台资源调度时,将保证此租户下使用CPU的最小限额为1 CPU。限制此租户使用CPU的最大额度为4 CPU。
- 创建租户tenant_2时,将此租户与资源配置cfg_2绑定。此租户CPU资源限制min_cpu为1,在后台资源调度时,将保证此租户下使用CPU的最小限额为1 CPU。限制此租户使用CPU的最大额度为2 CPU。
租户创建完成后,您可以通过查询__taurus_sys__.tenant表来查看当前的所有租户。
SELECT * FROM __taurus_sys__.tenant;
创建用户和数据库
租户创建成功后,分别在两个租户下创建用户和数据库。
- 在租户tenant_1下创建用户user_1,并授予user_1在tenant_1租户下的所有执行权限。此处可以将user_1@tenant_1当作租户tenant_1下的一个高权限账号。
- 在系统租户下创建'user_1@tenant_1'。
CREATE user 'user_1@tenant_1' IDENTIFIED WITH mysql_native_password BY {password}; - 在高权限账户下,为user_1@tenant_1授予租户tenant_1下的所有执行权限。
GRANT all privileges ON `%@tenant_1`.* to 'user_1@tenant_1'@'%' with grant option;
- 在系统租户下创建'user_1@tenant_1'。
- 在租户tenant_1下创建用户user_2,并授予user_2在tenant_1租户下的所有执行权限。此处可以将user_2@tenant_1当作租户tenant_1下的一个高权限账号。
- 在系统租户下创建'user_2@tenant_1'。
CREATE user 'user_2@tenant_1' IDENTIFIED WITH mysql_native_password BY {password}; - 在高权限账户下,为user_2@tenant_1授予租户tenant_1下的所有执行权限。
GRANT all privileges ON `%@tenant_1`.* to 'user_2@tenant_1'@'%' with grant option;
- 在系统租户下创建'user_2@tenant_1'。
- 用户创建成功后,通过该用户连接数据库实例的SQL语句如下:
mysql --host=xxxxxx -u user_1@tenant_1 -p password
使用该账号登录数据库成功后,您仅可查看当前租户下的数据库。对于系统租户下的数据库以及其他租户下的数据库该账号均不可见。
在租户tenant_1下创建用户以及数据库后,您可以通过相同的方式在tenant_2下创建用户和数据库。
测试场景
完成资源配置、租户、用户和数据库的创建后,通过以下两个场景验证多租户资源隔离的效果。
场景一:租户级CPU资源隔离
- Sysbench压测,查看CPU使用率。
执行以下命令压测,本文只是效果示范,相关参数可根据实际情况调整。
sysbench --db-driver=mysql --mysql-host=host --mysql-port=port --mysql-user=user_1@tenant_1 --mysql-password=password --mysql-db=sbtest --table_size=250000 --tables=25 --time=180 --threads=thread_num --percentile=95 --report-interval=1 oltp_read_write run
sysbench --db-driver=mysql --mysql-host=host --mysql-port=port --mysql-user=user_1@tenant_2 --mysql-password=password --mysql-db=sbtest --table_size=250000 --tables=25 --time=180 --threads=thread_num --percentile=95 --report-interval=1 oltp_read_write run
对租户tenant_1和tenant_2执行sysbench测试,对比不同租户下的CPU使用率和TPS/QPS。
图1 Sysbench压测结果
- 结果分析
10:58-11:00对应tenant_1测试结果,11:01-11:03对应tenant_2测试结果。
tenant_1相比tenant_2的CPU使用率,比例接近2:1;tenant_1相比tenant_2的TPS/QPS使用率,比例接近2:1。
其中tenant_1最多只能使用系统中的4个CPU,tenant_2最多只能使用系统中的2个CPU。
场景二:用户级CPU资源隔离
- 创建用户。
- 创建资源消费组。
通过租户tenant_1连接数据库实例,并创建资源消费组group1和group2,绑定消费组group1到用户user_1,绑定消费组group2到用户user_2。
mysql --host=xxxxxx -u user1@tenant_1 -p password
CALL dbms_resource_manager.create_consumer_group('group1', 'comment'); CALL dbms_resource_manager.create_consumer_group('group2', 'comment'); CALL dbms_resource_manager.set_consumer_group_mapping('USER', 'user_1', 'group1'); CALL dbms_resource_manager.set_consumer_group_mapping('USER', 'user_2', 'group2');资源消费组创建完成后,您可以通过查询__taurus_sys__.consumer_groups表和__taurus_sys__.group_mapping_rules来查看当前所有的配置资源。
SELECT * FROM __taurus_sys__.consumer_groups; SELECT * FROM __taurus_sys__.group_mapping_rules;
- 创建资源计划和指令。
- 通过租户tenant_1连接数据库实例。
- 创建资源计划plan1,配置资源计划指令plan_directive_for_user1,plan_directive_for_user1将plan1与group1关联,并且配置mgmt_p1为10,utilization_limit为40。
CALL dbms_resource_manager.create_plan('plan1', 'comment') CALL dbms_resource_manager.create_plan_directive('plan1', 'group1', 'plan_directive_for_user1', 10, 40); - 在资源计划plan1下,创建资源计划指令plan_directive_for_user2,将plan1与group2关联,并且配置mgmt_p1为10,utilization_limit为40。
CALL dbms_resource_manager.create_plan_directive('plan1', 'group2', 'plan_directive_for_user2', 10, 40); - 资源计划创建完成后,您可以通过查询__taurus_sys__.resource_plans表和__taurus_sys__.plan_directives来查看当前所有的配置资源。
SELECT * FROM __taurus_sys__.resource_plans; SELECT * FROM __taurus_sys__.plan_directives;
- 启用资源计划。
CALL dbms_resource_manager.set_resource_manager_plan('plan1'); - Sysbench压测,执行如下命令,查看CPU使用率。
sysbench --db-driver=mysql --mysql-host=host --mysql-port=port --mysql-user=user_1@tenant_1 --mysql-password=password --mysql-db=sbtest --table_size=250000 --tables=25 --time=180 --threads=thread_num --percentile=95 --report-interval=1 oltp_read_write run
sysbench --db-driver=mysql --mysql-host=host --mysql-port=port --mysql-user=user_2@tenant_1 --mysql-password=password --mysql-db=sbtest --table_size=250000 --tables=25 --time=180 --threads=thread_num --percentile=95 --report-interval=1 oltp_read_write run
对租户tenant_1下的用户user_1和user_2执行sysbench测试,对比不同用户下的CPU使用率和TPS/QPS。
图2 Sysbench压测结果
通过租户tenant_1连接数据库实例,可实时查看租户级和用户级的CPU使用率。
select * from information_schema.CPU_SUMMARY_BY_TENANT order by tenant_name;
select * from information_schema.CPU_SUMMARY_BY_USER order by tenant_name, user_name;
- 结果分析
11:15~11:17对应user_1@tenant_1测试结果,11:19~11:21对应user_2@tenant_1测试结果。
user_1@tenant_1相比user_2@tenant_1的CPU使用率,比例接近1:1;user_1@tenant_1相比user_2@tenant_1的TPS/QPS使用率,比例接近1:1。
其中user_1@tenant_1最多只能使用tenant_1下40%的CPU,user_2@tenant_1同理。