Clickhouse集群部署(3分片1副本)

Clickhouse集群部署

3台Linux服务器,搭建Clickhouse集群3分片1副本模式

1、安装Java、Clickhouse、Zookeeper

shell 复制代码
dpkg -i clickhouse-client_23.2.6.34_amd64.deb
dpkg -i clickhouse-common-static_23.2.6.34_amd64.deb
dpkg -i clickhouse-server_23.2.6.34_amd64.deb
# 默认安装在/etc文件夹下,/etc/clickhouse-server /etc/clickhouse-client,傻瓜式安装即可

tar -zxvf apache-zookeeper-3.7.0-bin.tar.gz # zookeeper解压启动即可

2、修改Clickhouse配置文件config.xml,加入到clickhouse标签内,尽量是加入到主配置文件中,单独配置然后在包含进主配置,容易因为一些权限问题导致集群部署不成功

3、修改Zookeeper配置文件,zoo.cfg

4、重启Clickhouse、zookeeper

systemctl restart clickhouse-server

./ZkServer.sh stop/start

5、配置application-pro.yml

yaml 复制代码
clickhouse:  # Clickhouse集群,3分片每个分片一个副本
  driver-class-name: ru.yandex.clickhouse.ClickHouseDriver #具体看pom文件中引用的是哪个
  jdbc-url: jdbc:clickhouse://192.168.3.19:8123,192.168.3.20:8123,192.168.3.21:8123/ck_cluster #集群名称
  username: jane
  password: 123456

hikari:
  connection-timeout: 20000
  maximum-pool-size: 60
  minimum-idle: 60
xml 复制代码
config.xml
<!--新增-->
	<!--ck集群节点-->
	<remote_servers>
		<!-- 集群名称,可以修改-->
		<ck_cluster>
			<!-- 配置三个分片, 每个分片对应一台机器, 为每个分片配置一个副本 -->
			<!--分片1-->
			<shard>
				<!-- 权重:新增一条数据的时候有多大的概率落入该分片,默认值:1 -->
				<weight>1</weight>
				<internal_replication>true</internal_replication>
				<replica>
					<host>192.168.3.19</host>
					<port>9000</port> <!-- 注意集群内部之间通讯用9000端口 -->
					<user>default</user>
					<password>Jane</password>
					<compression>true</compression>
				</replica>
			</shard>
			<!--分片2-->
			<shard>
				<weight>1</weight>
				<internal_replication>true</internal_replication>
				<replica>
					<host>192.168.3.20</host>
					<port>9000</port> <!-- 注意集群内部之间通讯用9000端口 -->
					<user>default</user>
					<password>Jane</password>
					<compression>true</compression>
				</replica>
			</shard>
			<!--分片3-->
			<shard>
				<weight>1</weight>
				<internal_replication>true</internal_replication>
				<replica>
					<host>192.168.3.21</host>
					<port>9000</port> <!-- 注意集群内部之间通讯用9000端口 -->
					<user>default</user>
					<password>Jane</password>
					<compression>true</compression>
				</replica>
			</shard>
		</ck_cluster>
	</remote_servers>

	<!--zookeeper相关配置-->
	<zookeeper>
		<node index="1">
			<host>192.168.3.19</host>
			<port>2181</port>
		</node>
		<node index="2">
			<host>192.168.3.20</host>
			<port>2181</port>
		</node>
		<node index="3">
			<host>192.168.3.21</host>
			<port>2181</port>
		</node>
	</zookeeper>

	<macros>
      	<shard>1</shard> <!--当前所属哪个分片-->
		<replica>192.168.3.19</replica> <!--分片所属副本的编号,可以是数字也可以是IP,在创建表时会用到-->
	</macros>
	<networks>
		<ip>::/0</ip>
	</networks>

	<!--压缩相关配置-->
	<clickhouse_compression>
		<case>
			<min_part_size>10000000000</min_part_size>
			<min_part_size_ratio>0.01</min_part_size_ratio>
			<method>lz4</method>
			<!--压缩算法lz4压缩⽐zstd快, 更占磁盘-->
		</case>
	</clickhouse_compression>
xml 复制代码
zoo.cfg --增加
dataDir=/data/zookeeper #zookeeper数据文件存储路径
server.1=192.168.3.19:2888:3888
server.2=192.168.3.20:2888:3888
server.3=192.168.3.21:2888:3888

验证

sql 复制代码
SELECT * FROM system.zookeeper WHERE path = '/clickhouse';
GRANT CREATE TABLE ON . TO 'default' WITH GRANT OPTION;
GRANT ALTER TABLE, DROP TABLE ON . TO 'default' WITH GRANT OPTION;
SHOW GRANTS FOR 'default';
SELECT * FROM system.clusters;
dpkg -i clickhouse-client_23.2.6.34_amd64.deb
./clickhouse-client --host="192.168.3.19" --port="9000" --user="default" --password="Jane"
sql 复制代码
--在各个节点建库、本地表
create database testdb;
--在各个节点建分布表
CREATE TABLE person_local
(
    `ID` Int8,
    `Name` String,
    `BirthDate` Date
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(BirthDate)
ORDER BY (Name, BirthDate)
SETTINGS index_granularity=8192;

--分布表(Distributed)本⾝不存储数据,相当于路由,需要指定集群名、数据库名、数据表名、分⽚KEY. 这⾥分⽚⽤rand()函数,表⽰随机分⽚。
CREATE TABLE person_all AS person_local
ENGINE = Distributed(ck_cluster, testdb, person_local, rand());

1、检查集群状态

sql 复制代码
登录Clickhouse集群某一节点数据库
cd /usr/bin
./clickhouse-client --host='192.168.3.19' --port='9000' --user='Jane' --password='Jane1234'
select * from system.cluster; # 查看集群信息,有输出
┌─cluster────┬─shard_num─┬─replica_num─┬─host_name──┬─host_address─┬─default_database─┐
│ ck_cluster │         1 │           1 │ 192.168.3.19 │ 192.168.3.19   │                  │
│ ck_cluster │         2 │           1 │ 192.168.3.20 │ 192.168.3.20   │                  │
│ ck_cluster │         3 │           1 │ 192.168.3.21 │ 192.168.3.21   │                  │
└────────────┴───────────┴─────────────┴────────────┴──────────────┴──────────────────┘
select database,table,is_readonly,replica_name,replica_path from system.replicas; # 了解每个副本的同步情况和状态,从而进行相应的管理和优化操作。
┌─database─┬─table─────────┬─replica_name─┬─replica_path───────────────────────────────────────────┐
│ default  │ channelLog    │ 192.168.3.19   │ /clickhouse/tables/1/channelLog/replicas/192.168.3.19    │
│ default  │ cycle         │ 192.168.3.19   │ /clickhouse/tables/1/cycle/replicas/192.168.3.19         │
│ default  │ info          │ 192.168.3.19   │ /clickhouse/tables/1/info/replicas/192.168.3.19          │
│ default  │ newChannelLog │ 192.168.3.19   │ /clickhouse/tables/1/newChannelLog/replicas/192.168.3.19 │
│ default  │ record        │ 192.168.3.19   │ /clickhouse/tables/1/record/replicas/192.168.3.19        │
│ default  │ step          │ 192.168.3.19   │ /clickhouse/tables/1/step/replicas/192.168.3.19          │
│ default  │ test_ck       │ 192.168.3.19   │ /clickhouse/tables/1/test_ck/replicas/192.168.3.19       │
└──────────┴───────────────┴──────────────┴─────────────────────────────────────────────────────

select * from system.macros;  # 查看分片|副本信息
┌─macro───┬─substitution─┐
│ replica │ 192.168.3.19   │
│ shard   │ 1            │
└─────────┴──────────────┘

2、检查ZooKeeper配置

如果使用ZooKeeper,可以直接在ClickHouse数据库中输入命令来验证ZooKeeper配置是否正确:

sql 复制代码
SELECT * FROM system.zookeeper WHERE path = '/clickhouse'; #可以实时监控ZooKeeper节点的状态和数据,确保集群的协调和同步正常进行.

3、创建ReplicatedMergeTree测试表

在任一节点上创建一个使用ReplicatedMergeTree引擎的测试表,以测试ZooKeeper同步功能是否正常:

4、创建Distributed引擎测试表/验证数据同步

创建一个Distributed引擎的测试表,并进行数据插入和查询操作,以验证集群的分布式功能是否正常工作。

sql 复制代码
# 创建一个分布式测试表测试数据分片是否正常。已经配置了zookeeper,所以创建表的DDL语句也会同步到其他节点上
CREATE TABLE test_local ON CLUSTER ck_cluster (
                         id Int32,
                         name String
                     ) ENGINE = MergeTree()
                     ORDER BY id;

CREATE TABLE test ON CLUSTER ck_cluster AS test_local
                     ENGINE = Distributed(ck_cluster, default, test_local, rand());
# 参数含义:ck_cluster集群名称,default数据库,test_local表,rand()分布式表采用的分配算法,除了这个还有sipHash64(字段名)
# 注意:分布式表是基于已经存在的本地表来实现的,分布式表相当于视图,本身并不存储数据,写分布式表,分布式表会将数据发送到各个机器上。查分布式表,会聚合所有机器的数据显示)
INSERT INTO test (id, name) VALUES (1, 'Alice'), (2, 'Bob'); # 在某个节点上执行插入操作
select * from test; # 在任一Clickhouse节点,直接查询分布式表可以看到这些数据,数据存在,则表示数据同步配置成功
select * from test_local; # 在其他Clickhouse节点上查询,只能看到自己本地的数据
SHOW databases;
show tables;
SELECT currentDatabase();

5、检查服务状态

在每台节点上启动/查看/重启/停止ClickHouse服务,以确保服务运行正常:

shell 复制代码
service clickhouse-server start # 或者systemctl restart clickhouse-server
service clickhouse-server status
service clickhouse-server restart
service clickhouse-server stop

通过以上步骤,可以全面验证ClickHouse集群是否部署成功并且正常运行

参考文章:

https://blog.csdn.net/weixin_44123540/article/details/119042654

https://blog.csdn.net/clearlxj/article/details/121774940

相关推荐
=蜗牛=5 天前
Docker 简单部署 ClickHouse 超详细图文步骤
clickhouse·docker·容器·部署·图文
狼与自由6 天前
clickhouse log引擎
clickhouse
狼与自由7 天前
clickhouse AggregatingMergeTree
clickhouse
狼与自由7 天前
clickhouse ReplacingMergeTree
android·clickhouse
狼与自由8 天前
clickhouse中的分区
clickhouse
狼与自由8 天前
clickhouse 查询
clickhouse
狼与自由8 天前
clickhouse mergeTree
clickhouse
狼与自由9 天前
clickhouse建表
clickhouse
简简单单就是我_hehe9 天前
clickhouse内置函数和关键词总结
clickhouse
狼与自由9 天前
clickhouse引擎
clickhouse·c#·linq