跨数据中心部署拓扑
本文以典型的两地三中心为例,介绍跨数据中心部署的拓扑以及关键参数。本文示例所涉及的城市是上海(即 sha)和北京(即 bja 和 bjb)。
拓扑信息
| 实例 | 个数 | 物理机配置 | BJ IP | SH IP | 配置 |
|---|---|---|---|---|---|
| TiDB | 5 | 16 VCore 32GB 1 | 10.0.1.1 10.0.1.2 10.0.1.3 10.0.1.4 | 10.0.1.5 | 默认端口 全局目录配置 |
| PD | 5 | 4 VCore 8GB 1 | 10.0.1.6 10.0.1.7 10.0.1.8 10.0.1.9 | 10.0.1.10 | 默认端口 全局目录配置 |
| TiKV | 5 | 16 VCore 32GB 4TB (nvme ssd) 1 | 10.0.1.11 10.0.1.12 10.0.1.13 10.0.1.14 | 10.0.1.15 | 默认端口 全局目录配置 |
| Monitoring & Grafana | 1 | 4 VCore 8GB 1 500GB (ssd) | 10.0.1.16 | 默认端口 全局目录配置 |
拓扑模版
跨机房配置模板
1. # Tip: PD priority needs to be manually set using the PD-ctl client tool. such as, member Leader_priority PD-name numbers.
2. # Global variables are applied to all deployments and used as the default value of
3. # the deployments if a specific deployment value is missing.
4. #
5. # Abbreviations used in this example:
6. # sh: Shanghai Zone
7. # bj: Beijing Zone
8. # sha: Shanghai Datacenter A
9. # bja: Beijing Datacenter A
10. # bjb: Beijing Datacenter B
12. global:
13. user: "tidb"
14. ssh_port: 22
15. deploy_dir: "/tidb-deploy"
16. data_dir: "/tidb-data"
17. monitored:
18. node_exporter_port: 9100
19. blackbox_exporter_port: 9115
20. deploy_dir: "/tidb-deploy/monitored-9100"
21. server_configs:
22. tidb:
23. log.level: debug
24. log.slow-query-file: tidb-slow.log
25. tikv:
26. server.grpc-compression-type: gzip
27. readpool.storage.use-unified-pool: true
28. readpool.storage.low-concurrency: 8
29. pd:
30. replication.location-labels: ["zone","dc","rack","host"]
31. replication.max-replicas: 5
32. label-property: # TiDB 5.2 及以上版本默认不支持 label-property 配置。若要设置副本策略,请使用 Placement Rules。
33. reject-leader:
34. - key: "dc"
35. value: "sha"
36. pd_servers:
37. - host: 10.0.1.6
38. - host: 10.0.1.7
39. - host: 10.0.1.8
40. - host: 10.0.1.9
41. - host: 10.0.1.10
42. tidb_servers:
43. - host: 10.0.1.1
44. - host: 10.0.1.2
45. - host: 10.0.1.3
46. - host: 10.0.1.4
47. - host: 10.0.1.5
48. tikv_servers:
49. - host: 10.0.1.11
50. ssh_port: 22
51. port: 20160
52. status_port: 20180
53. deploy_dir: "/tidb-deploy/tikv-20160"
54. data_dir: "/tidb-data/tikv-20160"
55. config:
56. server.labels:
57. zone: bj
58. dc: bja
59. rack: rack1
60. host: host1
61. - host: 10.0.1.12
62. ssh_port: 22
63. port: 20161
64. status_port: 20181
65. deploy_dir: "/tidb-deploy/tikv-20161"
66. data_dir: "/tidb-data/tikv-20161"
67. config:
68. server.labels:
69. zone: bj
70. dc: bja
71. rack: rack1
72. host: host2
73. - host: 10.0.1.13
74. ssh_port: 22
75. port: 20160
76. status_port: 20180
77. deploy_dir: "/tidb-deploy/tikv-20160"
78. data_dir: "/tidb-data/tikv-20160"
79. config:
80. server.labels:
81. zone: bj
82. dc: bjb
83. rack: rack1
84. host: host1
85. - host: 10.0.1.14
86. ssh_port: 22
87. port: 20161
88. status_port: 20181
89. deploy_dir: "/tidb-deploy/tikv-20161"
90. data_dir: "/tidb-data/tikv-20161"
91. config:
92. server.labels:
93. zone: bj
94. dc: bjb
95. rack: rack1
96. host: host2
97. - host: 10.0.1.15
98. ssh_port: 22
99. port: 20160
100. deploy_dir: "/tidb-deploy/tikv-20160"
101. data_dir: "/tidb-data/tikv-20160"
102. config:
103. server.labels:
104. zone: sh
105. dc: sha
106. rack: rack1
107. host: host1
108. readpool.storage.use-unified-pool: true
109. readpool.storage.low-concurrency: 10
110. raftstore.raft-min-election-timeout-ticks: 50
111. raftstore.raft-max-election-timeout-ticks: 60
112. monitoring_servers:
113. - host: 10.0.1.16
114. grafana_servers:
115. - host: 10.0.1.16
以上 TiDB 集群拓扑文件中,详细的配置项说明见通过 TiUP 部署 TiDB 集群的拓扑文件配置。
关键参数配置
本节介绍跨数据中心部署 TiDB 集群的关键参数配置。
TiKV 参数
- 设置 gRPC 的压缩格式,默认为
none。为提高跨机房部署场景的目标节点间 gRPC 包的传输速度,建议设置为 gzip 格式。
1. server.grpc-compression-type: gzip
- label 配置
由于采用跨机房部署 TiKV,为了避免物理机宕机导致 Raft Group 默认的 5 副本中丢失 3 副本,使集群不可用的问题,可以通过 label 来实现 PD 智能调度,保证同中心、同机柜、同机器 TiKV 实例不会出现 Raft Group 有 3 副本的情况。 - TiKV 配置
相同物理机配置相同的 host 级别 label 信息:
1. config:
2. server.labels:
3. zone: bj
4. dc: bja
5. rack: rack1
6. host: host2
- 防止异地 TiKV 节点发起不必要的 Raft 选举,需要将异地 TiKV 节点发起选举时经过最少的 tick 个数和最多经过的 tick 个数都调大,这两个参数默认设置均为 0。
1. raftstore.raft-min-election-timeout-ticks: 50
2. raftstore.raft-max-election-timeout-ticks: 60
注意:
通过
raftstore.raft-min-election-timeout-ticks和raftstore.raft-max-election-timeout-ticks为 TiKV 节点配置较大的 election timeout tick 可以大幅降低该节点上的 Region 成为 Leader 的概率。但在发生灾难的场景中,如果部分 TiKV 节点宕机,而其它存活的 TiKV 节点 Raft 日志落后,此时只有这个配置了较大的 election timeout tick 的 TiKV 节点上的 Region 能成为 Leader。由于此 TiKV 节点上的 Region 需要至少等待raftstore.raft-min-election-timeout-ticks设置的时间后才能发起选举,因此尽量避免将此配置值设置得过大,以免在这种场景下影响集群的可用性。
PD 参数
- PD 元数据信息记录 TiKV 集群的拓扑信息,根据四个维度调度 Raft Group 副本。
1. replication.location-labels: ["zone","dc","rack","host"]
- 调整 Raft Group 的副本数据量为 5,保证集群的高可用性。
1. replication.max-replicas: 5
- 拒绝异地机房 TiKV 的 Raft 副本选举为 Leader。
1. label-property:
2. reject-leader:
3. - key: "dc"
4. value: "sha"

注意
TiDB 5.2 及以上版本默认不支持 label-property 配置。若要设置副本策略,请使用 Placement Rules。
有关 Label 的使用和 Raft Group 副本数量,详见通过拓扑 label 进行副本调度。

注意
- 无需手动创建配置文件中的
tidb用户,TiUP cluster 组件会在目标主机上自动创建该用户。可以自定义用户,也可以和中控机的用户保持一致。 - 如果部署目录配置为相对路径,会部署在用户的 Home 目录下。
