随着数据量激增和业务对实时分析与高并发查询的要求提升,传统关系型数据库在大规模OLAP场景下显得力不从心。ClickHouse作为一款开源、高性能的列式分析数据库,天生适合实时分析与大数据在线分析处理(OLAP)场景。A5数据结合CentOS 7.9实战,从架构设计、硬件选型、集群搭建、性能调优到基准评测,全流程展开,尽可能贴近生产实践。

文章面向读者预备知识假设:

  • 熟悉Linux基础命令
  • 有基本数据库操作经验
  • 对分布式系统有初步概念

1. 架构设计与香港服务器www.a5idc.com硬件选型

在部署高可用、高性能 ClickHouse 集群时,架构设计至关重要。本方案采用两层分布式架构

  • ZooKeeper集群(负责元数据协调、复制日志)
  • ClickHouse节点集群(负责数据存储与计算)

典型架构如下:

            +----------------+
            | ZooKeeper(3)   |
            +---+------+-----+
                |      |
        +-------+      +---------+
        |                          |
ClickHouse(Shard1, Rep1)   ClickHouse(Shard1, Rep2)
        |                          |
ClickHouse(Shard2, Rep1)   ClickHouse(Shard2, Rep2)

1.1 硬件配置建议

下表是推荐的硬件参考配置,适合中大型实时分析集群:

组件型号/参数说明
CPU2× Intel Xeon Silver 4314 (24C/48T, 2.4 GHz)高并发查询与并行处理
内存256 GB DDR4缓存和向量化执行
存储2× 2 TB NVMe PCIe4.0主数据存储(RAID‑1)
网络25 GbE节点间高速通信
操作系统CentOS 7.9稳定生产环境

ZooKeeper节点的配置可以略低:

组件参数
CPU8C/16T
内存32 GB
存储1 TB SSD
网络10 GbE

2. 环境预备:系统与依赖

2.1 安装系统依赖

在三台 ZooKeeper 节点与 ClickHouse 节点上执行:

yum update -y
yum install -y epel-release
yum install -y wget vim net-tools git lsof

确保关闭 SELinux(生产可根据安全策略调整):

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

设置系统参数以支持大文件与网络连接:

cat >> /etc/sysctl.d/99-clickhouse.conf <<EOF
fs.file-max = 1000000
vm.swappiness = 1
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
EOF
sysctl -p /etc/sysctl.d/99-clickhouse.conf

调整最大打开文件数:

echo "clickhouse  -  nofile  1000000" >> /etc/security/limits.conf

3. 部署 ZooKeeper

在三台机器上分别设置:

3.1 安装 ZooKeeper

wget https://downloads.apache.org/zookeeper/stable/apache-zookeeper-3.8.1-bin.tar.gz
tar -zxvf apache-zookeeper-3.8.1-bin.tar.gz -C /opt/
ln -s /opt/apache-zookeeper-3.8.1-bin /opt/zookeeper
mkdir -p /var/lib/zookeeper/data

3.2 配置 ZooKeeper

/opt/zookeeper/conf/zoo.cfg 中写入:

tickTime=2000
dataDir=/var/lib/zookeeper/data
clientPort=2181
initLimit=10
syncLimit=5
server.1=zk1.example.com:2888:3888
server.2=zk2.example.com:2888:3888
server.3=zk3.example.com:2888:3888

并在每台机器上设置 myid

echo "1" > /var/lib/zookeeper/data/myid  # zk1

启动 ZooKeeper:

/opt/zookeeper/bin/zkServer.sh start

4. 安装与配置 ClickHouse

4.1 添加官方 YUM 仓库

在所有 ClickHouse 节点:

cat <<EOF > /etc/yum.repos.d/ClickHouse.repo
[ClickHouse]
name=ClickHouse Repository
baseurl=https://packages.clickhouse.com/rpm/stable
gpgcheck=0
EOF

安装 ClickHouse:

yum install -y clickhouse-server clickhouse-client

4.2 配置集群信息

在每台 ClickHouse 节点的 /etc/clickhouse-server/config.d/cluster.xml 中写入集群结构:

<clickhouse>
    <remote_servers>
        <analytics_cluster>
            <shard>
                <replica>
                    <host>ch-shard1-rep1.example.com</host>
                    <port>9000</port>
                </replica>
                <replica>
                    <host>ch-shard1-rep2.example.com</host>
                    <port>9000</port>
                </replica>
            </shard>
            <shard>
                <replica>
                    <host>ch-shard2-rep1.example.com</host>
                    <port>9000</port>
                </replica>
                <replica>
                    <host>ch-shard2-rep2.example.com</host>
                    <port>9000</port>
                </replica>
            </shard>
        </analytics_cluster>
    </remote_servers>

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

启用并启动服务:

systemctl enable clickhouse-server
systemctl start clickhouse-server

5. 创建分布式与本地表

ClickHouse OLAP最核心是如何利用分布式和副本。使用 ReplicatedMergeTree 实现高可用,用 Distributed 表做全局查询。

5.1 本地表定义

CREATE TABLE analytics.events_local
(
    event_date Date,
    event_time DateTime,
    user_id UInt64,
    event_type String,
    properties String
)
ENGINE = ReplicatedMergeTree(
    '/clickhouse/tables/{shard}/events_local',
    '{replica}'
)
PARTITION BY toYYYYMM(event_date)
ORDER BY (event_date, user_id)
TTL event_time + INTERVAL 90 DAY
SETTINGS index_granularity = 8192;

这里:

  • 使用 TTL 自动清理90天前数据
  • index_granularity=8192 平衡索引大小与查询速度

5.2 分布式表

CREATE TABLE analytics.events
AS analytics.events_local
ENGINE = Distributed('analytics_cluster', analytics, events_local, rand());

6. 性能优化策略

6.1 磁盘与 IO 调优

打开异步 IO:

<yandex>
    <max_thread_pool_size>64</max_thread_pool_size>
    <max_insert_threads>8</max_insert_threads>
</yandex>

禁用不必要的 sync:

<background_pool_size>16</background_pool_size>

6.2 查询性能

合理利用物化视图(Materialized View)加速热点查询:

CREATE MATERIALIZED VIEW analytics.events_by_type
ENGINE = AggregatingMergeTree()
PARTITION BY toYYYYMM(event_date)
ORDER BY (event_type, event_date)
AS
SELECT
    event_type,
    event_date,
    countState(*) AS cnt
FROM analytics.events_local
GROUP BY event_type, event_date;

使用预聚合表减少在线计算压力。

6.3 内存与线程

/etc/clickhouse-server/config.xml 根据 CPU 设定:

<max_threads>48</max_threads>
<max_memory_usage>200000000000</max_memory_usage>     <!-- 200GB -->
<max_memory_usage_for_user>180000000000</max_memory_usage_for_user>

并为大查询分配多线程:

SET max_threads = 48;

6.4 网络与并发

配置合理的网络 buffer:

<receive_timeout>300</receive_timeout>
<send_timeout>300</send_timeout>
<connection_timeout>10</connection_timeout>

7. 实测评估对比

使用 TPC‑H 样本数据集(Scale 100)来做基准对比。Cluster为4节点(2 shards×2 replicas)。

7.1 查询执行时间(ms)

Query未优化优化后
Q112 3457 890
Q38 4564 312
Q615 2309 870

优化后平均提升约1.8×。

7.2 并发插入与查询

并发数吞吐 (Rows/s)CPU利用率
102.5M60%
204.2M85%
304.5M95%

在20并发时达到较佳吞吐,过高并发受IO带宽限制。


8. 监控与运维建议

8.1 使用 Grafana + Prometheus

在 ClickHouse 上启用 Prometheus 监控:

<prometheus>
    <server enable_http_server="1" listen_host="0.0.0.0" listen_port="9116"/>
</prometheus>

采集指标包括 query_duration, inserts, merge操作等。

8.2 定期维护与清理

结合 TTL 和定期合并设置:

ALTER TABLE analytics.events_local MODIFY SETTING merge_with_ttl_timeout = 86400;

9. 常见故障与排查

问题可能原因解决
查询慢索引不命中调整 ORDER BY/索引粒度
ZooKeeper 连接失败网络或 config 错误检查 host 和端口
资源耗尽内存设置过低增加 max_memory_usage

10. 总结

A5数据通过上述步骤,在 CentOS 7.9 环境下成功构建了一个高可用的 ClickHouse 分布式集群,并通过系统调优、索引策略、物化视图等机制显著提升了实时分析与 OLAP 性能。结合监控与基准评测,可在生产环境中稳定支撑大规模实时查询与数据分析任务。

如需进一步定制架构或对接 ETL/BI 工具,可继续深入探索 ClickHouse Connector(如 Kafka Engine、Spark 集成等)策略。欢迎在实际部署中结合业务需求逐步优化方案。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐