集群架构 #
一、集群概述 #
ArangoDB集群支持水平扩展和高可用,由多个节点组成分布式系统。
1.1 集群组件 #
| 组件 | 说明 |
|---|---|
| Agent | 集群协调服务(Agency) |
| DBServer | 数据存储节点 |
| Coordinator | 协调节点,处理客户端请求 |
1.2 集群架构 #
text
┌─────────────────────────────────────────────────────────┐
│ 客户端请求 │
└─────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ Coordinators │
│ ┌───────────┐ ┌───────────┐ ┌───────────┐ │
│ │Coordinator│ │Coordinator│ │Coordinator│ │
│ │ (1) │ │ (2) │ │ (3) │ │
│ └───────────┘ └───────────┘ └───────────┘ │
└─────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ Agency │
│ ┌───────────┐ ┌───────────┐ ┌───────────┐ │
│ │ Agent │ │ Agent │ │ Agent │ │
│ │ (1) │ │ (2) │ │ (3) │ │
│ └───────────┘ └───────────┘ └───────────┘ │
└─────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ DBServers │
│ ┌───────────┐ ┌───────────┐ ┌───────────┐ │
│ │ DBServer │ │ DBServer │ │ DBServer │ │
│ │ (1) │ │ (2) │ │ (3) │ │
│ └───────────┘ └───────────┘ └───────────┘ │
└─────────────────────────────────────────────────────────┘
1.3 最小集群配置 #
text
生产环境最小配置:
├── Agent: 3个(奇数个)
├── DBServer: 3个
└── Coordinator: 2个以上
二、集群部署 #
2.1 Docker部署 #
创建docker-compose.yml:
yaml
version: '3.8'
services:
agent1:
image: arangodb/arangodb:3.12
command: arangod --server.endpoint tcp://0.0.0.0:8529 --agency.id 1 --agency.size 3 --agency.endpoint tcp://agent1:8529 --agency.endpoint tcp://agent2:8529 --agency.endpoint tcp://agent3:8529
ports:
- "8529:8529"
agent2:
image: arangodb/arangodb:3.12
command: arangod --server.endpoint tcp://0.0.0.0:8529 --agency.id 2 --agency.size 3 --agency.endpoint tcp://agent1:8529 --agency.endpoint tcp://agent2:8529 --agency.endpoint tcp://agent3:8529
agent3:
image: arangodb/arangodb:3.12
command: arangod --server.endpoint tcp://0.0.0.0:8529 --agency.id 3 --agency.size 3 --agency.endpoint tcp://agent1:8529 --agency.endpoint tcp://agent2:8529 --agency.endpoint tcp://agent3:8529
dbserver1:
image: arangodb/arangodb:3.12
command: arangod --server.endpoint tcp://0.0.0.0:8529 --cluster.my-address tcp://dbserver1:8529 --cluster.my-local-info dbserver1 --cluster.my-role DBSERVER --cluster.agency-endpoint tcp://agent1:8529 --cluster.agency-endpoint tcp://agent2:8529 --cluster.agency-endpoint tcp://agent3:8529
depends_on:
- agent1
- agent2
- agent3
coordinator1:
image: arangodb/arangodb:3.12
command: arangod --server.endpoint tcp://0.0.0.0:8529 --cluster.my-address tcp://coordinator1:8529 --cluster.my-local-info coordinator1 --cluster.my-role COORDINATOR --cluster.agency-endpoint tcp://agent1:8529 --cluster.agency-endpoint tcp://agent2:8529 --cluster.agency-endpoint tcp://agent3:8529
ports:
- "8530:8529"
depends_on:
- agent1
- agent2
- agent3
- dbserver1
2.2 使用ArangoDB Starter #
bash
arangodb --starter.data-dir=/data/cluster --starter.join host1,host2,host3
2.3 手动部署 #
启动Agent:
bash
arangod \
--server.endpoint tcp://0.0.0.0:8529 \
--agency.id 1 \
--agency.size 3 \
--agency.endpoint tcp://agent1:8529 \
--agency.endpoint tcp://agent2:8529 \
--agency.endpoint tcp://agent3:8529
启动DBServer:
bash
arangod \
--server.endpoint tcp://0.0.0.0:8529 \
--cluster.my-address tcp://dbserver1:8529 \
--cluster.my-role DBSERVER \
--cluster.agency-endpoint tcp://agent1:8529 \
--cluster.agency-endpoint tcp://agent2:8529 \
--cluster.agency-endpoint tcp://agent3:8529
启动Coordinator:
bash
arangod \
--server.endpoint tcp://0.0.0.0:8529 \
--cluster.my-address tcp://coordinator1:8529 \
--cluster.my-role COORDINATOR \
--cluster.agency-endpoint tcp://agent1:8529 \
--cluster.agency-endpoint tcp://agent2:8529 \
--cluster.agency-endpoint tcp://agent3:8529
三、分片 #
3.1 分片概念 #
分片是将数据分布到多个节点的技术:
text
数据分布:
├── 集合数据按分片键分布
├── 每个分片存储在不同的DBServer
└── 自动负载均衡
3.2 创建分片集合 #
javascript
db._create("orders", {
numberOfShards: 4,
shardKeys: ["userId"]
});
3.3 分片策略 #
| 策略 | 说明 |
|---|---|
| hash | 哈希分片(默认) |
| enterprise-hash | 企业版哈希分片 |
| community-compat | 社区版兼容 |
3.4 分片键选择 #
text
分片键选择原则:
├── 选择高基数字段
├── 避免热点数据
├── 考虑查询模式
└── 避免频繁更新
3.5 查看分片信息 #
javascript
db.orders.properties();
四、复制 #
4.1 复制概念 #
复制提供数据冗余和高可用:
text
复制模型:
├── Leader: 主分片,处理写入
├── Follower: 从分片,复制数据
└── 自动故障转移
4.2 配置复制因子 #
javascript
db._create("users", {
numberOfShards: 4,
replicationFactor: 3
});
4.3 写关注 #
javascript
db._create("critical_data", {
numberOfShards: 4,
replicationFactor: 3,
writeConcern: 2
});
| 写关注 | 说明 |
|---|---|
| 1 | 只需Leader确认 |
| 2 | Leader + 1个Follower确认 |
| 3 | 所有副本确认 |
4.4 读关注 #
aql
FOR user IN users
OPTIONS { readConcern: "linearizable" }
RETURN user
五、高可用 #
5.1 故障转移 #
text
故障转移流程:
├── 检测节点故障
├── 选举新的Leader
├── 更新路由信息
└── 客户端自动重连
5.2 节点健康检查 #
javascript
var instanceInfo = require("@arangodb/cluster").instanceInfo();
print(instanceInfo);
5.3 集群状态 #
javascript
require("@arangodb/cluster").clusterHealth();
5.4 维护模式 #
javascript
require("@arangodb/cluster").serverMaintenanceMode("dbserver1", true);
六、集群管理 #
6.1 添加节点 #
添加DBServer:
bash
arangodb add dbserver --starter.data-dir=/data/new-dbserver
添加Coordinator:
bash
arangodb add coordinator --starter.data-dir=/data/new-coordinator
6.2 移除节点 #
javascript
require("@arangodb/cluster").cleanoutServer("dbserver1");
6.3 集群均衡 #
javascript
require("@arangodb/cluster").rebalanceShards();
6.4 查看集群信息 #
javascript
db._statistics();
七、集群监控 #
7.1 健康状态 #
javascript
require("@arangodb/cluster").clusterHealth();
7.2 分片分布 #
javascript
require("@arangodb/cluster").shardDistribution();
7.3 节点统计 #
javascript
require("@arangodb/cluster").instanceInfo();
7.4 监控指标 #
| 指标 | 说明 |
|---|---|
| cluster.health | 集群健康状态 |
| cluster.shards | 分片状态 |
| cluster.collections | 集合分布 |
| cluster.servers | 服务器状态 |
八、性能优化 #
8.1 分片优化 #
text
优化建议:
├── 合理设置分片数量
├── 选择合适的分片键
├── 避免跨分片查询
└── 监控分片均衡
8.2 复制优化 #
text
优化建议:
├── 合理设置复制因子
├── 配置适当的写关注
├── 监控复制延迟
└── 避免过度复制
8.3 查询优化 #
aql
FOR user IN users
FILTER user.userId == @userId
RETURN user
8.4 网络优化 #
text
优化建议:
├── 使用高速网络
├── 节点间低延迟
├── 合理的网络拓扑
└── 监控网络流量
九、备份恢复 #
9.1 集群备份 #
bash
arangodump --server.endpoint tcp://coordinator:8529 \
--server.username root \
--server.password password \
--output-directory /backup
9.2 集群恢复 #
bash
arangorestore --server.endpoint tcp://coordinator:8529 \
--server.username root \
--server.password password \
--input-directory /backup
9.3 增量备份 #
bash
arangodump --server.endpoint tcp://coordinator:8529 \
--output-directory /backup/incremental \
--incremental true
十、总结 #
集群架构要点:
- 组件:Agent、DBServer、Coordinator
- 分片:数据分布到多个节点
- 复制:数据冗余和高可用
- 故障转移:自动选举新Leader
- 管理:添加/移除节点、均衡分片
下一步,让我们学习管理与运维!
最后更新:2026-03-27