OLAP系列之分析型数据库clickhouse集群扩缩容(三)

发布时间 2023-07-24 15:25:39作者: 杨梅冲

一、集群缩容

1.1 下线节点

步骤:
1.对外停止服务
2.转移数据
3.修改剩余节点配置
4.通知客户端修改节点列表
# 修改90,91服务器配置文件 vim /etc/clickhouse-server/conf.d/metrika.xml 中注释掉第3个shard <remote_servers> <clickhouse_cluster_3shards_1replicas> <shard> <internal_replication>true</internal_replication> <replica> <host>192.168.12.91</host> <port>9000</port> <user>default</user> <password>PeQLg45tJcxZk</password> </replica> </shard> <shard> <internal_replication>true</internal_replication> <replica> <host>192.168.12.90</host> <port>9000</port> <user>default</user> <password>PeQLg45tJcxZk</password> </replica> </shard> <!-- 注销掉 <shard> <internal_replication>true</internal_replication> <replica> <host>192.168.12.88</host> <port>9000</port> <user>default</user> <password>PeQLg45tJcxZk</password> </replica> </shard> --> </clickhouse_cluster_3shards_1replicas> </remote_servers>
# 查询集群 aliyun-47-122-26-25 :) select * from system.clusters; SELECT * FROM system.clusters Query id: 4a1a0d43-9c19-4424-9c87-338b081e8813 ┌─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_cluster_3shards_1replicas │ 1 │ 1 │ 1 │ 192.168.12.91 │ 192.168.12.91 │ 9000 │ 1 │ default │ │ 0 │ 0 │ 0 │ │ clickhouse_cluster_3shards_1replicas │ 2 │ 1 │ 1 │ 192.168.12.90 │ 192.168.12.90 │ 9000 │ 0 │ default │ │ 0 │ 0 │ 0 │ └──────────────────────────────────────┴───────────┴──────────────┴─────────────┴───────────────┴───────────────┴──────┴──────────┴─────────┴──────────────────┴──────────────┴─────────────────┴─────────────────────────┘ 2 rows in set. Elapsed: 0.001 sec. # 将下线节点的数据转移到其它节点,迁移方式可以使用以下方式: clickhouse-client --password use test_log :) insert into test_local select * from remote('192.168.12.88','test_log','test_local','default','password'); Query id: 7c2a0285-0323-4498-82b4-1adadc90aa18 Ok. 0 rows in set. Elapsed: 0.007 sec. test_log:库名 default:账号 test_local:表名 password:密码 # 如果数据库过大可以,可以将数据分别存储在其余2台集群上 # 在集群1上执行下面的sql,将集群3的部分数据写到集群3的本地表中 insert into test_log select * from remote('192.168.12.88:9000','test_log','user','password') where id % 2 = 0; # 在集群2执行下面的sql,将集群3的部分数据写到集群2的本地表中 insert into test_log select * from remote('192.168.12.88:9000','test_log','user','password') where id % 2 = 1;
SELECT *
FROM test_all
Query id: 2a3627db-c64b-45a1-b3be-1868dff84f90
┌─id─┬─name──────┐
│  1 │ zhangsan  │
│  2 │ lisi      │
│  7 │ yw        │
│  8 │ xc        │
│  5 │ zhangquan │
│  6 │ lihua     │
│ 11 │ bb        │
│ 12 │ acca      │
└────┴───────────┘
┌─id─┬─name───┐
│  3 │ wangm  │
│  4 │ lijing │
│  9 │ cx     │
│ 10 │ aa     │
│ 13 │ kkkk   │
└────┴────────┘
13 rows in set. Elapsed: 0.003 sec.

二、集群扩容

# 原配置
        <clickhouse_cluster_3shards_1replicas>
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>192.168.12.91</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>PeQLg45tJcxZk</password>
                </replica>
            </shard>
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>192.168.12.90</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>PeQLg45tJcxZk</password>
                </replica>
            </shard>
        </clickhouse_cluster_3shards_1replicas>


# 新增节点步骤
1.新节点安装clickhouse
2.在新节点新增相关本地表
3.修改旧节点配置
4.通知客户端修改节点列表


# 1.修改配置/etc/clickhouse-server/config.d/metrika.xml,编辑新节点,新增:
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>192.168.12.88</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>PeQLg45tJcxZk</password>
                </replica>
            </shard>

# 2.在新节点新建该集群相关本地表
# 注意和其它节点数据库表面引擎都要一样

# 3.修改集群旧节点的config.xml配置,加上新节点

修改完成后,clickhouse会自动感知到config文件变化,修改的内容会立刻生效

# 4.通知客户端更新节点列表

三、扩容后数据的均匀分布

# 通过设置集群权重,让后面的数据优先写入新节点

        <clickhouse_cluster_3shards_1replicas>
            <shard>
                <weight>1</weight>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>192.168.12.91</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>PeQLg45tJcxZk</password>
                </replica>
            </shard>
            <shard>
                <weight>1</weight>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>192.168.12.90</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>PeQLg45tJcxZk</password>
                </replica>
            </shard>
            <shard>
                <weight>99</weight>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>192.168.12.88</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>PeQLg45tJcxZk</password>
                </replica>
            </shard>
        </clickhouse_cluster_3shards_1replicas>