公司这几天让我搭建clickhouse集群,这一周我在公司的角色更像是运维人员搭建集群和服务,一个字干,另外kafka搭建点击这篇zookeeper集群搭建点击这篇
对于集群搭建来说使用docker来搭建更加方便,这里使用docker-compose来搭建clickhouse集群。
由于我自己就一台服务器,那就在一台搭个三分片三副本的集群,这里分二个阶段:准备阶段、搭建阶段。
1、准备阶段:连接客户端的话,一定要去服务器看看有没有配置安全组,否则连不上。
在这里插入图片描述
等搭建阶段运行端口起来后可以执行telnet命令,没有的话yum安装一个,用起来方便。

yum list telnet*              #列出telnet相关的安装包
yum install telnet-server     #安装telnet服务
yum install telnet.*          #安装telnet客户端 
telnet  你的ip  2181
#修改host文件
vim /etc/hosts
你自己的服务器ip server01
你自己的服务器ip server02
你自己的服务器ip server03

2、搭建阶段:安装docker和docker-compose、创建文件夹、修改配置、运行容器、查看状态

#更新yum
sudo yum update
#安装docker
sudo yum install docker-ce
#安装docker-compose,利用它编排和运行docker容器更方便
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
#设置可执行权限
sudo chmod +x /usr/local/bin/docker-compose
#查看安装成功
docker-compose --version
#创建目录
mkdir  /home/clickhouse
#先启动一个clickhouse-server容器,获取clickhouse-server的配置文件,操作完可以删除该容器
docker run -d --name clickhouse-server --ulimit nofile=262144:262144 yandex/clickhouse-server
#复制容器内的配置文件到宿主机,这里拷贝到/etc目录下
docker cp clickhouse-server:/etc/clickhouse-server/ /etc/
#准备clickhouse集群的配置目录
cp -rf /etc/clickhouse-server/ /etc/clickhouse-server01-01/
cp -rf /etc/clickhouse-server/ /etc/clickhouse-server01-02/
cp -rf /etc/clickhouse-server/ /etc/clickhouse-server02-01/
cp -rf /etc/clickhouse-server/ /etc/clickhouse-server02-02/
cp -rf /etc/clickhouse-server/ /etc/clickhouse-server03-01/
cp -rf /etc/clickhouse-server/ /etc/clickhouse-server03-02/

修改配置以clickhouse-01-01为例,其他的也要配置,修改include form节点为metrika.xml

vim /etc/clickhouse-server01-01/config.xml
<!-- If element has 'incl' attribute, then for it's value will be used corresponding substitution from another file.
         By default, path to file with substitutions is /etc/metrika.xml. It could be changed in config in 'include_from' element.
         Values for substitutions are specified in /yandex/name_of_substitution elements in that file.
      -->
    <include_from>/etc/clickhouse-server/metrika.xml</include_from>
    
    <listen_host>0.0.0.0</listen_host>
    <listen_host>127.0.0.1</listen_host>

修改metrika.xml,其他的也做类似修改

vim /etc/clickhouse-server01-01/metrika.xml
<!--所有实例均使用这个集群配置,不用个性化 -->
<yandex>
    <!-- 集群配置 -->
    <!-- clickhouse_remote_servers所有实例配置都一样 -->
    <!-- 集群配置 -->
      <clickhouse_remote_servers>
        <cluster_3s_1r>

            <!-- 数据分片1  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server01</host>
                    <port>9000</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server03</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 数据分片2  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server02</host>
                    <port>9002</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server01</host>
                    <port>9001</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>

            <!-- 数据分片3  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>server03</host>
                    <port>9004</port>
                    <user>default</user>
                    <password></password>
                </replica>
                <replica>
                    <host>server02</host>
                    <port>9003</port>
                    <user>default</user>
                    <password></password>
                </replica>
            </shard>
        </cluster_3s_1r>
    </clickhouse_remote_servers>

    <!-- ZK  -->
    <!-- zookeeper_servers所有实例配置都一样, 这里zookeeper根据你自己情况更改-->
    <zookeeper-servers>
        <node index="1">
            <host>你zookeeper的ip</host>
            <port>2181</port>
        </node>
        <node index="2">
            <host>你zookeeper的ip</host>
            <port>2182</port>
        </node>
        <node index="3">
            <host>你zookeeper的ip</host>
            <port>2183</port>
        </node>
    </zookeeper-servers>
    
    <!-- marcos每个实例配置不一样 分片1, 副本1 -->   
    <macros>
        <layer>01</layer>
        <shard>01</shard>
        <replica>cluster01-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>
        </case>
    </clickhouse_compression>
</yandex>

运行集群

#启动clickhouse集群,进入创建的数据挂载目录
cd /home/clickhouse
#上传编排文件
rz  docker-compose.yml
#启动
docker-compose up -d 
#查看日志
docker-compose logs

docker-compoes.yml

version: '3.1'
services:
      cs01-01:
        image: yandex/clickhouse-server
        restart: always
        container_name: cs01-01
        ports:
          - 8000:9000
          - 8123:8123
          - 9009:9009
        volumes:
             - /home/clickhouse01-01/:/var/lib/clickhouse
             - /etc/clickhouse-server01-01/:/etc/clickhouse-server/
        extra_hosts:
             - "server01:你自己的ip"
             - "server02:你自己的ip"
             - "server03:你自己的ip"
        hostname: server01

      cs01-02:
        image: yandex/clickhouse-server
        restart: always
        container_name: cs01-02
        ports:
          - 9001:9000
          - 8124:8123
          - 9010:9009
        volumes:
             - /home/clickhouse01-02/:/var/lib/clickhouse
             - /etc/clickhouse-server01-02/:/etc/clickhouse-server/
        extra_hosts:
             - "server01:你自己的ip"
             - "server02:你自己的ip"
             - "server03:你自己的ip"
        hostname: server01

      cs02-01:
        image: yandex/clickhouse-server
        restart: always
        container_name: cs02-01
        ports:
          - 9002:9000
          - 8125:8123
          - 9011:9009
        volumes:
             - /home/clickhouse02-01/:/var/lib/clickhouse
             - /etc/clickhouse-server02-01/:/etc/clickhouse-server/
        extra_hosts:
             - "server01:你自己的ip"
             - "server02:你自己的ip"
             - "server03:你自己的ip"
        hostname: server02

      cs02-02:
        image: yandex/clickhouse-server
        restart: always
        container_name: cs02-02
        ports:
            - 9003:9000
            - 8126:8123
            - 9012:9009
        volumes:
            - /home/clickhouse02-02/:/var/lib/clickhouse
            - /etc/clickhouse-server02-02/:/etc/clickhouse-server/
        extra_hosts:
             - "server01:你自己的ip"
             - "server02:你自己的ip"
             - "server03:你自己的ip"
        hostname: server02
    
      cs03-01:
        image: yandex/clickhouse-server
        restart: always
        container_name: cs03-01
        ports:
            - 9004:9000
            - 8127:8123
            - 9013:9009
        volumes:
            - /home/clickhouse03-01/:/var/lib/clickhouse
            - /etc/clickhouse-server03-01/:/etc/clickhouse-server/
        extra_hosts:
             - "server01:你自己的ip"
             - "server02:你自己的ip"
             - "server03:你自己的ip"
        hostname: server02
    
      cs03-02:
        image: yandex/clickhouse-server
        restart: always
        container_name: cs03-02
        ports:
            - 9005:9000
            - 8128:8123
            - 9014:9009
        volumes:
            - /home/clickhouse03-02/:/var/lib/clickhouse
            - /etc/clickhouse-server03-02/:/etc/clickhouse-server/
        extra_hosts:
             - "server01:你自己的ip"
             - "server02:你自己的ip"
             - "server03:你自己的ip"
        hostname: server03

恭喜你搭建成功,记得一键三连哦!

有些事总值得尝试,永不轻言放弃,前方总有希望在等待。

Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐