ClickHouse实战-ClickHouse集群构建

注意:我们需要在每台节点中部署ClickHouse部署方式详见ClickHouse安装部署

我们一般配置ClickHouse集群只需要修改Zookeeper存储和节点添加即可。

节点信息


主机 IP
ck1 10.10.0.1
ck2 10.10.0.2
ck3 10.10.0.3

配置Zookeeper


/etc/clickhouse-server/config.xml文件中添加以下配置信息

<zookeeper>
    <node index="1">
        <host>zk1</host>
        <port>2181</port>
    </node>
    <node index="2">
        <host>zk2</host>
        <port>2181</port>
    </node>
    <node index="3">
        <host>zk3</host>
        <port>2181</port>
    </node>
</zookeeper>

改配置文件主要配置连接Zookeeper的信息,每个节点的index不可重复

注意要添加到yandex节点中

配置ClickHouse集群


配置ClickHouse集群节点需要配置remote_servers节点

/etc/clickhouse-server/config.xml文件中添加以下配置信息

<remote_servers>
    <ck_cluster>
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>ck1</host>
                <port>9000</port>
            </replica>
            <replica>
                <host>ck2</host>
                <port>9000</port>
            </replica>
        </shard>
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>ck2</host>
                <port>9000</port>
            </replica>
            <replica>
                <host>ck3</host>
                <port>9000</port>
            </replica>
        </shard>
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>ck3</host>
                <port>9000</port>
            </replica>
            <replica>
                <host>ck1</host>
                <port>9000</port>
            </replica>
        </shard>
    </ck_cluster>
</remote_servers>
  • ck_cluster 集群标识,可以自行规定,在创建分布式表(引擎为Distributed)时需要用到。
  • weight 每个分片的写入权重值,数据写入时会有较大概率落到weight值较大的分片,这里全部设为1。
  • internal_replication 是否启用内部复制,即写入数据时只写入到一个副本,其他副本的同步工作靠复制表和ZooKeeper异步进行。

我们在shard分片中设置的是循环分片这样保证我们复制的节点某一个宕机后可以正常运行将配置分发到所有部署ClickHouse的节点中进行服务重启

此时我们的ClickHouse集群已经搭建完成!

我们还可以使用以下方式构建CK集群(也可以实现多集群构建)

修改集群配置


  • 修改vim /etc/clickhouse-server/config.xml配置文件
<zookeeper>
    <node index="1">
        <host>zk1</host>
        <port>2181</port>
    </node>
    <node index="2">
        <host>zk2</host>
        <port>2181</port>
    </node>
    <node index="3">
        <host>zk3</host>
        <port>2181</port>
    </node>
</zookeeper>

<remote_servers>
    <ck_cluster>
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>ck1</host>
                <port>9000</port>
            </replica>
            <replica>
                <host>ck2</host>
                <port>9000</port>
            </replica>
        </shard>
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>ck2</host>
                <port>9000</port>
            </replica>
            <replica>
                <host>ck3</host>
                <port>9000</port>
            </replica>
        </shard>
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>ck3</host>
                <port>9000</port>
            </replica>
            <replica>
                <host>ck1</host>
                <port>9000</port>
            </replica>
        </shard>
    </ck_cluster>
</remote_servers>

删除以上两个配置信息,新增以下内容

<remote_servers incl="clickhouse_remote_servers" />
<zookeeper incl="zookeeper-servers" optional="true" />

<include_from>/etc/clickhouse-server/ck-cluster.xml</include_from>
  • remote_servers 标志我们在外部文件中引用的集群配置节点是clickhouse_remote_servers
  • zookeeper 标志我们在外部文件引用的ZK集群配置节点是zookeeper-servers
  • include_from 标志我们引用的外部配置文件,指定文件的绝对路径

新建集群配置文件


刚刚我们指定了/etc/clickhouse-server/ck-cluster.xml配置文件,此时我们需要去对该文件做配置

  • 创建新的配置文件
touch /etc/clickhouse-server/ck-cluster.xml

在新的配置文件中增加zookeeperclickhouse集群信息

<yandex>
    <zookeeper-servers>
        <node index="1">
            <host>zk1</host>
            <port>2181</port>
        </node>
        <node index="2">
            <host>zk2</host>
            <port>2181</port>
        </node>
        <node index="3">
            <host>zk3</host>
            <port>2181</port>
        </node>
    </zookeeper-servers>
    
    <clickhouse_remote_servers>
        <ck_cluster>
            <shard_1>
                <weight>1</weight>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>ck1</host>
                    <port>9000</port>
                </replica>
                <replica>
                    <host>ck2</host>
                    <port>9000</port>
                </replica>
            </shard_1>
            <shard_2>
                <weight>1</weight>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>ck2</host>
                    <port>9000</port>
                </replica>
                <replica>
                    <host>ck3</host>
                    <port>9000</port>
                </replica>
            </shard_2>
            <shard_3>
                <weight>1</weight>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>ck3</host>
                    <port>9000</port>
                </replica>
                <replica>
                    <host>ck1</host>
                    <port>9000</port>
                </replica>
            </shard_3>
        </ck_cluster>
    </clickhouse_remote_servers> 

    <networks>
        <ip>::/0</ip>
    </networks>
</yandex>

此处需要注意的是在yandex中配置的相关节点一定到和config.xml中的

                                      <zookeeper incl="zookeeper-servers" optional="true" />这两个属性的incl一致,否则会扫描不到集群标志

修改配置后我们不需要重启服务,ClickHouse会自动加载修改后的配置信息
我们配置多个集群使用多个配置文件即可,多个replica节点配置多个也是可以的

上一篇:SQL Server 2008新特性——策略管理


下一篇:Cocoa 新的依赖管理工具:Carthage