扩展
在本示例中,你将学习如何搭建一个简单且可扩展的 ClickHouse 集群。 集群中共配置了五台服务器,其中两台用于对数据进行分片。 另外三台服务器用于协调。
你将要搭建的集群架构如下所示:

Although it is possible to run ClickHouse Server and ClickHouse Keeper combined on the same server, we strongly recommend using dedicated hosts for ClickHouse keeper in production environments, which is the approach we will demonstrate in this example.
Keeper servers can be smaller, and 4GB RAM is generally enough for each Keeper server until your ClickHouse Servers grow large.
前置条件
- 你已经在本地部署过 ClickHouse 服务器
- 你熟悉 ClickHouse 的基础配置概念,例如 配置文件
- 你的机器上已安装 Docker
设置目录结构和测试环境
The following steps will walk you through setting up the cluster from scratch. If you prefer to skip these steps and jump straight to running the cluster, you can obtain the example files from the examples repository 'docker-compose-recipes' directory.
在本教程中,您将使用 Docker compose 来 搭建 ClickHouse 集群。该配置同样可以修改后用于 独立的本地机器、虚拟机或云实例。
运行以下命令以设置本示例的目录结构:
将以下 docker-compose.yml 文件添加到 clickhouse-cluster 目录:
创建以下子目录和文件:
- The
config.ddirectory contains ClickHouse server configuration fileconfig.xml, in which custom configuration for each ClickHouse node is defined. This configuration gets combined with the defaultconfig.xmlClickHouse configuration file that comes with every ClickHouse installation. - The
users.ddirectory contains user configuration fileusers.xml, in which custom configuration for users is defined. This configuration gets combined with the default ClickHouseusers.xmlconfiguration file that comes with every ClickHouse installation.
It is a best practice to make use of the config.d and users.d directories when
writing your own configuration, rather than directly modifying the default configuration
in /etc/clickhouse-server/config.xml and etc/clickhouse-server/users.xml.
The line
Ensures that the configuration sections defined in the config.d and users.d
directories override the default configuration sections defined in the default
config.xml and users.xml files.
配置 ClickHouse 节点
服务器配置
现在修改位于 fs/volumes/clickhouse-{}/etc/clickhouse-server/config.d 的每个空配置文件 config.xml。下面高亮显示的行需要根据每个节点的具体情况进行修改:
| 目录 | 文件 |
|---|---|
fs/volumes/clickhouse-01/etc/clickhouse-server/config.d | config.xml |
fs/volumes/clickhouse-02/etc/clickhouse-server/config.d | config.xml |
下文将详细说明上述配置文件的各个部分。
网络和日志记录
External communication to the network interface is enabled by activating the listen host setting. This ensures that the ClickHouse server host is reachable by other hosts:
The port for the HTTP API is set to 8123:
The TCP port for interaction by ClickHouse's native protocol between clickhouse-client
and other native ClickHouse tools, and clickhouse-server and other clickhouse-servers
is set to 9000:
日志记录在 <logger> 块中定义。此示例配置提供一个调试日志,该日志将在达到 1000M 时滚动三次:
有关日志配置的更多信息,请参阅默认 ClickHouse 配置文件中的注释说明。
集群配置
集群配置在 <remote_servers> 块中设置。
这里定义了集群名称 cluster_2S_1R。
<cluster_2S_1R></cluster_2S_1R> 块定义了集群的布局,
使用 <shard></shard> 和 <replica></replica> 设置,并作为
分布式 DDL 查询的模板。分布式 DDL 查询是指使用 ON CLUSTER 子句在整个
集群中执行的查询。默认情况下,分布式 DDL 查询
处于启用状态,但也可以通过设置 allow_distributed_ddl_queries 来禁用。
internal_replication 默认设置为 false,因为每个分片仅有一个副本。
For each server, the following parameters are specified:
| Parameter | Description | Default Value |
|---|---|---|
host | The address of the remote server. You can use either the domain or the IPv4 or IPv6 address. If you specify the domain, the server makes a DNS request when it starts, and the result is stored as long as the server is running. If the DNS request fails, the server does not start. If you change the DNS record, you need to restart the server. | - |
port | The TCP port for messenger activity (tcp_port in the config, usually set to 9000). Not to be confused with http_port. | - |
Keeper 配置
<ZooKeeper> 部分用于指定 ClickHouse Keeper(或 ZooKeeper)的运行位置。
由于使用的是 ClickHouse Keeper 集群,需要指定集群中的每个 <node>,
并分别通过 <host> 和 <port> 标签指定其主机名和端口号。
ClickHouse Keeper 的设置将在教程的下一步骤中进行说明。
尽管可以在与 ClickHouse Server 相同的服务器上运行 ClickHouse Keeper,但在生产环境中,我们强烈建议将 ClickHouse Keeper 部署在专用主机上。
宏配置
此外,<macros> 配置段用于定义复制表的参数替换。这些宏参数列在 system.macros 表中,允许在查询中使用 {shard} 和 {replica} 等替换变量。
这些配置需要根据集群的实际布局进行相应定义。
用户配置
现在修改位于 fs/volumes/clickhouse-{}/etc/clickhouse-server/users.d 路径下的每个空配置文件 users.xml,添加以下内容:
| 目录 | 文件 |
|---|---|
fs/volumes/clickhouse-01/etc/clickhouse-server/users.d | users.xml |
fs/volumes/clickhouse-02/etc/clickhouse-server/users.d | users.xml |
在此示例中,为简化配置,默认用户未设置密码。 在生产环境中,不建议采用此配置。
在此示例中,集群中所有节点的 users.xml 文件都相同。
配置 ClickHouse Keeper
Keeper 配置
In order for replication to work, a ClickHouse keeper cluster needs to be set up and configured. ClickHouse Keeper provides the coordination system for data replication, acting as a stand in replacement for Zookeeper, which could also be used. ClickHouse Keeper is, however, recommended, as it provides better guarantees and reliability and uses fewer resources than ZooKeeper. For high availability and to keep quorum, it is recommended to run at least three ClickHouse Keeper nodes.
ClickHouse Keeper can run on any node of the cluster alongside ClickHouse, although it is recommended to have it run on a dedicated node which allows scaling and managing the ClickHouse Keeper cluster independently of the database cluster.
Create the keeper_config.xml files for each ClickHouse Keeper node
using the following command from the root of the example folder:
Modify the empty configuration files which were created in each
node directory fs/volumes/clickhouse-keeper-{}/etc/clickhouse-keeper. The
highlighted lines below need to be changed to be specific to each node:
| 目录 | 文件 |
|---|---|
fs/volumes/clickhouse-keeper-01/etc/clickhouse-keeper | keeper_config.xml |
fs/volumes/clickhouse-keeper-02/etc/clickhouse-keeper | keeper_config.xml |
fs/volumes/clickhouse-keeper-03/etc/clickhouse-keeper | keeper_config.xml |
Each configuration file will contain the following unique configuration (shown below).
The server_id used should be unique for that particular ClickHouse Keeper node
in the cluster and match the server <id> defined in the <raft_configuration> section.
tcp_port is the port used by clients of ClickHouse Keeper.
The following section is used to configure the servers that participate in the quorum for the raft consensus algorithm:
ClickHouse Cloud removes the operational burden associated with managing shards and replicas. The platform automatically handles high availability, replication, and scaling decisions. Compute and storage are separate and scale based on demand without requiring manual configuration or ongoing maintenance.
测试部署配置
确保 Docker 在您的机器上运行。
在 cluster_2S_1R 目录的根目录下使用 docker-compose up 命令启动集群:
您应该会看到 Docker 开始拉取 ClickHouse 和 Keeper 镜像, 然后启动容器:
要验证集群是否正在运行,请连接到 clickhouse-01 或 clickhouse-02 并运行以下查询。连接到第一个节点的命令如下所示:
如果成功,您将看到 ClickHouse 客户端提示符:
运行以下查询以检查各主机定义的集群拓扑:
运行以下查询以检查 ClickHouse Keeper 集群的状态:
The mntr command is also commonly used to verify that ClickHouse Keeper is
running and to get state information about the relationship of the three Keeper nodes.
In the configuration used in this example, there are three nodes working together.
The nodes will elect a leader, and the remaining nodes will be followers.
The mntr command gives information related to performance, and whether a particular
node is a follower or a leader.
You may need to install netcat in order to send the mntr command to Keeper.
Please see the nmap.org page for download information.
Run the command below from a shell on clickhouse-keeper-01, clickhouse-keeper-02, and
clickhouse-keeper-03 to check the status of each Keeper node. The command
for clickhouse-keeper-01 is shown below:
The response below shows an example response from a follower node:
The response below shows an example response from a leader node:
至此,您已成功部署了一个单分片双副本的 ClickHouse 集群。 下一步,您将在该集群中创建表。
创建数据库
现在您已验证集群已正确设置并正在运行,接下来将重新创建与 UK property prices 示例数据集教程中使用的相同表。该表包含自 1995 年以来英格兰和威尔士房地产交易价格的约 3000 万行数据。
通过在不同的终端标签页或窗口中分别运行以下各命令,连接到每个主机的客户端:
您可以在每个主机的 clickhouse-client 中运行以下查询,确认除默认数据库外尚未创建其他数据库:
从 clickhouse-01 客户端执行以下分布式 DDL 查询,使用 ON CLUSTER 子句创建名为 uk 的新数据库:
您可以再次从每个主机的客户端运行相同的查询,
以确认数据库已在整个集群中创建,
即使查询仅在 clickhouse-01 上执行:
在集群上创建表
数据库创建完成后,接下来创建表。 在任意主机客户端上运行以下查询:
请注意,该查询与 英国房产价格 示例数据集教程中原始 CREATE 语句所使用的查询完全相同,唯一的区别是增加了 ON CLUSTER 子句。
ON CLUSTER 子句用于分布式执行 DDL(数据定义语言)查询,例如 CREATE、DROP、ALTER 和 RENAME,以确保这些架构变更应用于集群中的所有节点。
您可以在各主机的客户端上运行以下查询,以确认表已在集群中创建:
在插入英国房价数据之前,让我们先进行一个快速实验,看看从任一主机向普通表插入数据时会发生什么。
从任一主机执行以下查询以创建测试数据库和表:
现在从 clickhouse-01 运行以下 INSERT 查询:
切换到 clickhouse-02 并运行以下 INSERT 查询:
现在从 clickhouse-01 或 clickhouse-02 运行以下查询:
您会注意到,与 ReplicatedMergeTree 表不同,这里只返回插入到该特定主机上的那一行数据,而不是两行都返回。
要跨两个分片读取数据,我们需要一个能够处理跨所有分片查询的接口,该接口在运行 SELECT 查询时合并来自两个分片的数据,在运行 INSERT 查询时将数据插入到两个分片。
在 ClickHouse 中,此接口称为分布式表,通过 Distributed 表引擎创建。下面我们来看看它的工作原理。
创建分布式表
使用以下查询创建分布式表:
在此示例中,选择 rand() 函数作为分片键,使插入操作随机分布到各个分片上。
现在从任一主机查询分布式表,您将获得在两台主机上插入的所有行,这与之前的示例不同:
对英国房产价格数据执行相同操作。从任意主机客户端运行以下查询,使用之前通过 ON CLUSTER 创建的现有表来创建分布式表:
向分布式表插入数据
现在连接到任意一台主机并插入数据:
数据插入后,可以使用分布式表检查行数:
在任一主机上运行以下查询,您将看到数据已基本均匀地分布在各个分片上(请注意,由于插入分片的选择是通过 rand() 设置的,因此您的结果可能会有所不同):
如果其中一台主机发生故障会怎样?让我们通过关闭 clickhouse-01 来模拟这种情况:
运行以下命令检查主机是否已停止运行:
现在从 clickhouse-02 运行之前在分布式表上执行的相同 select 查询:
遗憾的是,我们的集群不具备容错能力。如果其中一台主机发生故障,集群将被视为不健康状态,查询将会失败。这与我们在前面示例中看到的复制表不同——在复制表的情况下,即使其中一台主机发生故障,我们仍然能够插入数据。
结论
这种集群拓扑结构的优势在于,数据被分布在不同的主机上,每个节点只使用一半的存储空间。更重要的是,查询会在两个分片上并行处理,这在内存利用率方面更高效,同时减少了每个主机的 I/O。
这种集群拓扑结构的主要劣势是,一旦丢失其中一台主机,我们将无法继续提供查询服务。
在下一个示例中,我们将介绍如何设置一个包含两个分片和两个副本的集群,以同时提供可扩展性和容错能力。