docker swarm搭建clickhouse集群

2021年3月23日 12点热度 0条评论 来源: qq_38360409

docker swarm搭建clickhouse集群

1.编写docker-componse.yaml文件

version: '3'

services:
  zookeeper:
    image: zookeeper
    ports:
      - "2181:2181"
      - "2182:2182"
    volumes:
      - "/data/clickhouse/zookeeper/data:/data"
      - "/data/clickhouse/zookeeper/log:/datalog"
    deploy:
      placement:
        constraints:
          - node.hostname ==  ck-1
  tabix:
    image: spoonest/clickhouse-tabix-web-client
    ports:
      - "8080:80"
    deploy:
      placement:
        constraints:
          - node.hostname ==  ck-1
  clickhouse01:
    image: yandex/clickhouse-server
    user: root
    deploy:
      placement:
        constraints:
          - node.hostname == ck-2
    ports:
      - "8123:8123"
      - "9000:9000"
    volumes:
      - /data/clickhouse/clickhouse-server/etc:/etc/clickhouse-server
      - /data/clickhouse/clickhouse-server/data:/var/lib/clickhouse
      - /data/clickhouse/clickhouse-server/log:/var/log/clickhouse-server/
    ulimits:
      nofile:
        soft: 262144
        hard: 262144
    depends_on:
      - "zookeeper"
  clickhouse02:
    image: yandex/clickhouse-server
    user: root
    deploy:
      placement:
        constraints:
          - node.hostname == ck-3
    ports:
      - "9012:9000"
      - "8134:8123"
    volumes:
      - /data/clickhouse/clickhouse-server/etc:/etc/clickhouse-server
      - /data/clickhouse/clickhouse-server/data:/var/lib/clickhouse
      - /data/clickhouse/clickhouse-server/log:/var/log/clickhouse-server/
    ulimits:
      nofile:
        soft: 262144
        hard: 262144
    depends_on:
      - "zookeeper"

2.创建数据存储目录

# 创建目录
mkdir /data
# 挂盘
mount /dev/vdb1 /data
# 创建映射目录
mkdir -p /data/clickhouse/zookeeper/data
mkdir -p /data/clickhouse/zookeeper/log
mkdir -p /data/clickhouse/clickhouse-server/etc
mkdir -p /data/clickhouse/clickhouse-server/data
mkdir -p /data/clickhouse/clickhouse-server/log

3.上传config.xml及usesr.xml配置文件
config.xml配置文件可以从单机部署的clickhouse获取,或者下载下面的
链接:https://pan.baidu.com/s/1uERbusWSwM5fuBp1WL5Oaw
提取码:x7a6
注意:config.xml文件需要修改相关配置如下

# 删除remote_servers内的配置,incl参数是为了识别外部文件添加的
<remote_servers incl="clickhouse_remote_servers">
	
</remote_servers>
# 添加外部配置文件
<include_from>/etc/clickhouse-server/metrika.xml</include_from>

users.xml

<yandex>   
    <profiles>       
        <default>   
            <!-- 用于在单个服务器上运行查询的最大RAM -->
            <max_memory_usage>10000000000</max_memory_usage>     
            <!-- 使用未压缩数据块的缓存 -->
            <use_uncompressed_cache>0</use_uncompressed_cache>    
            <!-- 分布式查询处理过程中选择副本的方式 -->
            <!-- random   nearest_hostname:从错误最少的副本集合中选择    in_order:按照指定的顺序选择第一个活副本    first_or_random:如果第一个副本错误数较高,则从错误数最少的副本中随机选择一个副本  -->
            <load_balancing>random</load_balancing>
            <!-- 参数设置 -->
            <input_format_import_nested_json>1</input_format_import_nested_json>
            <allow_experimental_map_type>1</allow_experimental_map_type>
        </default>       
        <readonly>           
            <max_memory_usage>10000000000</max_memory_usage>           
            <use_uncompressed_cache>0</use_uncompressed_cache>           
            <load_balancing>random</load_balancing>            
            <readonly>1</readonly>       
        </readonly>   
    </profiles>
    <!-- 跟踪每个小时的资源消耗,而不限制使用量 -->
    <quotas>           
        <!-- Name of quota. -->       
        <default>           
            <interval>               
                <duration>3600</duration>                
                <queries>0</queries>               
                <errors>0</errors>               
                <result_rows>0</result_rows>               
                <read_rows>0</read_rows>               
                <execution_time>0</execution_time>           
            </interval>       
        </default>   
    </quotas>   
    <users>       
        <default>           
            <password_sha256_hex>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08</password_sha256_hex>           
            <networks incl="networks" replace="replace">                
                <ip>::/0</ip>           
            </networks>           
            <profile>default</profile>      
            <quota>default</quota>
        </default>
        <ck>   
            <password_sha256_hex>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08</password_sha256_hex>  
            <networks incl="networks" replace="replace">      
                <ip>::/0</ip>   
            </networks>   
            <profile>readonly</profile>     
            <quota>default</quota>  
        </ck>
    </users>
</yandex>
# 将以上俩个文件上传到/data/clickhouse/clickhouse-server/etc下
mv config.xml /data/clickhouse/clickhouse-server/etc
mv users.xml /data/clickhouse/clickhouse-server/etc
# users.xml中的密码需要自定义,建议使用加密的方式填写
# 生成密文的方法
PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD";echo -n "$PASSWORD" | sha256sum | tr -d '-'

4.创建集群配置文件

cd /data/clickhouse/clickhouse-server/etc 
vim metrika.xml

metrika.xml

<yandex>
    <!-- 集群配置 -->
    <clickhouse_remote_servers>
        <my_cluster>
            <!-- 数据分片1  -->
            <shard>
                <internal_replication>false</internal_replication>
                <replica>
                    <host>ck-2</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>test</password>
                </replica>
                <replica>
                    <host>ck-3</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>test</password>
                </replica>
            </shard>
            <!-- 数据分片2  -->
            <shard>
                <internal_replication>false</internal_replication>
                <replica>
                    <host>ck-3</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>test</password>
                </replica>
                <replica>
                    <host>ck-2</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>test</password>
                </replica>
            </shard>
        </my_cluster>
    </clickhouse_remote_servers>
    <!-- ZK  -->
    <zookeeper-servers>
        <node index="1">
            <host>ck-1</host>
            <port>2181</port>
        </node>
    </zookeeper-servers>
    <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>
        </case>
    </clickhouse_compression>
</yandex>

5.启动clickhouse集群

# 启动clickhouse
docker stack deploy -c docker-compose.yaml clickhouse

6.查询集群信息

select * from system.clusters;


PS:对于配置文件等有问题的朋友欢迎留言讨论,也可以加本人知识星球:https://t.zsxq.com/nUN7QRj
,一起讨论交流。

    原文作者:qq_38360409
    原文地址: https://blog.csdn.net/qq_38360409/article/details/115128633
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。