Clickhouse的集群安装和部署

一、安装环境

本次安装使用clikchouse用户来安装,安装环境为CetOS7。其余linux也可适用(略有变动)
Clickhouse的环境需求官网也有介绍:
即,CH只支持Linux,且必须支持4.2 SSE指令。若是在其他环境中想要搭建CH,可以使用docker或者使用线上云服务。

System requirements for pre-built packages: Linux, x86_64 with SSE 4.2.

检测系统是否支持SSE4.2

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

关闭防火墙

# 关闭防火墙
systemctl stop firewalld.service

# 禁止开机启动防火墙
systemctl disable firewalld.service

二、版本选择和下载

创建安装包下载的目录

mkdir -p /bigdata/software/clickhouse

获取最新的稳定版本,并下载安装包

export LATEST_VERSION=$(curl -s https://repo.clickhouse.tech/tgz/stable/ | \
    grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | sort -V -r | head -n 1)
curl -O https://repo.clickhouse.tech/tgz/stable/clickhouse-common-static-$LATEST_VERSION.tgz
curl -O https://repo.clickhouse.tech/tgz/stable/clickhouse-common-static-dbg-$LATEST_VERSION.tgz
curl -O https://repo.clickhouse.tech/tgz/stable/clickhouse-server-$LATEST_VERSION.tgz
curl -O https://repo.clickhouse.tech/tgz/stable/clickhouse-client-$LATEST_VERSION.tgz

三、安装

解压安装包,并安装

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
tar -xzvf clickhouse-client-$LATEST_VERSION.tgz
sudo clickhouse-client-$LATEST_VERSION/install/doinst.sh

核心目录结构介绍

  1. /etc/clickhouse-server:服务端配置文件的目录
  2. /etc/clickhouse-client:客户端配置文件的目录
  3. /var/lib/clickhouse:默认的数据存储目录
  4. /var/log/clickhouse-server:默认的日志存储位置

配置文件介绍

  1. /etc/security/limits.d/clickhouse.conf:Clickhouse文件句柄数配置
  2. /etc/cron.d/clickhouse-server:cron定时任务配置。用于恢复因异常而中断的Clickhouse服务。默认10分钟执行一次,它会检查clickhouse服务是否在运行,若不在运行则执行start操作。
*/10 * * * * root (which service > /dev/null 2>&1 && (service clickhouse-server condstart ||:)) || /etc/init.d/clickhouse-server condstart > /dev/null 2>&1
  1. /usr/bin路径下的可执行文件
  • clickhouse:clikchouse主程序的可执行文件
  • clickhouse-client:指向Clickhouse可执行文件的软连接,用于客户端的连接
  • clickhouse-server:指向Clickhouse可执行文件的软连接,用于服务端的启动
  • clickhouse-compressor:内置提供的压缩工具,可用于数据的压缩和解压

三、启动服务

1)磁盘存储配置

修改默认数据存储路径,由于真正使用时数据量会比较大,应把这个路径切换到大容量的磁盘上。

<path>/ch/data/</path>
<tmp_path>/ch/data/tmp/</tmp_path>
<user_files_path>/ch/data/user_files/</user_files_path>

修改文件目录权限

chown -R clickhouse:clickhouse /ch/daata/

如果你有多个磁盘,则你需要去设置多个磁盘的路径,修改文件config.xml

<storage_configuration>
		...
        <disks>
            <sdc> <!-- disk name -->
                <path>/datac/clickhouse/</path>
            </sdc>
            <sdd> <!-- disk name -->
                <path>/datad/clickhouse/</path>
            </sdd>
            <sde> <!-- disk name --> 
                <path>/datae/clickhouse/</path>
            </sde>
			...
		</disks>
		...
</storage_configuration>

默认情况下,clickhouse只会往你设置的默认地址下写入数据,所以你需要设置新的存储策略。修改config.xml文件。

<storage_configuration>
	...
	<policies>
		...
	    <!-- 该策略为数据平均写到所有磁盘上 -->
	    <hdd_in_order> <!-- 策略名称 -->
	        <volumes>
	            <single>
	                <disk>default</disk> <!-- 磁盘名称 -->
	                <disk>sdc</disk> <!-- 磁盘名称 -->
	                ...
	            </single>
	        <volumes>
	    </hadd_in_order>
	    ...
	</policies>
	...
</storage_configuration>

如果这个时候重启clickhouse查询表system.storage_policies能够查询到你新配置的存储规则。
在这里插入图片描述
2)zk配置和集群配置
clickhouse中要使用集群模式,需要去集成zookeeper。zookeeper的安装此处就省略了,clickhouse需要修改metrika.xml文件,在其中添加

<zookeeper-servers>
    <node index="1">
        <host>xxxx1</host>
        <port>2181</port>
    </node>
    <node index="2">
        <host>xxxx2</host>
        <port>2181</port>
    </node>
    <node index="3">
        <host>xxxx3</host>
        <port>2181</port>
    </node>
</zookeeper-servers>

clickhouse集群中每个节点是互相没有交集的,所以需要在metrika.xml中添加上所有节点的集群信息

<clickhouse_remote_servers>
    <report_shards_replicas>
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>host1</host>
                <port>9005</port>
                <user>default</user>
                <password>xxxx</password>
            </replica>
            <!-- 若有多个分片则继续往下配replica -->
            <replica>
                <host>host4</host>
                <port>9005</port>
                <user>default</user>
                <password>xxxx</password>
            </replica>
        </shard>
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>host2</host>
                <port>9005</port>
                <user>default</user>
                <password>xxxx</password>
            </replica>
        </shard>
        <shard>
            <weight>1</weight>
            <internal_replication>true</internal_replication>
            <replica>
                <host>host3</host>
                <port>9005</port>
                <user>default</user>
                <password>xxxx</password>
            </replica>
        </shard>
	</report_shards_replicas>
</clickhouse_remote_servers>

<!-- 配置macros是为了方便后续创建分布式表的时候可以用动态参数指定表在zk上的路径 -->
<macros>
    <!-- layer可不配置,若只有一套集群 -->
    <layer>01</layer>
    <shard>01</shard>
    <replica>cluster01-01-1</replica>
</macros>
  1. 配置用户权限
<?xml version="1.0"?>
<yandex>
    <profiles>
        <!-- 读写用户配置 -->
        <default>
            <!-- 单查询最大内存使用 -->
            <max_memory_usage>10000000000</max_memory_usage>
            <!-- 是否使用未压缩格式存储缓存(一般不建议) -->
            <use_uncompressed_cache>0</use_uncompressed_cache>
            <!-- 分配模式下选择副本的方式 -->
            <load_balancing>random</load_balancing>
        </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><!-- 用户和访问权限控制 -->
    <users>
        <!-- default为用户么,可以自己指定 -->
        <default>
            <!-- 密码可以用SHA256加密 -->
            <password_sha256_hex>967f3bf355dddfabfca1c9f5cab39352b2ec1cd0b05f9e1e6b8f629705fe7d6e</password_sha256_hex>
            <!-- 访问权限设置。
                 
                 任何地方都能读取:
                    <ip>::/0</ip>
​
                 只能从本地读取:
                    <ip>::1</ip>
                    <ip>127.0.0.1</ip>
​
                 可以用正则表达式去表示。
             -->
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
            <!-- profile 指定标签 -->
            <profile>default</profile>
            <!-- Quota 指定标签 -->
            <quota>default</quota>
        </default>
        <!-- 只读用户(个人创建) -->
        <ck>
            <password_sha256_hex>967f3bf355dddfabfca1c9f5cab39352b2ec1cd0b05f9e1e6b8f629705fe7d6e</password_sha256_hex>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
            <profile>readonly</profile>
            <quota>default</quota>
        </ck>
    </users><!-- 资源限额 -->
    <quotas>
        <!-- 资源限额的名字. -->
        <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>
</yandex>

后续还有config.xml和metrika.xml其它的配置详细可看我的另一篇文章。
https://blog.csdn.net/sileiH/article/details/113404907

4) 启动clickhouse-service
在各个节点上执行:

sudo /etc/init.d/clickhouse-server start

登录CH​客户端:

本地登录:

clickhouse-client -u username --password pwd

远程登录:

clickhouse-client -h host --port port -u username --password pwd

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/sileiH/article/details/113736233