天天看點

ClickHouse學習系列之四【副本&分片部署說明】

背景

  以前介紹過ClickHouse相關的系列文章,現在繼續說明。本文開始說明ClickHouse的副本與分片,和其他資料庫一樣,ClickHouse也會出現單節點故障和單節點資源到達上限的情況。是以針對上面的2個問題,就出現了副本和分片。副本:能避免單節點故障的問題,類似于MySQL的Replicate和MongoDB的Replicate Set。分片:解決單節點瓶頸的問題,類似于MySQL的分庫分表和MongoDB的Sharding。

部署說明

一:副本

特點:

1. 依賴ZooKeeper,通過其來協調多個副本之間的同步。

2. 表級别的副本,副本是在表級别定義的。

3. 多主架構,可以在任意副本上執行語句。

環境:(版本:21.6.3.14)

執行個體A  執行個體B 
IP 12.16.20.12 12.16.20.17
Port 9000

測試: 

需要配置ZooKeeper的位址,并且需要保證2個執行個體的IP、Port可以通訊(<listen_host>0.0.0.0</listen_host>)。副本模式隻需要修改2個執行個體的配置檔案(config.xml)中的ZooKeeper的資訊:

<zookeeper>
        <node index="1">   -- 單節點,可以配置成叢集模式(3節點)
            <host>12.16.20.17</host>
            <port>2181</port>
        </node>
    </zookeeper>      

建表引擎格式:

ENGINE = ReplicatedMergeTree('zk_path', 'replica_name')      

zk_path:指定在ZooKeeper中建立資料表的路徑,可以自定義。同一張表同一個分片的不同副本,定義相同路徑。為了友善記錄,可以約定為:

'/clickhouse/tables/{shard}/table_name'      
  • shard:分片資訊,如果隻配置副本,分片資訊保持一緻即可。
  • replica_name:副本集名稱,唯一。同一張表同一個分片的不同副本,定義不同名稱。

執行個體A上執行:

CREATE TABLE repl_test
(
    `id` String,
    `price` Float64,
    `create_time` DateTime
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/01/repl_test','dba06')
PARTITION BY toYYYYMM(create_time)
ORDER BY id      

執行個體B上執行:

CREATE TABLE repl_test
(
    `id` String,
    `price` Float64,
    `create_time` DateTime
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/01/repl_test','dba07')
PARTITION BY toYYYYMM(create_time)
ORDER BY id      

2個執行個體上的副本表的zk_path一樣,replica_name不一樣。需要注意的是:和MySQL不一樣,副本表需要在2個執行個體上分别手動建立。

現在在執行個體A上執行插入:

insert into repl_test values('A001',123,'2021-05-01 09:09:00');
insert into repl_test values('A002',123,'2021-06-01 09:09:00');
insert into repl_test values('B001',321,'2021-05-01 09:09:00');
insert into repl_test values('B002',321,'2021-06-01 09:09:00');      

分别在2個執行個體上查詢該表,都能顯示資料,并且在執行個體B上執行插入,也能同步到執行個體A上,達到了多主的要求。

上面的建表語句中,shard、replica使用了寫死的方式:01;dba06、dba07。這樣維護起來比較麻煩,可以通過動态變量來替換,也可以把各個執行個體公有的參數ZooKeeper拎出來一起放到metrika.xml中(/etc/clickhouse-server/config.d/metrika.xml):

<yandex>
    <zookeeper-server>
        <node index="1">
            <host>12.16.20.17</host>
            <port>2181</port>
        </node>
    </zookeeper-server>
    <macros>
        <replica>dba06</replica>  -- 另一個執行個體設定dba07
        <shard>1</shard>
    </macros>
</yandex>      

每個執行個體的配置檔案(config.xml)一緻,隻有<macros>參數裡的不一樣。并且修改config.xml,把上面的配置檔案include進去:

<include_from>/etc/clickhouse-server/config.d/metrika.xml</include_from>
    <zookeeper incl="zookeeper-server" optional="false" />
    <macros incl="macros" optional="true" />      

incl标簽對應的值需要和metrika.xml檔案中的選項組保持一緻。配置完成之後,可以通過以下SQL檢視各個執行個體的變量:

select * from system.macros;      

完成之後,在建立副本表時隻需要在2個執行個體上執行同樣的SQL:

CREATE TABLE repl_test123
(
    `id` String,
    `price` Float64,
    `create_time` DateTime
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/repl_test123','{replica}')
PARTITION BY toYYYYMM(create_time)
ORDER BY id      

這樣就保證了同一個語句不需要修改就在多個執行個體上執行,減少了手動修改導緻的問題。

總結:

副本表的好處:增加了資料的可靠性,減少了單節點故障導緻資料丢失的問題;副本表資料的同步是通過ZooKeeper進行協調的,不需要進行資料的傳輸;并且在各個副本表的各個執行個體上都可以執行SQL,提高了單點寫入的性能問題,也分攤了查詢,讀寫分離。

副本表的不足:需要在多個副本執行個體上建表,運維不友善;也無法解決單節點達到瓶頸的問題。

二:分片

 ClickHouse每個執行個體都可以成為一個分片(shard),在叢集配置中用shard代表分片,用replica代表副本。分片是指将資料拆分,将其分散在不同的執行個體上的過程。将資料分散到不同的機器上,不需要功能強大的伺服器就可以存儲更多的資料和處理更大的負載。每個分片隻負責總資料的一部分,通過一個名為Distributed的引擎進行操作。類似于MongoDB的Sharding。

分片表的引擎可以用任意引擎,但是如果使用非ReplicatedMergeTree引擎的話,副本的資料同步需要Distributed來負責(寫分片和副本),加大了其壓力。是以如果使用分片,并且需要副本,推薦使用ReplicatedMergeTree引擎,資料同步交由其處理,減輕Distributed壓力(需要增加internal_replication參數:<internal_replication>true</internal_replication>)。

環境:

執行個體C  執行個體D 
9010

測試:

和副本一樣,也一樣需要配置ZooKeeper,也要保證2個執行個體的IP、Port可以通訊(<listen_host>0.0.0.0</listen_host>),因為ZooKeeper在各個節點上配置一緻,便于維護,獨立到/etc/clickhouse-server/config.d/metrika.xml 檔案裡。現在測試2種場景:2分片0副本、2分片1副本。

因為各個副本配置的叢集資訊一緻(同一個叢集),是以叢集配置資訊也放到metrika.xml 檔案裡。

場景一:2分片0副本

config.xml 配置檔案各個執行個體一樣(預設即可),隻需要修改include:

<include_from>/etc/clickhouse-server/config.d/metrika.xml</include_from>
    <zookeeper incl="zookeeper-server" optional="false" />
    <macros incl="macros" optional="false" />
    <remote_servers incl="ck_remote_servers" />      

metrika.xml資訊:

<?xml version="1.0"?>
<yandex>

    <zookeeper-server>
        <node index="1">
            <host>12.16.20.17</host>
            <port>2181</port>
        </node>
    </zookeeper-server>

    <macros>
        <replica>dba06</replica>
        <shard>1</shard>
    </macros>

    <ck_remote_servers>
        <test_cluster_0_repl>
            <shard>
                <weight>1</weight>
                <replica>
                    <host>12.16.20.12</host>
                    <port>9000</port>
                    <priority>1</priority>
                </replica>
            </shard>
            <shard>
                <weight>1</weight>
                <replica>
                    <host>12.16.20.17</host>
                    <port>9000</port>
                    <priority>1</priority>
                </replica>
            </shard>
        </test_cluster_0_repl>
    </ck_remote_servers>

</yandex>      

各個執行個體的配置都一樣,除了标簽<macros>各個執行個體不一樣,差別是:

<macros>
        <replica>dba07</replica> -- 可以配置各個節點的本地ip
        <shard>2</shard>
    </macros>      

2分片[1、2]0副本的叢集為[test_cluster_0_repl]已經配置完成,可以通過以下語句檢視:

-- 檢視叢集
select * from system.clusters

-- 檢視macros
select * from system.macros      

注意,基于叢集可以使用 on cluster cluster_name 文法,隻需要在一個節點上執行SQL就可同步到所有節點。如新增表:

CREATE TABLE test_clsuter_a on cluster test_cluster_0_repl
(
    `id` String,
    `price` Float64,
    `create_time` DateTime
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(create_time)
ORDER BY id      

表引擎可以為任意引擎,該叢集下的所有節點都會建立該表(本地表),接着建立Distributed分布式表,文法:

Distributed('叢集名','資料庫名','表名', '分片鍵')      

Distributed引擎表,其自身不存儲資料,而是作為資料分片的代理,自動路由資料到叢集中的各個節點,其中<分片鍵>參數要求傳回整型類型的取值,即按照分片鍵的規則将資料分布到各個節點,如:

  • userid:按照使用者id餘數拆分
  • rand():按照随機數拆分
  • intHash64(userid):按照使用者id散列值劃分
CREATE TABLE test_clsuter_all on cluster test_cluster_0_repl
(
    `id` String,
    `price` Float64,
    `create_time` DateTime
)
ENGINE = Distributed('test_cluster_0_repl','default','test_clsuter_a', rand())      

向分布式表test_clsuter_all寫入資料: 

ClickHouse學習系列之四【副本&amp;分片部署說明】
ClickHouse學習系列之四【副本&amp;分片部署說明】
insert into test_clsuter_all values('X001',123,'2021-05-01 09:09:00');
insert into test_clsuter_all values('X002',123,'2021-06-01 09:09:00');
insert into test_clsuter_all values('W001',321,'2021-05-01 09:09:00');
insert into test_clsuter_all values('W002',321,'2021-06-01 09:09:00');
insert into test_clsuter_all values('K001',123,'2021-05-01 09:09:00');
insert into test_clsuter_all values('K002',123,'2021-06-01 09:09:00');
insert into test_clsuter_all values('M001',321,'2021-05-01 09:09:00');
insert into test_clsuter_all values('M002',321,'2021-06-01 09:09:00');
insert into test_clsuter_all values('S001',123,'2021-05-01 09:09:00');
insert into test_clsuter_all values('S002',123,'2021-06-01 09:09:00');
insert into test_clsuter_all values('G001',321,'2021-05-01 09:09:00');
insert into test_clsuter_all values('G002',321,'2021-06-01 09:09:00');      

View Code

最後在叢集中的任意節點查詢該表,都能看到全量資料。本地表則可以看到節點自身的資料。

場景二:2分片1副本

和場景一配置一樣,唯一的差别就是,在metrika.xml中再添加一個副本:

<?xml version="1.0"?>
<yandex>

    <zookeeper-server>
        <node index="1">
            <host>12.16.20.17</host>
            <port>2181</port>
        </node>
    </zookeeper-server>

    <macros>
        <replica>dba06</replica>
        <shard>1</shard>
    </macros>

    <ck_remote_servers>
        <test_cluster_1_repl>
            <shard>
                <internal_replication>true</internal_replication>  -- 有複制表引擎自己分發同步資料,減少Distributed壓力。
                <weight>1</weight>
                <replica>
                    <host>12.16.20.12</host>
                    <port>9000</port>
                    <priority>1</priority>
                </replica>
                <replica>
                    <host>12.16.20.12</host>
                    <port>9010</port>
                    <priority>1</priority>
                </replica>
            </shard>
            <shard>
                <internal_replication>true</internal_replication>
                <weight>1</weight>
                <replica>
                    <host>12.16.20.17</host>
                    <port>9000</port>
                    <priority>1</priority>
                </replica>
                <replica>
                    <host>12.16.20.17</host>
                    <port>9010</port>
                    <priority>1</priority>
                </replica>
            </shard>
        </test_cluster_1_repl>
    </ck_remote_servers>

</yandex>      

各個節點的差別也是macros:

<macros>
        <replica>dba06</replica> --可以寫本機ip
        <shard>1</shard>    --分片1,分片2
    </macros>      

完成之後,可以通過查詢clusters表來了解各個節點的資訊:

select * from system.clusters;
┌─cluster─────────────┬─shard_num─┬─shard_weight─┬─replica_num─┬─host_name──────┬─host_address───┬─port─┬─is_local─┬─user────┬─default_database─┬─errors_count─┬─slowdowns_count─┬─estimated_recovery_time─┐
│ test_cluster_1_repl │         1 │            1 │           1 │ 12.16.20.12 │ 12.16.20.12 │ 9000 │        1 │ default │                  │            0 │               0 │                       0 │
│ test_cluster_1_repl │         1 │            1 │           2 │ 12.16.20.12 │ 12.16.20.12 │ 9010 │        0 │ default │                  │            0 │               0 │                       0 │
│ test_cluster_1_repl │         2 │            1 │           1 │ 12.16.20.17   │ 12.16.20.17   │ 9000 │        0 │ default │                  │            0 │               0 │                       0 │
│ test_cluster_1_repl │         2 │            1 │           2 │ 12.16.20.17   │ 12.16.20.17   │ 9010 │        0 │ default │                  │            0 │               0 │                       0 │
└─────────────────────┴───────────┴──────────────┴─────────────┴────────────────┴────────────────┴──────┴──────────┴─────────┴──────────────────┴──────────────┴─────────────────┴─────────────────────────┘      

注意,基于叢集可以使用 on cluster cluster_name 文法,隻需要在一個節點上執行SQL即可同步到所有節點。如新增表:

CREATE TABLE repl_abc on cluster test_cluster_1_repl
(
    `id` String,
    `price` Float64,
    `create_time` DateTime
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/repl_abc','{replica}')
PARTITION BY toYYYYMM(create_time)
ORDER BY id      

這樣各個節點(配置設定和副本)都會建立該表(本地表),接着建立Distributed分布式表:

CREATE TABLE repl_abc_all on cluster test_cluster_1_repl
(
    `id` String,
    `price` Float64,
    `create_time` DateTime
)
ENGINE = Distributed('test_cluster_1_repl','testdb','repl_abc', rand())      
ClickHouse學習系列之四【副本&amp;分片部署說明】
ClickHouse學習系列之四【副本&amp;分片部署說明】
insert into repl_abc_all values('X001',123,'2021-05-01 09:09:00');
insert into repl_abc_all values('X002',123,'2021-06-01 09:09:00');
insert into repl_abc_all values('W001',321,'2021-05-01 09:09:00');
insert into repl_abc_all values('W002',321,'2021-06-01 09:09:00');
insert into repl_abc_all values('K001',123,'2021-05-01 09:09:00');
insert into repl_abc_all values('K002',123,'2021-06-01 09:09:00');
insert into repl_abc_all values('M001',321,'2021-05-01 09:09:00');
insert into repl_abc_all values('M002',321,'2021-06-01 09:09:00');
insert into repl_abc_all values('S001',123,'2021-05-01 09:09:00');
insert into repl_abc_all values('S002',123,'2021-06-01 09:09:00');
insert into repl_abc_all values('G001',321,'2021-05-01 09:09:00');
insert into repl_abc_all values('G002',321,'2021-06-01 09:09:00');      

和場景一類似,在任意一節點向分布式表寫入資料,最後在叢集中的任意節點查詢該表,都能看到全量資料。本地表則可以看到節點自身的資料。

注意:建立副本表(ReplicatedMergeTree)的時候,如果資料庫的引擎是Atomic,則在删除表之後馬上重建會遇到:

Code: 253. DB::Exception: Received from localhost:9000. DB::Exception: There was an error on [12.16.20.17:9000]: Code: 253, e.displayText() = DB::Exception: Replica /clickhouse/tables/2/repl_test123/replicas/dba07 already exists. (version 21.6.3.14 (official build)).      

避免該問題的方法有2種:

  • 使用普通資料庫而不是Atomic資料庫(ENGINE = Atomic): CREATE DATABASE … Engine=Ordinary。
  • 修改參數 database_atomic_delay_before_drop_table_sec = 0 
    <database_atomic_delay_before_drop_table_sec>0</database_atomic_delay_before_drop_table_sec>      

這樣,在建立副本表(ReplicatedMergeTree)再删除馬上重建,不會報錯。

以上是沒有使用者認證的部署,線上環境如果有認證(user:default;password:123456)的話,需要在各個shard中添加參數(metrika.xml),各個參數的意義:

<ck_remote_servers>
        <test_cluster_1_repl>
        <!-- 分片1 -->
            <shard>
                <!-- 有複制表直接負責資料分發同步 -->
                <internal_replication>true</internal_replication>
                <!-- 影響資料分布的傾斜,權重越大資料寫入越多 -->
                <weight>1</weight>
                <!-- 分片、副本配置 -->
                <replica>
                    <host>12.16.20.12</host>
                    <port>9000</port>
                    <!-- 節點認證使用者 -->
                    <user>default</user>
                    <!-- 節點使用者密碼 -->
                    <password>123456</password>
                    <!-- 節點權重,值越小優先級越高 -->
                    <priority>1</priority>
                </replica>
                <replica>
                    <host>12.16.20.12</host>
                    <port>9010</port>
                    <user>default</user>
                    <password>123456</password>
            <priority>1</priority>
                </replica>
            </shard>
            <!-- 分片2 -->
            <shard>
                <internal_replication>true</internal_replication>
                <weight>1</weight>
                <replica>
                    <host>12.16.20.17</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>123456</password>
            <priority>1</priority> 
                </replica>
                <replica>
                    <host>12.16.20.17</host>
                    <port>9010</port>
                    <user>default</user>
                    <password>123456</password>
            <priority>1</priority>
                </replica>
            </shard>
        </test_cluster_1_repl>
    </ck_remote_servers>      

這樣,在設定好認證的資料庫上也能正常的進行分片、複制的功能。

分片的好處:解決了單節點達到瓶頸的問題,和通過分布式表Distributed引擎能本身實作資料的路由和聚合。

分片分布式表的不足:Distributed表在寫入時會在本地節點生成臨時資料,會産生寫放大,是以會對CPU及記憶體造成一些額外消耗,也會增加merge負擔。

使用說明

一:ZooKeeper離線

不管是ClickHouse的副本還是分片都是通過ZooKeeper來進行協調的,如果ZooKeeper節點異常了,則會出現:

Code: 242. DB::Exception: Received from xxxx DB::Exception: Table is in readonly mode (zookeeper path: /xxx/xxx/xxx).       

表不能進行修改,能查詢,變成了隻讀表。是以ZooKeeper線上也必須要開啟叢集模式。

二:副本離線

①:如果一個分片的節點出現異常當機,如果有副本節點,則資料寫入分布式表會直接寫入到其副本節點,不影響資料庫的可用性。當節點起來之後該節點會自動同步該資料。

②:如果一個分片的節點出現異常當機,包括副本節點,則資料讀取和寫入分布式表隻會對未異常的節點進行正常操作,異常節點報錯。如果異常節點起來之後,會把報錯期間的資料重新寫入,保證資料的最終寫入成功。

擴容&縮容

一:縮容

1:副本縮容

副本節點作為一個獨立的節點,直接下線縮容即可。

2:分片縮容

按照上面的部署,把2分片縮容到1分片。具體的操作如下:

①:準備資料

-- 首先看看分片2上有多少個分區:
> select table,active,partition_id,name,database from system.parts where table='repl_abc';
确認有2個分區:202105、202106

-- 檢視分片1的本地表資料,保證縮容之後,資料寫入的準确性
> select count(*) from repl_abc;
結果是17

-- 檢視分片1的分布式表資料
>select count(*) from repl_abc_all;
結果是40      

即需要把分片2的資料(23條)寫入到分片1,再下線分片2。

②:導出資料,在分片1上執行:FETCH PARTITION

-- 備份分片2上的分區202105
clickhouse-client --port=9000 --password=123456 -m  --query  "ALTER TABLE testdb.repl_abc FETCH PARTITION 202105 FROM '/clickhouse/tables/2/repl_abc'"

-- 備份分片2上的分區202106
clickhouse-client --port=9000 --password=123456 -m  --query  "ALTER TABLE testdb.repl_abc FETCH PARTITION 202106 FROM '/clickhouse/tables/2/repl_abc'"      

上面的導出檔案會儲存在data目錄下的detached目錄中,其中的FROM是zk_path的位址:即shard分區為2,表為repl_abc的資料。

③:導入資料,在分片1上執行:

-- 導入分區202105到表中
clickhouse-client --port=9000 --password=123456 -m  --query  "ALTER TABLE testdb.repl_abc ATTACH PARTITION 202105"

-- 導入分區202106到表中
clickhouse-client --port=9000 --password=123456 -m  --query  "ALTER TABLE testdb.repl_abc ATTACH PARTITION 202106"      

④:驗證資料

-- 查詢分片1的本地表
select count(*) from repl_abc;
結果為40,說明資料遷移成功      

⑤:删除源表&修改配置

驗證沒問題之後,删除分片2上的本地表,然後修改metrika.xml檔案,删除分片2的資訊,縮容配置持久化。最後通過下面SQL查詢叢集資訊:

select * from system.clusters;      

二:擴容

1. 副本擴容

 副本擴容比較簡單,隻需要在新執行個體上建立表,修改表結構中的{replica_name},{zk_path}不需要改變。資料會自動通過ZooKeeper來協調擷取主資訊,從主上下載下傳資料到本地。

2. 分片擴容

增加分片節點,叢集的分片數将增加,這樣會讓新的分片表和老的分片表的分片數量不一緻。因為擴容需要用到新分片,是以需要先新增一個叢集,讓擴容的分片表可以使用到新的分片節點,然後将舊叢集中的資料遷移至新叢集,最後删除舊叢集的資料與叢集配置資訊。具體的分片配置可以看上面的部署。大緻的操作步驟:

-- 1. 修改配置,在原有的配置上,新增叢集。
2. 讓配置生效,重新開機所有節點。
3. 在新叢集中新增分片表。
4. 使用分布式表遷移資料:
-- INSERT INTO TABLE db.tb_all SELECT * FROM db_1.tb_1_all
5. 遷移完成之後,删除舊的分片表。      

三:配置模闆 

1)執行個體:

192.168.11.1 192.168.11.2

2)配置:

config.xml 配置檔案不需要修改,新增2個參數即可(參數優化暫時不考慮):

<listen_host>0.0.0.0</listen_host>
...
...
    <include_from>/etc/clickhouse-server/config.d/metrika.xml</include_from>
    <zookeeper incl="zookeeper-server" optional="false" />
    <macros incl="macros" optional="true" />
    <remote_servers incl="ck_remote_servers" />      

因為include了metrika.xml檔案,是以隻需要修改該檔案即可。該檔案包含了:ZooKeeper位址、叢集資訊、以及macros變量。

1分片1副本:類似一個主從

  • 主節點配置:
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    <?xml version="1.0"?>
    <yandex>
     
        <zookeeper-server>
            <node index="1">
                <host>192.168.11.1</host>
                <port>2181</port>
            </node>
        </zookeeper-server>
     
        <macros>
            <replica>A</replica>
            <shard>1</shard>
        </macros>
     
        <ck_remote_servers>
            <cluster_1shard_1repl>
                <shard>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.1</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                    <replica>
                        <host>192.168.11.2</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
            </cluster_1shard_1repl>
        </ck_remote_servers>
     
     
    </yandex>      
  • 副本點配置:
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    <?xml version="1.0"?>
    <yandex>
     
        <zookeeper-server>
            <node index="1">
                <host>192.168.11.1</host>
                <port>2181</port>
            </node>
        </zookeeper-server>
     
        <macros>
            <replica>B</replica>
            <shard>1</shard>
        </macros>
     
        <ck_remote_servers>
            <cluster_1shard_1repl>
                <shard>
                    <internal_replication>true</internal_replication>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.1</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                    <replica>
                        <host>192.168.11.2</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
            </cluster_1shard_1repl>
        </ck_remote_servers>
     
    </yandex>      

2分片0副本

  • 分片1配置:
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    <?xml version="1.0"?>
    <yandex>
     
        <zookeeper-server>
            <node index="1">
                <host>192.168.11.1</host>
                <port>2181</port>
            </node>
        </zookeeper-server>
     
        <macros>
            <replica>A</replica>
            <shard>1</shard>
        </macros>
     
        <ck_remote_servers>
            <cluster_2shard_0repl>
                <shard>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.1</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
                <shard>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.2</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
            </cluster_2shard_0repl>
        </ck_remote_servers>
     
    </yandex>      
  • ClickHouse學習系列之四【副本&amp;分片部署說明】
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    <?xml version="1.0"?>
    <yandex>
     
        <zookeeper-server>
            <node index="1">
                <host>192.168.11.1</host>
                <port>2181</port>
            </node>
        </zookeeper-server>
     
        <macros>
            <replica>B</replica>
            <shard>2</shard>
        </macros>
     
        <ck_remote_servers>
            <cluster_2shard_0repl>
                <shard>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.1</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
                <shard>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.2</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
            </cluster_2shard_0repl>
        </ck_remote_servers>
     
    </yandex>      

2分片1副本

  • ClickHouse學習系列之四【副本&amp;分片部署說明】
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    <?xml version="1.0"?>
    <yandex>
     
        <zookeeper-server>
            <node index="1">
                <host>192.168.11.1</host>
                <port>2181</port>
            </node>
        </zookeeper-server>
     
        <macros>
            <replica>A</replica>
            <shard>1</shard>
        </macros>
     
        <ck_remote_servers>
            <cluster_2shard_1repl>
                <shard>
                    <internal_replication>true</internal_replication>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.1</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                    <replica>
                        <host>192.168.11.1</host>
                        <port>9010</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
                <shard>
                    <internal_replication>true</internal_replication>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.2</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                    <replica>
                        <host>192.168.11.2</host>
                        <port>9010</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
            </cluster_2shard_1repl>
        </ck_remote_servers>
     
    </yandex>      
  • 分片1副本配置:
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    <?xml version="1.0"?>
    <yandex>
     
        <zookeeper-server>
            <node index="1">
                <host>192.168.11.1</host>
                <port>2181</port>
            </node>
        </zookeeper-server>
     
        <macros>
            <replica>AA</replica>
            <shard>1</shard>
        </macros>
     
        <ck_remote_servers>
            <cluster_2shard_1repl>
                <shard>
                    <internal_replication>true</internal_replication>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.1</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                    <replica>
                        <host>192.168.11.1</host>
                        <port>9010</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
                <shard>
                    <internal_replication>true</internal_replication>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.2</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                    <replica>
                        <host>192.168.11.2</host>
                        <port>9010</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
            </cluster_2shard_1repl>
        </ck_remote_servers>
     
    </yandex>      
  • 分片2配置:
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    <?xml version="1.0"?>
    <yandex>
     
        <zookeeper-server>
            <node index="1">
                <host>192.168.11.1</host>
                <port>2181</port>
            </node>
        </zookeeper-server>
     
        <macros>
            <replica>B</replica>
            <shard>2</shard>
        </macros>
     
        <ck_remote_servers>
            <cluster_2shard_1repl>
                <shard>
                    <internal_replication>true</internal_replication>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.1</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                    <replica>
                        <host>192.168.11.1</host>
                        <port>9010</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
                <shard>
                    <internal_replication>true</internal_replication>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.2</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                    <replica>
                        <host>192.168.11.2</host>
                        <port>9010</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
            </cluster_2shard_1repl>
        </ck_remote_servers>
     
    </yandex>      
  • 分片2副本配置:
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    ClickHouse學習系列之四【副本&amp;分片部署說明】
    <?xml version="1.0"?>
    <yandex>
     
        <zookeeper-server>
            <node index="1">
                <host>192.168.11.1</host>
                <port>2181</port>
            </node>
        </zookeeper-server>
     
        <macros>
            <replica>BB</replica>
            <shard>2</shard>
        </macros>
     
        <ck_remote_servers>
            <cluster_2shard_1repl>
                <shard>
                    <internal_replication>true</internal_replication>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.1</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                    <replica>
                        <host>192.168.11.1</host>
                        <port>9010</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
                <shard>
                    <internal_replication>true</internal_replication>
                    <weight>1</weight>
                    <replica>
                        <host>192.168.11.2</host>
                        <port>9000</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                    <replica>
                        <host>192.168.11.2</host>
                        <port>9010</port>
                        <user>default</user>
                        <password>123456</password>
                    </replica>
                </shard>
            </cluster_2shard_1repl>
        </ck_remote_servers>
     
    </yandex>      

以上的配置差別:macros 參數定義

總結

ClickHouse是一個快速開源的列式資料庫管理系統,在其高性能的前提下,通過副本表和分片來進一步保障了資料庫的可用性和可靠性,關于更多的該方面的說明,後續會進一步說明和更新。

~~~~~~~~~~~~~~~

萬物之中,希望至美

~~~~~~~~~~~~~~~

繼續閱讀