zookeeper + ReplicatedMergeTree(复制表) + Distributed(分布式表)
实例:
主机列表:
```
cat >> /etc/hosts<<EOF
192.168.4.9 vm-centos7-ck-0001
192.168.4.10 vm-centos7-ck-0002
192.168.4.11 vm-centos7-ck-0003
192.168.4.12 vm-centos7-ck-0004
EOF
```
ck角色划分(4台):
分片1:vm-centos7-ck-0001 vm-centos7-ck-0002 分片内两两互相复制,互为副本
分片2:vm-centos7-ck-0003 vm-centos7-ck-0004 分片内两两互相复制,互为副本
zookeeper集群(3台):最后单独分开,这里直接用
vm-centos7-ck-0001
vm-centos7-ck-0002
vm-centos7-ck-0003
zookeeper 集群进行了这样三种角色划分:leader、follower、observer分别对应着总统、议员和观察者。
总统(leader):负责进行投票的发起和决议,更新系统状态。
议员(follower):用于接收客户端请求并向客户端返回结果以及在选举过程中参与投票。
观察者(observer):也可以接收客户端连接,将写请求转发给leader节点,但是不参与投票过程,只同步leader的状态。通常对查询操作做负载。
## 安装zookeeper集群
- 安装java环境
```shll
yum install java -y
```
- 安装zk集群
```shell
leader下载二进制文件:
wget http://archive.apache.org/dist/zookeeper/zookeeper-3.6.0/apache-zookeeper-3.6.0-bin.tar.gz
rm -rf /usr/local/zookeeper
tar xf apache-zookeeper-3.6.0-bin.tar.gz -C /usr/local/
mv /usr/local/apache-zookeeper-3.6.0-bin /usr/local/zookeeper
创建目录:
mkdir /data/zookeeper/data -p
mkdir /data/logs/zookeeper -p
机器1指定节点号1,以此类推:
echo '1' > /data/zookeeper/data/myid
zookeeper配置文件:
cat > /usr/local/zookeeper/conf/zoo.cfg <<EOF
# 基本时间单位, 毫秒值
tickTime=2000
# tickTime的倍数,follower和leader之间的最长心跳时间
initLimit=30000
# tickTime的倍数,leader和follower之间发送消息, 请求和应答的最大时间
syncLimit=10
# 数据目录
dataDir=/data/zookeeper/data
# 日志目录,如果没设定,默认和dataDir相同
dataLogDir=/data/logs/zookeeper
# 监听client连接的端口号
clientPort=2181
# zookeeper最大连接
maxClientCnxns=2000
# 最大的会话超时时间
maxSessionTimeout=60000000
# 保留的文件数目,默认3个
autopurge.snapRetainCount=10
# 自动清理snapshot和事务日志,清理频率,单位是小时
autopurge.purgeInterval=1
# 等待处理的最大请求数量
globalOutstandingLimit=200
# 日志文件大小Kb,切换快照生成日志
preAllocSize=131072
#两次事务快照之间可执行事务的次数,默认的配置值为100000
snapCount=3000000
# leader是否接受client请求,默认为yes即leader可以接受client的连接,当节点数为>3时,建议关闭
leaderServes=yes
# 2888 Leader选举的端口;3888 Zookeeper服务器之间的通信端口
server.1=192.168.4.9:2888:3888
server.2=192.168.4.10:2888:3888
server.3=192.168.4.11:2888:3888
EOF
三台机都要启动:
/usr/local/zookeeper/bin/zkServer.sh start
查看状态:
# /usr/local/zookeeper/bin/zkServer.sh status
高可用说明:
1个leader + 2个follower
leader机器宕机后,会跳到其他机器替换follower成leader
恢复后自动同步为follower
```
## 安装clickhouse
- 在每个节点中安装clickhouse
- 下载仓库
```javascript
curl -s https://packagecloud.io/install/repositories/altinity/clickhouse/script.rpm.sh | sudo os=centos dist=7 bash
```
- 查看安装包
```javascript
yum list "clickhouse*"
```
- 安装服务
```javascript
yum install clickhouse-server clickhouse-client -y
```
- 查看安装列表
```javascript
yum list installed "clickhouse*"
```
- 控制台输出
```javascript
Installed Packages
clickhouse-client.noarch
clickhouse-common-static.x86_64
clickhouse-server.noarch
```
- 查看配置
```javascript
cd /etc/clickhouse-server/
vim config.xml
默认:
数据目录:/var/lib/clickhouse
临时目录:/var/lib/clickhouse/tmp
日志目录:/var/log/clickhouse-server
HTTP端口:8123
TCP 端口:9000
<http_port>8123</http_port>
<tcp_port>9000</tcp_port>
<mysql_port>9004</mysql_port>
<interserver_http_port>9009</interserver_http_port>
修改默认配置
mkdir /data/db/ck/clickhouse/tmp -p
mkdir /data/logs/clickhouse-server -p
mkdir /data/db/ck/clickhouse/data_old -p
chown clickhouse.clickhouse /data/db/ck/clickhouse -R
chown clickhouse.clickhouse /data/logs/clickhouse-server -R
sed -i 's#/var/log/clickhouse-server#/data/logs/clickhouse-server#g' /etc/init.d/clickhouse-server
sed -i 's#/opt/clickhouse#/data/db/ck/clickhouse/data_old#g' /etc/init.d/clickhouse-server
sed -i 's#/var/lib/clickhouse#/data/db/ck/clickhouse/data#g' /etc/init.d/clickhouse-server
```
- 配置文件
config.xml
```shell
cat > /etc/clickhouse-server/config.xml <<EOF
<?xml version="1.0