Docker快速搭建Clickhouse集羣(3分片3副本)

背景

前文簡單介紹了下Clickhouse的安裝和客戶端使用,在實際生產環境中,Clickhouse經常是以集羣模式部署的,因爲不少系統不知足sse4.2指令,這裏使用docker來搭建一個Clickhouse的集羣。html

1. 環境說明

1.1 機器列表

機器名node

IP算法

配置sql

操做系統docker

部署的服務數據庫

備註centos

server01服務器

192.168.21.21運維

8c8gssh

centos7.3

clickhouserver(cs01-01)和

clickhouserver(cs01-02)

   clickhouse01-01: 實例1, 端口: tcp 9000, http 8123, 同步端口9009, 類型: 分片1, 副本1

     clickhouse01-02: 實例2, 端口: tcp 9001, http 8124, 同步端口9010, 類型: 分片2, 副本2 (clickhouse2的副本)

server02

192.168.21.69

8c8g

centos7.3

clickhouserver(cs02-01)和

clickhouserver(cs02-02)

 clickhouse02-01: 實例1, 端口: tcp 9000, http 8123, 同步端口9009, 類型: 分片2, 副本1

     clickhouse02-02: 實例2, 端口: tcp 9001, http 8124, 同步端口9010, 類型: 分片3, 副本2 (clickhouse3的副本)

server03

192.168.21.6

8c8g

centos7.3

clickhouserver(cs03-01)和

clickhouserver(cs03-02)

clickhouse03-01: 實例1, 端口: tcp 9000, http 8123, 同步端口9009, 類型: 分片3, 副本1

     clickhouse03-02: 實例2, 端口: tcp 9001, http 8124, 同步端口9010, 類型: 分片1, 副本2 (clickhouse1的副本)

1.2 機器初始化

1.2.1 配置host

執行:vi /etc/hosts, 加入下面三行:

192.168.21.21 server01

192.168.21.69 server02

192.168.21.6 server03

1.2.2 安裝docker

每臺機器上均安裝相同版本的docker

參照:docker環境搭建

1.2.3 安裝Zookeeper

參照zookper集羣搭建(3節點)安裝

1.3 目錄初始化

1.3.1 建立對應本地路徑

分別在三臺服務器,建立數據存儲目錄: 

mkdir /data/clickhouse

1.3.2 獲取clickhouse-server的配置

1)在server01服務器作以下操做

先按照官方教程的docker命令啓動Clickhouse-Server

docker run -d --name clickhouse-server --ulimit nofile=262144:262144 --volume=/data/clickhouse/:/var/lib/clickhouse yandex/clickhouse-server

2)啓動完成後,複製容器內的配置文件到本機目錄下

#拷貝容器內容的配置到/etc目錄下
docker cp clickhouse-server:/etc/clickhouse-server/ /etc/

#將server01上的目錄重命名
cp -rf /etc/clickhouse-server/ /etc/clickhouse-server01/
cp -rf /etc/clickhouse-server/ /etc/clickhouse-server02/

3)而後將/etc/clickhouse-server/ 分別拷貝到每一個機器上

#拷貝配置到server02上
scp /etc/clickhouse-server/ server02:/etc/clickhouse-server01/
scp /etc/clickhouse-server/ server02:/etc/clickhouse-server02/
#拷貝配置到server03上
scp /etc/clickhouse-server/ server03:/etc/clickhouse-server01/
scp /etc/clickhouse-server/ server03:/etc/clickhouse-server02/

2. 集羣環境搭建

2.1 集羣環境拓撲圖

集羣環境說明:

clickhouse01-01: 實例1, 端口: tcp 9000, http 8123, 同步端口9009, 類型: 分片1, 副本1

clickhouse01-02: 實例2, 端口: tcp 9001, http 8124, 同步端口9010, 類型: 分片2, 副本2 (clickhouse2的副本)

clickhouse02-01: 實例1, 端口: tcp 9000, http 8123, 同步端口9009, 類型: 分片2, 副本1

clickhouse02-02: 實例2, 端口: tcp 9001, http 8124, 同步端口9010, 類型: 分片3, 副本2 (clickhouse3的副本)

clickhouse03-01: 實例1, 端口: tcp 9000, http 8123, 同步端口9009, 類型: 分片3, 副本1

clickhouse03-02: 實例2, 端口: tcp 9001, http 8124, 同步端口9010, 類型: 分片1, 副本2 (clickhouse1的副本)

2.2 配置集羣

2.2.1 待修改的配置文件

須要修改的配置有兩個(若是有須要也能夠配置user.xml):

  • /etc/clickhouse-server/config.xml
  • /etc/clickhouse-server/metrika.xml(新增文件)

2.2.2 server1上配置clickhouse-server的實例

2.2.2.1 clickhouse-01-01的配置:

1)/etc/clickhouse-server01/config.xml(其餘實例此配置內容和這個同樣就行)

修改include from節點爲實際的引用到的文件

 <!-- 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.
      -->
    <include_from>/etc/clickhouse-server/metrika.xml</include_from>
    
    <listen_host>0.0.0.0</listen_host>
    <listen_host>127.0.0.1</listen_host>

2)/etc/clickhouse-server01/metrika.xml(全部實例的配置內容都和這個同樣就行)

<!--全部實例均使用這個集羣配置,不用個性化 -->
<yandex>

    <!-- 集羣配置 -->
    <!-- clickhouse_remote_servers全部實例配置都同樣 -->
    <!-- 集羣配置 -->
      <clickhouse_remote_servers>
        <cluster_3s_1r>

            <!-- 數據分片1  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server01</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server03</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 數據分片2  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server02</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server01</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 數據分片3  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server03</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server02</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>
        </cluster_3s_1r>
    </clickhouse_remote_servers>

    <!-- ZK  -->
    <!-- zookeeper_servers全部實例配置都同樣 -->
    <zookeeper-servers>
        <node index="1">
            <host>192.168.21.66</host>
            <port>2181</port>
        </node>
        <node index="2">
            <host>192.168.21.57</host>
            <port>2181</port>
        </node>
        <node index="3">
            <host>192.168.21.17</host>
            <port>2181</port>
        </node>
    </zookeeper-servers>
    
    <!-- marcos每一個實例配置不同 分片1, 副本1 -->   
    <macros>
        <layer>01</layer>
        <shard>01</shard>
        <replica>cluster01-01-1</replica>
    </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>
        </case>
    </clickhouse_compression>

</yandex>

2.2.2.2 clickhouse-01-02的配置:

1)/etc/clickhouse-server02/metrika.xml(全部實例的配置內容都和這個同樣就行)

<!--全部實例均使用這個集羣配置,不用個性化 -->
<yandex>

    <!-- 集羣配置 -->
    <!-- clickhouse_remote_servers全部實例配置都同樣 -->
    <clickhouse_remote_servers>
        <cluster_3s_1r>

            <!-- 數據分片1  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server01</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server03</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 數據分片2  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server02</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server01</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 數據分片3  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server03</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server02</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>
        </cluster_3s_1r>
    </clickhouse_remote_servers>

    <!-- ZK  -->
    <!-- zookeeper_servers全部實例配置都同樣 -->
    <zookeeper-servers>
        <node index="1">
            <host>192.168.21.66</host>
            <port>2181</port>
        </node>
        <node index="2">
            <host>192.168.21.57</host>
            <port>2181</port>
        </node>
        <node index="3">
            <host>192.168.21.17</host>
            <port>2181</port>
        </node>
    </zookeeper-servers>
    
    <!-- marcos每一個實例配置不同 分片2, 副本2-->   
    <macros>
        <layer>01</layer>
        <shard>02</shard>
        <replica>cluster01-02-2</replica>
    </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>
        </case>
    </clickhouse_compression>

</yandex>

2.2.3 server2上配置clickhouse-server的實例

2.2.3.1 clickhouse-02-01的配置:

/etc/clickhouse-server01/metrika.xml

<!--全部實例均使用這個集羣配置,不用個性化 -->
<yandex>

    <!-- 集羣配置 -->
    <!-- clickhouse_remote_servers全部實例配置都同樣 -->
       <clickhouse_remote_servers>
        <cluster_3s_1r>

            <!-- 數據分片1  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server01</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server03</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 數據分片2  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server02</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server01</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 數據分片3  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server03</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server02</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>
        </cluster_3s_1r>
    </clickhouse_remote_servers>


    <!-- ZK  -->
    <!-- zookeeper_servers全部實例配置都同樣 -->
    <zookeeper-servers>
        <node index="1">
            <host>192.168.21.66</host>
            <port>2181</port>
        </node>
        <node index="2">
            <host>192.168.21.57</host>
            <port>2181</port>
        </node>
        <node index="3">
            <host>192.168.21.17</host>
            <port>2181</port>
        </node>
    </zookeeper-servers>
    
    <!-- marcos每一個實例配置不同 分片2, 副本1-->   
    <macros>
        <layer>01</layer>
        <shard>02</shard>
        <replica>cluster01-02-1</replica>
    </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>
        </case>
    </clickhouse_compression>

</yandex>

2.2.3.2 clickhouse-02-02的配置:

/etc/clickhouse-server02/metrika.xml

<!--全部實例均使用這個集羣配置,不用個性化 -->
<yandex>

    <!-- 集羣配置 -->
    <!-- clickhouse_remote_servers全部實例配置都同樣 -->
     <clickhouse_remote_servers>
        <cluster_3s_1r>

            <!-- 數據分片1  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server01</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server03</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 數據分片2  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server02</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server01</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 數據分片3  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server03</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server02</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>
        </cluster_3s_1r>
    </clickhouse_remote_servers>


    <!-- ZK  -->
    <!-- zookeeper_servers全部實例配置都同樣 -->
    <zookeeper-servers>
        <node index="1">
            <host>192.168.21.66</host>
            <port>2181</port>
        </node>
        <node index="2">
            <host>192.168.21.57</host>
            <port>2181</port>
        </node>
        <node index="3">
            <host>192.168.21.17</host>
            <port>2181</port>
        </node>
    </zookeeper-servers>
    
    <!-- marcos每一個實例配置不同 分片3, 副本2-->   
    <macros>
        <layer>01</layer>
        <shard>03</shard>
        <replica>cluster01-03-2</replica>
    </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>
        </case>
    </clickhouse_compression>

</yandex>

2.2.4 server3上配置clickhouse-server的實例

2.2.4.1 clickhouse-03-01的配置:

1)/etc/clickhouse-server01/metrika.xml

<!--全部實例均使用這個集羣配置,不用個性化 -->
<yandex>

    <!-- 集羣配置 -->
    <!-- clickhouse_remote_servers全部實例配置都同樣 -->
       <clickhouse_remote_servers>
        <cluster_3s_1r>

            <!-- 數據分片1  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server01</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server03</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 數據分片2  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server02</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server01</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 數據分片3  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server03</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server02</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>
        </cluster_3s_1r>
    </clickhouse_remote_servers>

    <!-- ZK  -->
    <!-- zookeeper_servers全部實例配置都同樣 -->
    <zookeeper-servers>
        <node index="1">
            <host>192.168.21.66</host>
            <port>2181</port>
        </node>
        <node index="2">
            <host>192.168.21.57</host>
            <port>2181</port>
        </node>
        <node index="3">
            <host>192.168.21.17</host>
            <port>2181</port>
        </node>
    </zookeeper-servers>
    
    <!-- marcos每一個實例配置不同 分片3, 副本1-->   
    <macros>
        <layer>01</layer>
        <shard>03</shard>
        <replica>cluster01-03-1</replica>
    </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>
        </case>
    </clickhouse_compression>

</yandex>

2.2.4.2 clickhouse-03-02的配置:

1. /etc/clickhouse-server02/metrika.xml

<!--全部實例均使用這個集羣配置,不用個性化 -->
<yandex>

    <!-- 集羣配置 -->
    <!-- clickhouse_remote_servers全部實例配置都同樣 -->
     <clickhouse_remote_servers>
        <cluster_3s_1r>

            <!-- 數據分片1  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server01</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server03</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 數據分片2  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server02</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server01</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 數據分片3  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server03</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server02</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>
        </cluster_3s_1r>
    </clickhouse_remote_servers>


    <!-- ZK  -->
    <!-- zookeeper_servers全部實例配置都同樣 -->
    <zookeeper-servers>
        <node index="1">
            <host>192.168.21.66</host>
            <port>2181</port>
        </node>
        <node index="2">
            <host>192.168.21.57</host>
            <port>2181</port>
        </node>
        <node index="3">
            <host>192.168.21.17</host>
            <port>2181</port>
        </node>
    </zookeeper-servers>
    
    <!-- marcos每一個實例配置不同 分片1, 副本2-->   
    <macros>
        <layer>01</layer>
        <shard>01</shard>
        <replica>cluster01-01-2</replica>
    </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>
        </case>
    </clickhouse_compression>

</yandex>

2.3 運行clickhouse集羣

2.3.1 運行clickhouse01-01實例

登錄到server01:ssh server01

#先刪除cs01-01容器
docker rm -f cs01-01

#運行Clickhouse-server
docker run -d \
--name cs01-01 \
--ulimit nofile=262144:262144 \
--volume=/data/clickhouse01/:/var/lib/clickhouse \
--volume=/etc/clickhouse-server01/:/etc/clickhouse-server/ \
--add-host server01:192.168.21.21 \
--add-host server02:192.168.21.69 \
--add-host server03:192.168.21.6 \
--add-host i-r9es2e0q:192.168.21.21 \
--add-host i-o91d619w:192.168.21.69 \
--add-host i-ldipmbwa:192.168.21.6 \
--hostname $(hostname) \
-p 9000:9000 \
-p 8123:8123 \
-p 9009:9009 \
yandex/clickhouse-server

說明: 

--add-host參數:由於咱們在配置文件中使用了hostname來指代咱們的服務器,爲了讓容器可以識別,因此須要加此參數,對應的host配置會自動被添加到容器主機的/etc/hosts裏面

--hostname參數:clickhouse中的system.clusters表會顯示集羣信息,其中is_local的屬性若是不配置hostname的話clickhouse沒法識別是不是當前本機。is_local都爲0的話會影響集羣操做,is_local經過clickhouse-client登陸到任一clickhouse-server上查看:SELECT * FROM system.clusters;

--p參數:暴露容器中的端口到本機端口中。

2.3.2 運行clickhouse01-02實例

登錄到server01:ssh server01;

docker run -d \
--name cs01-02 \
--ulimit nofile=262144:262144 \
--volume=/data/clickhouse02/:/var/lib/clickhouse \
--volume=/etc/clickhouse-server02/:/etc/clickhouse-server/ \
--add-host server01:192.168.21.21 \
--add-host server02:192.168.21.69 \
--add-host server03:192.168.21.6 \
--add-host i-r9es2e0q:192.168.21.21 \
--add-host i-o91d619w:192.168.21.69 \
--add-host i-ldipmbwa:192.168.21.6 \
--hostname $(hostname) \
-p 9001:9000 \
-p 8124:8123 \
-p 9010:9009 \
yandex/clickhouse-server

2.3.3 運行clickhouse02-01實例

登錄到server02ssh server02

docker run -d \
--name cs02-01 \
--ulimit nofile=262144:262144 \
--volume=/data/clickhouse01/:/var/lib/clickhouse \
--volume=/etc/clickhouse-server01/:/etc/clickhouse-server/ \
--add-host server01:192.168.21.21 \
--add-host server02:192.168.21.69 \
--add-host server03:192.168.21.6 \
--add-host i-r9es2e0q:192.168.21.21 \
--add-host i-o91d619w:192.168.21.69 \
--add-host i-ldipmbwa:192.168.21.6 \
--hostname $(hostname) \
-p 9000:9000 \
-p 8123:8123 \
-p 9009:9009 \
yandex/clickhouse-server

2.3.4 運行clickhouse02-02實例

登錄到server02:ssh server02

docker run -d \
--name cs02-02 \
--ulimit nofile=262144:262144 \
--volume=/data/clickhouse02/:/var/lib/clickhouse \
--volume=/etc/clickhouse-server02/:/etc/clickhouse-server/ \
--add-host server01:192.168.21.21 \
--add-host server02:192.168.21.69 \
--add-host server03:192.168.21.6 \
--add-host i-r9es2e0q:192.168.21.21 \
--add-host i-o91d619w:192.168.21.69 \
--add-host i-ldipmbwa:192.168.21.6 \
--hostname $(hostname) \
-p 9001:9000 \
-p 8124:8123 \
-p 9010:9009 \
yandex/clickhouse-server

2.3.5 運行clickhouse03-01實例

登錄到server03:ssh server03

docker run -d \
--name cs03-01 \
--ulimit nofile=262144:262144 \
--volume=/data/clickhouse01/:/var/lib/clickhouse \
--volume=/etc/clickhouse-server01/:/etc/clickhouse-server/ \
--add-host server01:192.168.21.21 \
--add-host server02:192.168.21.69 \
--add-host server03:192.168.21.6 \
--add-host i-r9es2e0q:192.168.21.21 \
--add-host i-o91d619w:192.168.21.69 \
--add-host i-ldipmbwa:192.168.21.6 \
--hostname $(hostname) \
-p 9000:9000 \
-p 8123:8123 \
-p 9009:9009 \
yandex/clickhouse-server

2.3.6 運行clickhouse03-02實例

登錄到server03:ssh server03

docker run -d \
--name cs03-02 \
--ulimit nofile=262144:262144 \
--volume=/data/clickhouse02/:/var/lib/clickhouse \
--volume=/etc/clickhouse-server02/:/etc/clickhouse-server/ \
--add-host server01:192.168.21.21 \
--add-host server02:192.168.21.69 \
--add-host server03:192.168.21.6 \
--add-host i-r9es2e0q:192.168.21.21 \
--add-host i-o91d619w:192.168.21.69 \
--add-host i-ldipmbwa:192.168.21.6 \
--hostname $(hostname) \
-p 9001:9000 \
-p 8124:8123 \
-p 9010:9009 \
yandex/clickhouse-server

2.4 clickhouse集羣的數據操做

2.4.1 運行客戶端鏈接clickhouse server

1)隨便在哪一臺實例的機器上執行以下(連不一樣的clickhouse實例只須要改下host和port的值便可)

docker run -it \ --rm \ --add-host server01:192.168.21.21 \ --add-host server02:192.168.21.69 \ --add-host server03:192.168.21.6 \ yandex/clickhouse-client \ --host server01 \ --port 9000

2)執行下以下命令查看下配置信息:

#須要看下,是否和metrika.xml配置的分片和副本信息一致,若是不一致,須要check下每一個clickhouse-server實例的配置

#應該is_local顯示爲0,且分片和副本信息都正確

SELECT * FROM system.clusters;

2.4.2 建立本地複製表和分佈式表

全部實例配置完上面這些以後,分別執行啓動命令啓動,而後全部實例都執行下面語句建立數據庫:

例如在實例01-02上執行:

#執行Clickhouse client進入Clickhouse
docker run -it --rm --add-host server01:192.168.21.21 --add-host server02:192.168.21.69 --add-host server03:192.168.21.6 \  
yandex/clickhouse-client --host server01 --port 9001
ClickHouse client version 19.17.5.18 (official build).
Connecting to server01:9001 as user default.
Connected to ClickHouse server version 19.17.5 revision 54428.

i-r9es2e0q :) CREATE DATABASE test;

CREATE DATABASE test

Ok.

2.4.3 建立複製表

而後對於全部實例分別建立對應的複製表,這裏測試建立一個簡單的表:

#在clickhouse01-01 9000上執行:
docker run -it --rm --add-host server01:192.168.21.21 --add-host server02:192.168.21.69 --add-host server03:192.168.21.6 \
yandex/clickhouse-client --host server01 --port 9000
#而後執行
CREATE TABLE test.device_thing_data (
                time                     UInt64,
                user_id                 String,
                device_id                 String,
                source_id                 String,
                thing_id                   String,
                identifier                String,
                value_int32                Int32,
                value_float                Float32,
                value_double            Float64,
                value_string            String,
                value_enum              Enum8('0'=0,'1'=1,'2'=2,'3'=3,'4'=4,'5'=5,'6'=6,'7'=7,'8'=8),
                value_string_ex         String,
                value_array_string         Array(String),
                value_array_int32         Array(Int32),
                value_array_float         Array(Float32),
                value_array_double         Array(Float64),
                action_date                Date,
                action_time             DateTime
            ) Engine= ReplicatedMergeTree('/clickhouse/tables/01-01/device_thing_data','cluster01-01-1') PARTITION BY toYYYYMM(action_date) ORDER BY (user_id,device_id,thing_id,identifier,time,intHash64(time)) SAMPLE BY intHash64(time) SETTINGS index_granularity=8192
   
#在clickhouse01-02 9001上執行:
docker run -it --rm --add-host server01:192.168.21.21 --add-host server02:192.168.21.69 --add-host server03:192.168.21.6 \
yandex/clickhouse-client --host server01 --port 9001
而後執行
CREATE TABLE test.device_thing_data (
                time                     UInt64,
                user_id                 String,
                device_id                 String,
                source_id                 String,
                thing_id                   String,
                identifier                String,
                value_int32                Int32,
                value_float                Float32,
                value_double            Float64,
                value_string            String,
                value_enum              Enum8('0'=0,'1'=1,'2'=2,'3'=3,'4'=4,'5'=5,'6'=6,'7'=7,'8'=8),
                value_string_ex         String,
                value_array_string         Array(String),
                value_array_int32         Array(Int32),
                value_array_float         Array(Float32),
                value_array_double         Array(Float64),
                action_date                Date,
                action_time             DateTime
            ) Engine= ReplicatedMergeTree('/clickhouse/tables/01-02/device_thing_data','cluster01-02-2') PARTITION BY toYYYYMM(action_date) ORDER BY (user_id,device_id,thing_id,identifier,time,intHash64(time)) SAMPLE BY intHash64(time) SETTINGS index_granularity=8192
    
#在clickhouse02-01 9000上執行:
docker run -it --rm --add-host server01:192.168.21.21 --add-host server02:192.168.21.69 --add-host server03:192.168.21.6 \
yandex/clickhouse-client --host server02 --port 9000
#而後執行
CREATE TABLE test.device_thing_data (
                time                     UInt64,
                user_id                 String,
                device_id                 String,
                source_id                 String,
                thing_id                   String,
                identifier                String,
                value_int32                Int32,
                value_float                Float32,
                value_double            Float64,
                value_string            String,
                value_enum              Enum8('0'=0,'1'=1,'2'=2,'3'=3,'4'=4,'5'=5,'6'=6,'7'=7,'8'=8),
                value_string_ex         String,
                value_array_string         Array(String),
                value_array_int32         Array(Int32),
                value_array_float         Array(Float32),
                value_array_double         Array(Float64),
                action_date                Date,
                action_time             DateTime
            ) Engine= ReplicatedMergeTree('/clickhouse/tables/01-02/device_thing_data','cluster01-02-1') PARTITION BY toYYYYMM(action_date) ORDER BY (user_id,device_id,thing_id,identifier,time,intHash64(time)) SAMPLE BY intHash64(time) SETTINGS index_granularity=8192
  
#在clickhouse02-02 9001上執行:
docker run -it --rm --add-host server01:192.168.21.21 --add-host server02:192.168.21.69 --add-host server03:192.168.21.6 \
yandex/clickhouse-client --host server02 --port 9001   
#而後執行
CREATE TABLE test.device_thing_data (
                time                     UInt64,
                user_id                 String,
                device_id                 String,
                source_id                 String,
                thing_id                   String,
                identifier                String,
                value_int32                Int32,
                value_float                Float32,
                value_double            Float64,
                value_string            String,
                value_enum              Enum8('0'=0,'1'=1,'2'=2,'3'=3,'4'=4,'5'=5,'6'=6,'7'=7,'8'=8),
                value_string_ex         String,
                value_array_string         Array(String),
                value_array_int32         Array(Int32),
                value_array_float         Array(Float32),
                value_array_double         Array(Float64),
                action_date                Date,
                action_time             DateTime
            ) Engine= ReplicatedMergeTree('/clickhouse/tables/01-03/device_thing_data','cluster01-03-2') PARTITION BY toYYYYMM(action_date) ORDER BY (user_id,device_id,thing_id,identifier,time,intHash64(time)) SAMPLE BY intHash64(time) SETTINGS index_granularity=8192
     
#在clickhouse03-01 9000上執行:
docker run -it --rm --add-host server01:192.168.21.21 --add-host server02:192.168.21.69 --add-host server03:192.168.21.6 \
yandex/clickhouse-client --host server03 --port 9000
#而後執行
CREATE TABLE test.device_thing_data (
                time                     UInt64,
                user_id                 String,
                device_id                 String,
                source_id                 String,
                thing_id                   String,
                identifier                String,
                value_int32                Int32,
                value_float                Float32,
                value_double            Float64,
                value_string            String,
                value_enum              Enum8('0'=0,'1'=1,'2'=2,'3'=3,'4'=4,'5'=5,'6'=6,'7'=7,'8'=8),
                value_string_ex         String,
                value_array_string         Array(String),
                value_array_int32         Array(Int32),
                value_array_float         Array(Float32),
                value_array_double         Array(Float64),
                action_date                Date,
                action_time             DateTime
            ) Engine= ReplicatedMergeTree('/clickhouse/tables/01-03/device_thing_data','cluster01-03-1') PARTITION BY toYYYYMM(action_date) ORDER BY (user_id,device_id,thing_id,identifier,time,intHash64(time)) SAMPLE BY intHash64(time) SETTINGS index_granularity=8192
     
 
#在clickhouse03-02 9001上執行:
docker run -it --rm --add-host server01:192.168.21.21 --add-host server02:192.168.21.69 --add-host server03:192.168.21.6 \
yandex/clickhouse-client --host server03 --port 9001   
而後執行    
CREATE TABLE test.device_thing_data (
                time                     UInt64,
                user_id                 String,
                device_id                 String,
                source_id                 String,
                thing_id                   String,
                identifier                String,
                value_int32                Int32,
                value_float                Float32,
                value_double            Float64,
                value_string            String,
                value_enum              Enum8('0'=0,'1'=1,'2'=2,'3'=3,'4'=4,'5'=5,'6'=6,'7'=7,'8'=8),
                value_string_ex         String,
                value_array_string         Array(String),
                value_array_int32         Array(Int32),
                value_array_float         Array(Float32),
                value_array_double         Array(Float64),
                action_date                Date,
                action_time             DateTime
            ) Engine= ReplicatedMergeTree('/clickhouse/tables/01-01/device_thing_data','cluster01-01-2') PARTITION BY toYYYYMM(action_date) ORDER BY (user_id,device_id,thing_id,identifier,time,intHash64(time)) SAMPLE BY intHash64(time) SETTINGS index_granularity=8192

2.4.3 建立分佈式表(用於查詢)

而後建立完上面複製表以後,能夠建立分佈式表,分佈式表只是做爲一個查詢引擎,自己不存儲任何數據,查詢時將sql發送到全部集羣分片,而後進行進行處理和聚合後將結果返回給客戶端,所以clickhouse限制聚合結果大小不能大於分佈式表節點的內存,固然這個通常條件下都不會超過;分佈式表能夠全部實例都建立,也能夠只在一部分實例建立,這個和業務代碼中查詢的示例一致,建議設置多個,當某個節點掛掉時能夠查詢其餘節點上的表,分佈式表的建表語句以下:

#在clickhouse-server集羣上一次性建立全部的分佈式表,操做卡主了。緣由不明
CREATE TABLE device_thing_data_all ON CLUSTER cluster_3s_1r AS test.device_thing_data ENGINE = Distributed(cluster_3s_1r, default,  device_thing_data, rand())

#以下這個須要每一個機器上都操做一遍
CREATE TABLE device_thing_data_all  AS test.device_thing_data ENGINE = Distributed(cluster_3s_1r, test,  device_thing_data, rand())

2.4.4 測試可用性

#客戶端鏈接到某個clickhouse-server實例(例如cs01-01)
#查詢分佈式表,此時沒有查詢到數據
select * from  image_label_all;

#在cs01-01上執行以下查看是否有數據
#查詢本地複製表,此時沒有查詢到數據
select * from test.device_thing_data;


#往復製表中表裏插入一條數據
INSERT INTO test.device_thing_data;  (user_id) VALUES ('1')

#因爲剛纔在cs01-01上插入一條數據,因此應該有數據了
select * from test.device_thing_data;

#查詢分佈式表,也有數據了
select * from  image_label_all;

#在cs03-02上查詢複製表的數據(因爲cs03-02是cs01-01的副本,因此數據被自動同步過來了),因此應該有數據了
select * from test.device_thing_data;

#在cs03-01上插入一條數據,此時應該會把數據同步到cs02-02上
INSERT INTO test.device_thing_data  (user_id) VALUES ('2')

#再次查詢分佈式表,此時應該查到cs03-01和cs01-01上的兩條數據
select * from  image_label_all;

博主:測試生財(一個不爲996而996的測開碼農)

座右銘:專一測試開發與自動化運維,努力讀書思考寫做,爲內卷的人生奠基財務自由。

內容範疇:技術提高,職場雜談,事業發展,閱讀寫做,投資理財,健康人生。

csdn:https://blog.csdn.net/ccgshigao

博客園:https://www.cnblogs.com/qa-freeroad/

51cto:https://blog.51cto.com/14900374

在碼農的苦悶歲月裏,期待咱們一塊兒成長,歡迎關注,感謝拍磚!

相關文章
相關標籤/搜索