# ClickHouse集群搭建的方法
## 1. ClickHouse簡介
ClickHouse是俄羅斯Yandex公司開發的一個開源的列式數據庫管理系統(DBMS),主要用于在線分析處理(OLAP)。它能夠以極高的速度進行實時數據分析,支持線性擴展,非常適合大規模數據分析場景。
### 1.1 核心特性
- **列式存儲**:數據按列存儲,壓縮效率高
- **向量化執行引擎**:利用CPU SIMD指令加速查詢
- **分布式查詢處理**:支持多服務器并行處理
- **實時數據攝入**:支持高吞吐量的數據插入
- **高可用性**:支持數據復制和故障轉移
## 2. 集群架構設計
### 2.1 基本概念
ClickHouse集群由多個分片(Shard)組成,每個分片可以包含多個副本(Replica)。數據在不同分片間分布,在副本間復制。
### 2.2 典型架構
[Client]
|
+----------------+----------------+
| | |
[Shard1] [Shard2] [Shard3]
/ \ / \ / \
[Replica1] [Replica2] [Replica1] [Replica2] [Replica1] [Replica2]
### 2.3 硬件要求
| 組件 | 推薦配置 |
|---------------|-----------------------------------|
| CPU | 16核以上,支持AVX指令集 |
| 內存 | 64GB以上 |
| 存儲 | SSD或NVMe,建議RD10配置 |
| 網絡 | 10Gbps以上網絡連接 |
## 3. 環境準備
### 3.1 操作系統要求
- Linux (推薦CentOS 7+/Ubuntu 18.04+)
- 內核版本3.10+
- 關閉swap: `swapoff -a && sed -i '/swap/s/^/#/' /etc/fstab`
- 調整文件描述符限制:
```bash
echo "fs.file-max = 500000" >> /etc/sysctl.conf
echo "* soft nofile 262144" >> /etc/security/limits.conf
echo "* hard nofile 262144" >> /etc/security/limits.conf
# CentOS
yum install -y epel-release
yum install -y clickhouse-server clickhouse-client
# Ubuntu
apt-get install -y apt-transport-https ca-certificates dirmngr
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E0C56BD4
echo "deb https://repo.clickhouse.com/deb/stable/ main/" | tee /etc/apt/sources.list.d/clickhouse.list
apt-get update
apt-get install -y clickhouse-server clickhouse-client
ClickHouse主要配置文件位于/etc/clickhouse-server/
目錄:
config.xml
:主配置文件users.xml
:用戶權限配置metrika.xml
:集群配置(通常通過include引入)創建/etc/clickhouse-server/config.d/metrika.xml
:
<yandex>
<clickhouse_remote_servers>
<cluster_3shards_2replicas>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>shard1-replica1</host>
<port>9000</port>
</replica>
<replica>
<host>shard1-replica2</host>
<port>9000</port>
</replica>
</shard>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>shard2-replica1</host>
<port>9000</port>
</replica>
<replica>
<host>shard2-replica2</host>
<port>9000</port>
</replica>
</shard>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>shard3-replica1</host>
<port>9000</port>
</replica>
<replica>
<host>shard3-replica2</host>
<port>9000</port>
</replica>
</shard>
</cluster_3shards_2replicas>
</clickhouse_remote_servers>
<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>
<macros>
<shard>01</shard>
<replica>shard1-replica1</replica>
</macros>
</yandex>
注意:每個節點需要修改<macros>
部分以匹配自身角色。
在config.xml
中啟用復制:
<distributed_ddl>
<path>/clickhouse/task_queue/ddl</path>
</distributed_ddl>
編輯users.xml
設置管理員賬戶:
<users>
<admin>
<password>secure_password</password>
<networks>
<ip>::/0</ip>
</networks>
<profile>default</profile>
<quota>default</quota>
<access_management>1</access_management>
</admin>
</users>
ClickHouse使用ZooKeeper協調分布式操作和復制。
# 在所有ZK節點上執行
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz
tar -xzf apache-zookeeper-3.6.3-bin.tar.gz
mv apache-zookeeper-3.6.3-bin /opt/zookeeper
創建/opt/zookeeper/conf/zoo.cfg
:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/var/lib/zookeeper
clientPort=2181
server.1=zk1:2888:3888
server.2=zk2:2888:3888
server.3=zk3:2888:3888
在每個節點創建myid
文件:
# 在zk1上
echo "1" > /var/lib/zookeeper/myid
# 在zk2上
echo "2" > /var/lib/zookeeper/myid
# 在zk3上
echo "3" > /var/lib/zookeeper/myid
/opt/zookeeper/bin/zkServer.sh start
systemctl enable clickhouse-server
systemctl start clickhouse-server
-- 在任意節點執行
SELECT * FROM system.clusters;
-- 檢查ZooKeeper連接
SELECT * FROM system.zookeeper WHERE path = '/';
在每個節點上創建相同的本地表結構:
CREATE TABLE default.test_local ON CLUSTER cluster_3shards_2replicas (
id UInt64,
event_time DateTime,
data String
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/test_local', '{replica}')
PARTITION BY toYYYYMM(event_time)
ORDER BY (id, event_time);
在任意節點創建分布式表:
CREATE TABLE default.test_distributed ON CLUSTER cluster_3shards_2replicas AS default.test_local
ENGINE = Distributed(cluster_3shards_2replicas, default, test_local, rand());
-- 插入到分布式表,數據會自動分配到各分片
INSERT INTO test_distributed VALUES
(1, now(), 'data1'),
(2, now(), 'data2');
-- 直接插入到本地表
INSERT INTO test_local VALUES
(3, now(), 'local data');
-- 從分布式表查詢(聚合所有分片數據)
SELECT * FROM test_distributed;
-- 從本地表查詢
SELECT * FROM test_local;
-- 查看查詢日志
SELECT * FROM system.query_log;
-- 查看正在運行的查詢
SELECT * FROM system.processes;
-- 查看副本狀態
SELECT * FROM system.replicas;
配置config.xml
:
<prometheus>
<endpoint>/metrics</endpoint>
<port>9363</port>
<metrics>true</metrics>
<events>true</events>
<asynchronous_metrics>true</asynchronous_metrics>
</prometheus>
-- 優化表
OPTIMIZE TABLE test_local FINAL;
-- 查看表大小
SELECT
table,
formatReadableSize(sum(bytes)) as size
FROM system.parts
WHERE active
GROUP BY table;
癥狀:副本表無法同步,報ZooKeeper錯誤
解決:
1. 檢查ZooKeeper服務狀態
2. 驗證網絡連接
3. 檢查/etc/hosts
中的主機名解析
癥狀:副本間數據不一致
解決:
1. 檢查system.replicas
表
2. 嘗試SYSTEM RESTART REPLICA
命令
3. 必要時從其他副本重新同步
癥狀:查詢響應變慢
解決:
1. 檢查系統資源使用情況
2. 分析慢查詢system.query_log
3. 考慮添加索引或優化表結構
metrika.xml
ClickHouse集群搭建需要仔細規劃分片和副本策略,正確配置ZooKeeper協調服務,并通過分布式表實現透明的數據訪問。本文詳細介紹了從環境準備到集群配置、表創建、數據操作和維護的全過程,遵循這些步驟可以建立一個高性能、高可用的ClickHouse分析集群。
實際生產環境中,還需要根據具體業務需求調整配置參數,并建立完善的監控告警系統,確保集群長期穩定運行。 “`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。