ClickHouse高可用集群搭建(离线安装)

一、简介

ClickHouse官网

二、生产集群搭建

准备

  • Zookeeper集群
  • Linux服务器四台(以四台为列)

创建用户组和用户

groupadd clickhouse  ---创建用户组
useradd -m -d /home/clickhouse -g clickhouse clickhouse ---创建用户
passwd clickhouse ---修改密码

下载

准备好ClickHouse安装包,官网下载地址(选择需要下载的版本)

  • clickhouse-common-static-$LATEST_VERSION.tgz
  • clickhouse-common-static-dbg-$LATEST_VERSION.tgz
  • clickhouse-server-$LATEST_VERSION.tgz
  • clickhouse-client-$LATEST_VERSION.tgz

解压、安装(注意切换用户clickhouse)

tar -xzvf clickhouse-common-static-$LATEST_VERSION.tgz
sudo clickhouse-common-static-$LATEST_VERSION/install/doinst.sh

tar -xzvf clickhouse-common-static-dbg-$LATEST_VERSION.tgz
sudo clickhouse-common-static-dbg-$LATEST_VERSION/install/doinst.sh

tar -xzvf clickhouse-server-$LATEST_VERSION.tgz
sudo clickhouse-server-$LATEST_VERSION/install/doinst.sh
#启动命令
sudo systemctl start clickhouse-server  或  sudo /etc/init.d/clickhouse-server start  

#安装clickhouse-client,在其中一台机器安装即可,用于命令连接clickhouse-server
tar -xzvf clickhouse-client-$LATEST_VERSION.tgz
sudo clickhouse-client-$LATEST_VERSION/install/doinst.sh

修改配置

安装成功后,在/etc/clickhouse-server/ 目录下生成配置文件config.xml及user.xml、自定义配置目录config.d及users.d,推荐在config.d及users.d下新建配置文件,然后在config.xml及user.xml中引入config.d及users.d下的配置文件。

  • 修改config.xml
mkdir /opt/clickhouse_data    #新建ClickHouse数据目录
chown -R clickhouse:clickhouse /opt/clickhouse_data #注意修改目录权限
sudo vim /etc/clickhouse-server/config.xml

<level>trace</level>
<log>/opt/clickhouse_data/log/clickhouse-server/clickhouse-server.log</log>
<errorlog>/opt/clickhouse_data/log/clickhouse-server/clickhouse-server.err.log</errorlog>
<path>/opt/clickhouse_data/clickhouse/</path>
<tmp_path>/opt/clickhouse_data/clickhouse/tmp/</tmp_path>
<user_files_path>/opt/clickhouse_data/clickhouse/user_files/</user_files_path>
<include_from>/etc/clickhouse-server/config.d/metrika.xml</include_from> <!--添加该参数-->

  • 新建/etc/clickhouse-server/config.d/metrika.xml
vim /etc/clickhouse-server/config.d/metrika.xml

<yandex>
<!--ck集群节点-->
<remote_servers>
    <test_sipo_cluster>
        <!--分片1-->
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>iZqj001np4kv0sxzzgvyu1Z</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
            <replica>
                <host>iZqj001np4kv0sxzzgvyu0Z</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
        </shard>
        <!--分片2-->
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>iZqj001np4kv0sxzzgvyu3Z</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
            <replica>
                <host>iZqj001np4kv0sxzzgvyu4Z</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
        </shard>
        <!--分片3-->
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>iZqj001np4kv0sxzzgvyu2Z</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
            <replica>
                <host>iZqj001np4kv0sy1yi00tbZ</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
        </shard>
        <!--分片4-->
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>iZqj001np4kv0sy1yi00tcZ</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
            <replica>
                <host>iZqj001np4kv0sy1yi00t9Z</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
        </shard>
    </test_sipo_cluster>
    <test_sipo_cluster_8>
        <!--分片1-->
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>iZqj001np4kv0sxzzgvyu1Z</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
        </shard>
        <!--分片2-->
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>iZqj001np4kv0sxzzgvyu0Z</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
        </shard>
        <!--分片3-->
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>iZqj001np4kv0sxzzgvyu3Z</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
        </shard>
        <!--分片4-->
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>iZqj001np4kv0sxzzgvyu4Z</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
        </shard>
        <!--分片5-->
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>iZqj001np4kv0sxzzgvyu2Z</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
        </shard>
        <!--分片6-->
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>iZqj001np4kv0sy1yi00tbZ</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
        </shard>
        <!--分片7-->
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>iZqj001np4kv0sy1yi00tcZ</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
        </shard>
        <!--分片8-->
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>iZqj001np4kv0sy1yi00t9Z</host>
                <port>9099</port>
                <user>default</user>
                <password>admin</password>
                <compression>true</compression>
            </replica>
        </shard>
    </test_sipo_cluster_8>
</remote_servers>

<!--zookeeper相关配置-->
<zookeeper>
    <node>
        <host>master01</host>
        <port>2181</port>
    </node>
    <node>
        <host>master02</host>
        <port>2181</port>
    </node>
    <node>
        <host>slave01</host>
        <port>2181</port>
    </node>
</zookeeper>

<macros>
    <replica>iZqj001np4kv0sxzzgvyu1Z</replica> <!--当前节点主机名-->
</macros>

<!--
         <macros>
        <shard>01</shard>
        <replica>example01-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> <!--压缩算法lz4压缩比zstd快, 更占磁盘-->
    </case>
</clickhouse_compression>
</yandex>

上述配置中三个节点中不同的地方在于

<macros>
  <replica>192.168.xxx.xxx</replica>
</macros>

改为当前节点的IP即可

  • 新建/etc/clickhouse-server/config.d/listen.xml
vim /etc/clickhouse-server/config.d/listen.xml

<yandex>
    <listen_host>0.0.0.0</listen_host>
</yandex>

  • 新建/etc/clickhouse-server/users.d/default-password.xml
vim /etc/clickhouse-server/users.d/default-password.xml

<yandex>
    <users>
        <default>
            <password remove='1' />
            <password_sha256_hex>8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918</password_sha256_hex>
        </default>
    </users>
</yandex>

重启clickhouse服务

sudo systemctl stop clickhouse-server  或 /etc/init.d/clickhouse-server restart

验证

在每个节点启动clickhouse客户端,和单节点启动完全一样,查询集群信息

select * from system.clusters;

集群信息

cluster                          |shard_num|shard_weight|replica_num|host_name  |host_address|port|is_local|user   |default_database|errors_count|estimated_recovery_time|
---------------------------------|---------|------------|-----------|-----------|------------|----|--------|-------|----------------|------------|-----------------------|
test_ck_cluster                  |        1|           1|          1|10.160.22.6|10.160.22.6 |9000|       1|default|                |           0|                      0|
test_ck_cluster                  |        1|           1|          2|10.160.22.7|10.160.22.7 |9000|       0|default|                |           0|                      0|
test_ck_cluster                  |        2|           1|          1|10.160.22.8|10.160.22.8 |9000|       0|default|                |           0|                      0|
test_ck_cluster                  |        2|           1|          2|10.160.22.9|10.160.22.9 |9000|       0|default|                |           0|                      0|
test_cluster_two_shards          |        1|           1|          1|127.0.0.1  |127.0.0.1   |9000|       1|default|                |           0|                      0|
test_cluster_two_shards          |        2|           1|          1|127.0.0.2  |127.0.0.2   |9000|       0|default|                |           0|                      0|
test_cluster_two_shards_localhost|        1|           1|          1|localhost  |127.0.0.1   |9000|       1|default|                |           0|                      0|
test_cluster_two_shards_localhost|        2|           1|          1|localhost  |127.0.0.1   |9000|       1|default|                |           0|                      0|
test_shard_localhost             |        1|           1|          1|localhost  |127.0.0.1   |9000|       1|default|                |           0|                      0|
test_shard_localhost_secure      |        1|           1|          1|localhost  |127.0.0.1   |9440|       0|default|                |           0|                      0|
test_unavailable_shard           |        1|           1|          1|localhost  |127.0.0.1   |9000|       1|default|                |           0|                      0|
test_unavailable_shard           |        2|           1|          1|localhost  |127.0.0.1   |   1|       0|default|                |           0|                      0|

转自:https://www.jianshu.com/p/59a2e50b5be4

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容