本期作者:邓亚运
37 互娱高级 DBA,负责公司 MySQL,Redis,Hadoop,Clickhouse 集群的管理和维护。
前面的文章简单的介绍了 ClickHouse(第01期)
,以及也进行了简单的性能测试(第02期)
。本期说说集群的搭建以及数据复制,如果复制数据需要 zookeeper 配合。
环境:
1)3 台机器。我这里是 3 台虚拟机,都安装了 clickhouse。
2)绑定 hosts,其实不绑定也没关系,配置文件里面直接写 ip。(3 台机器都绑定 hosts,如下)
192.168.0.10 db_server_yayun_01
192.168.0.20 db_server_yayun_02
192.168.0.30 db_server_yayun_03
3)创建配置文件,默认这个配置文件是不存在的。/etc/clickhouse-server/config.xml 有提示,如下:
If element has 'incl' attribute, then for it's value will be used corresponding substitution from another file.
By default, path to file with substitutions is /etc/metrika.xml. It could be changed in config in 'include_from' element.
Values for substitutions are specified in /yandex/name_of_substitution elements in that file.
配置文件 /etc/metrika.xml 内容如下:
true
db_server_yayun_01
9000
true
db_server_yayun_02
9000
true
db_server_yayun_03
9000
192.168.0.30
2181
192.168.0.10
::/0
10000000000
0.01
lz4
3 台机器的配置文件都一样,唯一有区别的是:
192.168.0.10
服务器 ip 是多少这里就写多少,其实不写 ip 也没关系,3 台机器不重复就行。这里是复制需要用到的配置。还有 zk 的配置如下:
192.168.0.30
2181
我的 zk 是安装在 30 的机器上面的,只安装了一个实例,生产环境肯定要放到单独的机器,并且配置成集群。配置文件修改好以后 3 台服务器重启。
官方文档给的步骤是:
ClickHouse deployment to cluster
ClickHouse cluster is a homogenous cluster. Steps to set up:
1. Install ClickHouse server on all machines of the cluster
2. Set up cluster configs in configuration file
3. Create local tables on each instance
4. Create a Distributed table
前面 2 步都搞定了,下面创建本地表,再创建 Distributed 表。(3台 机器都创建,DDL 不同步,蛋疼,也可以使用 ON Cluster 语法。在一个节点执行即可。)
CREATE TABLE ontime_local (FlightDate Date,Year UInt16) ENGINE = MergeTree(FlightDate, (Year, FlightDate), 8192);
CREATE TABLE ontime_all AS ontime_local ENGINE = Distributed(perftest_3shards_1replicas, default, ontime_local, rand())
插入数据(随便一台机器就行):
:) insert into ontime_all (FlightDate,Year)values('2001-10-12',2001);
INSERT INTO ontime_all (FlightDate, Year) VALUES
Ok.
1 rows in set. Elapsed: 0.013 sec.
:) insert into ontime_all (FlightDate,Year)values('2002-10-12',2002);
INSERT INTO ontime_all (FlightDate, Year) VALUES
Ok.
1 rows in set. Elapsed: 0.004 sec.
:) insert into ontime_all (FlightDate,Year)values('2003-10-12',2003);
INSERT INTO ontime_all (FlightDate, Year) VALUES
Ok.
我这里插入了 3 条数据。下面查询看看(任何一台机器都可以):
:) select * from ontime_all;
SELECT *
FROM ontime_all
┌─FlightDate─┬─Year─┐
│ 2001-10-12 │ 2001 │
└────────────┴──────┘
┌─FlightDate─┬─Year─┐
│ 2002-10-12 │ 2002 │
└────────────┴──────┘
┌─FlightDate─┬─Year─┐
│ 2003-10-12 │ 2003 │
└────────────┴──────┘
→ Progress: 3.00 rows, 12.00 B (48.27 rows/s., 193.08 B/s.)
3 rows in set. Elapsed: 0.063 sec.
:)
当在其中一台机器上面查询的时候,抓包其他机器可以看见是有请求的。
tcpdump -i any -s 0 -l -w - dst port 9000
那么关闭其中一台机器呢?
:) select * from ontime_all;
SELECT *
FROM ontime_all
┌─FlightDate─┬─Year─┐
│ 2001-10-12 │ 2001 │
└────────────┴──────┘
┌─FlightDate─┬─Year─┐
│ 2002-10-12 │ 2002 │
└────────────┴──────┘
┌─FlightDate─┬─Year─┐
│ 2003-10-12 │ 2003 │
└────────────┴──────┘
↓ Progress: 6.00 rows, 24.00 B (292.80 rows/s., 1.17 KB/s.) Received exception from server:
Code: 279. DB::Exception: Received from localhost:9000, ::1. DB::NetException. DB::NetException: All connection tries failed. Log:
Code: 210, e.displayText() = DB::NetException: Connection refused: (db_server_yayun_02:9000, 192.168.0.20), e.what() = DB::NetException
Code: 210, e.displayText() = DB::NetException: Connection refused: (db_server_yayun_02:9000, 192.168.0.20), e.what() = DB::NetException
Code: 210, e.displayText() = DB::NetException: Connection refused: (db_server_yayun_02:9000, 192.168.0.20), e.what() = DB::NetException
可以看见已经抛错了,竟然不是高可用?是的,就是不是高可用,上面的配置是设置 3 个分片,但是没有副本,所以在挂掉一个节点以后,就会存在问题。后面又看到了文档的另外一种配置方法,那就是配置 2 个节点,副本 2 个,经过测试高可用没有问题,另外也是分布式并行查询。感兴趣的同学可以自行测试。https://clickhouse.yandex/reference_en.html#Distributed
下面进行数据复制的测试,zk 已经配置好了,直接建表测试(3 台机器都创建):
CREATE TABLE ontime_replica (FlightDate Date,Year UInt16) ENGINE = ReplicatedMergeTree('/clickhouse_perftest/tables/ontime_replica','{replica}',FlightDate,(Year, FlightDate),8192);
插入数据测试:
insert into ontime_replica (FlightDate,Year)values('2018-10-12',2018);
参考资料:
https://clickhouse.yandex/reference_en.html#Distributed
https://clickhouse.yandex/tutorial.html
关于 ClickHouse 的技术内容,你们还有什么想知道的吗?赶紧留言告诉小编吧!