一、集群部署簡介
部署的詳情可以看官網
先部署兩個server,三個keeper[zookeeper]
clickhouse之前依賴的存儲是zookeeper,后來改為了keeper,官網給出了原因
所以這就決定了clickhouse有兩種安裝方式,依賴于keeper做存儲或者依賴于zookeeper做存儲
二、zookeeper作為存儲
1.zookeeper安裝
zookeeper安裝可以看之前的文章
2.clickhouse部署
修改配置文件
2.1 修改listen_host
<!-- Same for hosts without support for IPv6: -->
<listen_host>0.0.0.0</listen_host> <!-- 把這里注釋去掉,允許所有地址可以訪問 -->
2.2 修改存儲路徑
<!-- Path to data directory, with trailing slash. --><path>/var/lib/clickhouse/</path>
2.3 添加集群配置
<!--這屬于兩個分片,每個分片語一個副本的架構-->
<remote_servers><cluster_2S_1R> <!--定義的集群名字--><shard><internal_replication>true</internal_replication><replica><host>chnode1</host><port>9000</port></replica></shard><shard><internal_replication>true</internal_replication><replica><host>chnode2</host><port>9000</port></replica></shard></cluster_2S_1R></remote_servers><!--
注意,上面的寫法是放到兩個shard里,也可放到一個shard,下面是單一分片兩副本的寫法,如果放到不同的shard里macros的配置就得不同了
<remote_servers><cluster_2S_1R> <!--定義的集群名字--><shard><internal_replication>true</internal_replication><replica><host>chnode1</host><port>9000</port></replica><replica><host>chnode2</host><port>9000</port></replica></shard></cluster_2S_1R></remote_servers>-->
2.4配置zookeeper
<zookeeper><node><host>example1</host><port>2181</port></node><node><host>example2</host><port>2181</port></node><node><host>example3</host><port>2181</port></node>
</zookeeper>
2.5配置macros
<!--如果是單分片量副本的配置-->
<!-- 配置分片macros變量,在用client創建表的時候會自動帶入,第一臺ck的配置 --><macros><shard>01</shard><replica>chnode1</replica> <!-- 這里指定當前集群節點的名字或者IP --></macros>
<!-- 配置分片macros變量,在用client創建表的時候會自動帶入,第二臺ck的配置 --><macros><shard>01</shard><replica>chnode2</replica> <!-- 這里指定當前集群節點的名字或者IP --></macros>
3.啟動clickhouse
systemctl start clickhouse-server.service
systemctl enable clickhouse-server.service
4.登錄機器并檢查集群
4.1登錄
# 登錄
clickhouse-client
# 查看集群信息
select * from system.clusters
4.2建表
CREATE TABLE t1 ON CLUSTER cluster_2S_1R
(`ts` DateTime,`uid` String,`biz` String
)
ENGINE = ReplicatedMergeTree('/clickhouse/test1/tables/{shard}/t1', '{replica}')
PARTITION BY toYYYYMMDD(ts)
ORDER BY ts
SETTINGS index_granularity = 8192# 出現如下報錯
Received exception from server (version 23.6.2):
Code: 159. DB::Exception: Received from localhost:9000. DB::Exception: Watching task /clickhouse/task_queue/ddl/query-0000000004 is executing longer than distributed_ddl_task_timeout (=180) seconds. There are 2 unfinished hosts (0 of them are currently active), they are going to execute the query in background. (TIMEOUT_EXCEEDED)
# 這個報錯是某些ck服務異常才出現的報錯,我這是因為我配置文件里的remote_server里的host ip寫錯了,相當于找不到服務了,修改后重啟就好了
4.3 測試dml
目前DDL生效,但是插入數據在其他節點不生效
查看節點2的clickhouse日志,其中會有如下報錯
2023.08.10 15:49:54.836507 [ 8514 ] {} <Error> test1.t1 (*****-48d4-44ed-9bad-2a03410321a9): auto DB::StorageReplicatedMergeTree::processQueueEntry(ReplicatedMergeTreeQueue::SelectedEntryPtr)::(anonymous class)::operator()(LogEntryPtr &) const: Code: 198. DB::Exception: Not found address of host: bj-ck3. (DNS_ERROR), Stack trace (when copying this message, always include the lines below):
可以看到這里是因為域名無法解析,因為ZooKeeper 里面存儲的是hosts域名,不是IP,所以需要配置/etc/hosts
192.168.1.1 bj-ck1
192.168.1.2 bj-ck2
192.168.1.3 bj-ck3
ps: /etc/hosts的配置里,如果配置多個的話,是以第一個為準,其他都類似別名么
比如192.168.1.1配置如下:192.168.1.1 bj-1 bj-2
如果別的機器是以域名訪問192.168.1.1,如果別的機器只配置了192.168.1.1 bj-2,其實是解析不到192.168.1.1的
三、keeper作為存儲
ClickHouse Keeper 提供數據復制和分布式 DDL 查詢執行的協調系統。 ClickHouse Keeper 與 Apache ZooKeeper 兼容。 此配置在端口 9181 上啟用 ClickHouse Keeper。
注意:
如果出于任何原因更換或重建 Keeper 節點,請勿重復使用現有的 server_id。 例如,如果重建了server_id為2的Keeper節點,則將其server_id設置為4或更高。
分片和副本降低了分布式 DDL 的復雜性。 配置的值會自動替換到您的 DDL 查詢中,從而簡化您的 DDL。
1.安裝并啟動keeper
# 安裝clickhouse-keeper
sudo apt-get install -y clickhouse-keeper
# 啟用并啟動clickhouse-keeper
sudo systemctl enable clickhouse-keeper
sudo systemctl start clickhouse-keeper
sudo systemctl status clickhouse-keeper
2.修改keeper配置文件keeper_config.xml
<keeper_server><tcp_port>9181</tcp_port><!-- 這里是主要的修改位置,保證集群中每個幾點的id是唯一的 --><server_id>1</server_id><log_storage_path>/var/lib/clickhouse/coordination/logs</log_storage_path><snapshot_storage_path>/var/lib/clickhouse/coordination/snapshots</snapshot_storage_path><coordination_settings><operation_timeout_ms>10000</operation_timeout_ms><min_session_timeout_ms>10000</min_session_timeout_ms><session_timeout_ms>100000</session_timeout_ms><raft_logs_level>information</raft_logs_level><!-- All settings listed in https://github.com/ClickHouse/ClickHouse/blob/master/src/Coordination/CoordinationSettings.h --></coordination_settings><!-- enable sanity hostname checks for cluster configuration (e.g. if localhost is used with remote endpoints) --><hostname_checks_enabled>true</hostname_checks_enabled><!-- 這里是第二處需要變更的位置,需要把集群中的keeper配置上 --><raft_configuration><server><id>1</id><!-- Internal port and hostname --><hostname>192.168.1.1</hostname><port>9234</port></server><server><id>2</id><!-- Internal port and hostname --><hostname>192.168.1.2</hostname><port>9234</port></server><server><id>3</id><!-- Internal port and hostname --><hostname>192.168.1.3</hostname><port>9234</port></server><!-- Add more servers here --></raft_configuration></keeper_server><clickhouse><zookeeper><node index="1"><host>chnode1</host><port>9181</port></node><node index="2"><host>chnode2</host><port>9181</port></node><node index="3"><host>chnode3</host><port>9181</port></node></zookeeper></clickhouse>
3.clickhouse的配置
clickhouse的配置與zookeeper作為存儲時的配置幾乎一致,只需要把zookeeper的配置注釋掉即可
ps: 這里還有個小插曲,使用keeper的時候發現dml的數據又一次不同步了,查看clickhouse-server.err.log,發現有如下報錯
2023.08.16 11:19:00.782071 [ 8566 ] {} <Error> ConfigReloader: Error updating configuration from '/etc/clickhouse-server/config.xml' config.: Code: 999. Coordination::Exception: Connection loss, path: All connection tries failed while connecting to ZooKeeper
使用telnet后發現確實telnet不通,于是修改keeper的配置文件keeper_config.xml,添加如下內容
<listen_host>0.0.0.0</listen_host>
重啟keeper
systemctl restart clickhouse-keeper