代码改变世界

Clickhouse集群扩容和收缩

  abce  阅读(787)  评论(0编辑  收藏  举报
集群节点信息
节点
IP
Clickhouse node1
21.198.165.19
Clickhouse node2
21.198.165.20
Clickhouse node3
21.198.165.21
集群配置信息
没有做分片,只是做了副本。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<remote_servers>
    <!-- 集群名称,确保和config.xml中的<remote_servers incl="clickhouse_cluster_name" />的incl的中的参数一致-->
    <clickhouse_test>
        <!--分片1-->
        <shard>
            <!-- 表示是否只将数据写入其中一个副本,默认为false,表示写入所有副本,在复制表的情况下可能会导致重复和不一致,所以这里一定要改为true。-->
            <internal_replication>true</internal_replication>
            <replica>
                <host>xh-mytest-ck01</host>
                <port>9000</port>
            </replica>
            <!--复制集1-->
            <replica>
                <host>xh-mytest-ck02</host>
                <port>9000</port>
            </replica>
            <!--复制集2-->
            <replica>
                <host>xh-mytest-ck03</host>
                <port>9000</port>
            </replica>
        </shard>
    </clickhouse_test>
</remote_servers>

查看一下集群现状:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
) select * from cluster;
 
SELECT *
FROM cluster
 
Query id: 89bfb719-447a-445c-8e30-fd5f62fc3415
 
 
0 rows in set. Elapsed: 0.002 sec.
 
Received exception from server (version 23.2.3):
Code: 60. DB::Exception: Received from localhost:9000. DB::Exception: Table system.cluster doesn't exist. (UNKNOWN_TABLE)
 
xh-mytest-ck01.sci-inv.cn :) select * from clusters;
 
SELECT *
FROM clusters
 
Query id: 29f7cacf-5ab4-46bd-a6c2-b2ec3be5ca93
 
┌─cluster─────────┬─shard_num─┬─shard_weight─┬─replica_num─┬─host_name──────┬─host_address──┬─port─┬─is_local─┬─user────┬─default_database─┬─errors_count─┬─slowdowns_count─┬─estimated_recovery_time─┐
│ clickhouse_test │         1 │            1 │           1 │ xh-mytest-ck01 │ 21.198.165.19 │ 9000 │        1 │ default │                  │            0 │               0 │                       0 │
│ clickhouse_test │         1 │            1 │           2 │ xh-mytest-ck02 │ 21.198.165.20 │ 9000 │        0 │ default │                  │            0 │               0 │                       0 │
│ clickhouse_test │         1 │            1 │           3 │ xh-mytest-ck03 │ 21.198.165.21 │ 9000 │        0 │ default │                  │            0 │               0 │                       0 │
└─────────────────┴───────────┴──────────────┴─────────────┴────────────────┴───────────────┴──────┴──────────┴─────────┴──────────────────┴──────────────┴─────────────────┴─────────────────────────┘

 

移除副本
副本节点下线,主副本仍然保留相同的数据,因此不需要对副本节点数据进行备份。
主副本和副本间是对等的,因此下线主副本和副本原理相同,下线主副本后,副本会选举成为主副本提供服务。
现在将第三个节点下线。
从集群配置中移除第三个节点的副本配置,修改成如下配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<remote_servers>
    <!-- 集群名称,确保和config.xml中的<remote_servers incl="clickhouse_cluster_name" />的incl的中的参数一致-->
    <clickhouse_test>
        <!--分片1-->
        <shard>
            <!-- 表示是否只将数据写入其中一个副本,默认为false,表示写入所有副本,在复制表的情况下可能会导致重复和不一致,所以这里一定要改为true。-->
            <internal_replication>true</internal_replication>
            <replica>
                <host>xh-mytest-ck01</host>
                <port>9000</port>
            </replica>
            <!--复制集1-->
            <replica>
                <host>xh-mytest-ck02</host>
                <port>9000</port>
            </replica>
        </shard>
    </clickhouse_test>
</remote_servers>
zookeeper已经支持动态修改配置信息,移除节点配置后,不用重启。
修改完成后,再次查看集群的信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
:) select * from clusters;
 
SELECT *
FROM clusters
 
Query id: 1ee7baf4-3569-48b0-b7bf-c073b5ec2bb0
 
┌─cluster─────────┬─shard_num─┬─shard_weight─┬─replica_num─┬─host_name──────┬─host_address──┬─port─┬─is_local─┬─user────┬─default_database─┬─errors_count─┬─slowdowns_count─┬─estimated_recovery_time─┐
│ clickhouse_test │         1 │            1 │           1 │ xh-mytest-ck01 │ 21.198.165.19 │ 9000 │        1 │ default │                  │            0 │               0 │                       0 │
│ clickhouse_test │         1 │            1 │           2 │ xh-mytest-ck02 │ 21.198.165.20 │ 9000 │        0 │ default │                  │            0 │               0 │                       0 │
└─────────────────┴───────────┴──────────────┴─────────────┴────────────────┴───────────────┴──────┴──────────┴─────────┴──────────────────┴──────────────┴─────────────────┴─────────────────────────┘
 
2 rows in set. Elapsed: 0.003 sec.
至此,移除节点操作就结束了!
 
增加副本
增加副本节点,仅需要修改新节点配置,然后执行建库和建表语句,语句执行后会从ZooKeeper中取主节点信息,然后同步数据。但是这样主节点没有副本节点信息,执行查询语句时只会选择主节点,而副本节点查询语句可以在主节点和副本节点间随机选择,建议主节点同样修改配置,使得主节点副本节点配置保持一致。
 
这里仍然使用上面的环境做测试。
在将节点添加到集群之前,将上面的数据清理掉,模拟一个新的环境。
将新增节点添加到集群:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<remote_servers>
    <!-- 集群名称,确保和config.xml中的<remote_servers incl="clickhouse_cluster_name" />的incl的中的参数一致-->
    <clickhouse_test>
        <!--分片1-->
        <shard>
            <!-- 表示是否只将数据写入其中一个副本,默认为false,表示写入所有副本,在复制表的情况下可能会导致重复和不一致,所以这里一定要改为true。-->
            <internal_replication>true</internal_replication>
            <replica>
                <host>xh-mytest-ck01</host>
                <port>9000</port>
            </replica>
            <!--复制集1-->
            <replica>
                <host>xh-mytest-ck02</host>
                <port>9000</port>
            </replica>
            <!--复制集2-->
            <replica>
                <host>xh-mytest-ck03</host>
                <port>9000</port>
            </replica>
        </shard>
    </clickhouse_test>
</remote_servers>
添加后,查看集群的信息。可以看到,新的节点已经可以看到了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
:) select * from clusters;
 
SELECT *
FROM clusters
 
Query id: 0e1942f4-8422-44ea-89b0-6ee045b98a46
 
┌─cluster─────────┬─shard_num─┬─shard_weight─┬─replica_num─┬─host_name──────┬─host_address──┬─port─┬─is_local─┬─user────┬─default_database─┬─errors_count─┬─slowdowns_count─┬─estimated_recovery_time─┐
│ clickhouse_test │         1 │            1 │           1 │ xh-mytest-ck01 │ 21.198.165.19 │ 9000 │        0 │ default │                  │            0 │               0 │                       0 │
│ clickhouse_test │         1 │            1 │           2 │ xh-mytest-ck02 │ 21.198.165.20 │ 9000 │        0 │ default │                  │            0 │               0 │                       0 │
│ clickhouse_test │         1 │            1 │           3 │ xh-mytest-ck03 │ 21.198.165.21 │ 9000 │        1 │ default │                  │            0 │               0 │                       0 │
└─────────────────┴───────────┴──────────────┴─────────────┴────────────────┴───────────────┴──────┴──────────┴─────────┴──────────────────┴──────────────┴─────────────────┴─────────────────────────┘
 
3 rows in set. Elapsed: 0.003 sec.
 
创建数据库和表。创建好库和表之后,会自动同步数据。
 
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-03-17 Linux集群内多路径设别名称的一致性
点击右上角即可分享
微信分享提示