首页 > 技术文章 > clickhouse集群搭建部署

lebaishi 2022-03-04 17:55 原文

一.安装前准备

1.文件打开数调整

在 /etc/security/limits.conf和/etc/security/limits.d/20-nproc.conf 这两个文件的末尾加入以下内容:

sudo vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 131072
* hard nproc 131072

重启服务器之后生效,用 ulimit -n 或者 ulimit -a 查看设置结果

2.取消selinux

修改 /etc/selinux/config 中的 SELINUX=disabled 后重启

vim /etc/selinux/config
SELINUX=disabled

3.关闭防火墙

systemctl status firewalld.service
systemctl stop firewalld.service

4.安装依赖

root用户执行:
yum install -y libtool
yum install -y *unixODBC*

5.验证是否支持sse 4.2指令集

需要验证当前服务器的CPU是否支持SSE 4.2指令集,因为向量化执行需要用到这项特性

grep -q sse4_2 /proc/cpuinfo && echo "SSE 4.2 supported" || echo "SSE 4.2 not
supported"

二.安装clickhouse

1.单机安装

参照官网安装步骤:

sudo yum install yum-utils
sudo rpm --import https://repo.clickhouse.com/CLICKHOUSE-KEY.GPG
sudo yum-config-manager --add-repo https://repo.clickhouse.com/rpm/clickhouse.repo
sudo yum install clickhouse-server clickhouse-client

如果没法链接互联网,则也可以使用 rpm 的方式来进行离线安装,需要下载的安装包有:

clickhouse-server-20.5.4.40-2.noarch.rpm
clickhouse-common-static-20.5.4.40-2.x86_64.rpm
clickhouse-client-20.5.4.40-2.noarch.rpm

下载地址在:
https://repo.yandex.ru/clickhouse/rpm/stable/x86_64/
https://packagecloud.io/Altinity/clickhouse

启动ck服务:

前台启动:
clickhouse-server --config-file=/etc/clickhouse-server/config.xml

后台启动:
nohup clickhouse-server --config-file=/etc/clickhouse-server/config.xml
1>~/logs/clickhouse_std.log 2>~/logs/clickhouse_err.log &

启动ck客户端:

clickhouse-client --port=xxx --host=xxx --user=xxx --password=xxx -m
常用参数:端口,主机,用户,密码

-m:多行模式

基本使用:

创建库,表:
create database test;
create table test01(id Int8,name String) engine = TinyLog;
插入数据:
insert into table test01 values (1, 'naixue'), (2, 'clickhouse'), (3,'spark');
查询数据:
select id ,name from test01;
select count(*) from test01;

image

这里补充一下创建用户,默认只有default用户,一般不用.

vim /etc/clickhouse-server/users.xml
在users标签中添加以下内容:
        <lemo>
            <password>lemo</password>
            <networks>
                <ip>::/0</ip>
            </networks>

            <!-- Settings profile for user. -->
            <profile>default</profile>

            <!-- Quota for user. -->
            <quota>default</quota>
        </lemo>

2.搭建集群模式

三台服务器分别安装上clickhouse

2.1 修改配置文件:

修改端口:(默认9000端口跟hdfs冲突)
vim /etc/clickhouse-server/config.xml
<tcp_port>9001</tcp_port>

listen_host 表示能监听的主机,:: 表示任意主机都可以访问
<listen_host>::</listen_host>

添加集群相关配置
<remote_servers>
        <clickhouse_3shards_1replicas>
        <shard>
        <!-- 数据自动同步 -->
        <internal_replication>true</internal_replication>
        <replica>
            <host>hadoop101</host>
            <port>9001</port>
        </replica>
        </shard>
        <shard>
        <replica>
            <internal_replication>true</internal_replication>
            <host>hadoop102</host>
            <port>9001</port>
        </replica>
        </shard>
        <shard>
        <internal_replication>true</internal_replication>
        <replica>
            <host>hadoop103</host>
            <port>9001</port>
        </replica>
        </shard>
    </clickhouse_3shards_1replicas>
    </remote_servers>

    <zookeeper>
        <node>
            <host>hadoop101</host>
            <port>2181</port>
        </node>
        <node>
            <host>hadoop102</host>
            <port>2181</port>
        </node>
        <node>
            <host>hadoop103</host>
            <port>2181</port>
        </node>
    </zookeeper>

    <macros>
        <shard>01</shard>
        <replica>hadoop101</replica>
    </macros>

网上资料大都是说要新建/etc/metrika.xml文件并添加集群配置,我试了一下好像不行,可能是跟版本有关系,后面发现/etc/clickhouse-server/config.xml配置文件里有集群相关配置就进行了如上配置.

可参考下面这位大佬的博客,可能也不是版本问题,以后再研究一下这个配置问题,直接在config.xml文件配置是没有问题的.
https://www.cnblogs.com/jiashengmei/p/11991243.html

分发配置文件到所有服务器.修改以下内容(以hadoop103为例):

    <macros>
        <shard>03</shard>
        <replica>hadoop103</replica>#修改成对应的hostname
    </macros>

2.2 启动服务
先启动zookeeper,再启动clickhouse

nohup clickhouse-server --config-file=/etc/clickhouse-server/config.xml > /dev/null &

2.3 查看集群信息
客户端连接任意节点:

clickhouse-client --host=hadoop102 --port=9001 --user=lemo --password=lemo -m

select * from system.clusters;

image

2.4 建库测试

create database test01 on cluster clickhouse_3shards_1replicas;
客户端连接其他节点查看数据库也有test01这个库.
image

ck集群搭建完成...

推荐阅读