目录
单节点模式,跳过步骤4.3。
集群模式,每个节点都需操作以下步骤。
1 准备
1.1 JDK
https://blog.csdn.net/weixin_43724577/article/details/130995274
1.2 关闭防火墙
systemctl disable firewalld
systemctl stop firewalld
2 下载bin包
ZooKeeper官网:https://zookeeper.apache.org/
Apache Archive:https://archive.apache.org/dist/zookeeper/
下载-bin.tar.gz
将安装包上传至centos7。
3 解压到指定目录
tar -zxvf /tmp/apache-zookeeper-3.9.1-bin.tar.gz -C /opt/module/
4 配置修改
4.1 重命名配置文件
mv /opt/module/apache-zookeeper-3.9.1-bin/conf/zoo_sample.cfg /opt/module/apache-zookeeper-3.9.1-bin/conf/zoo.cfg
4.2 修改dataDir路径
vim /opt/module/apache-zookeeper-3.9.1-bin/conf/zoo.cfg
dataDir=/opt/module/apache-zookeeper-3.9.1-bin/zkData
创建文件夹
mkdir /opt/module/apache-zookeeper-3.9.1-bin/zkData
4.3 增加集群配置
我的三台机器分别为
192.168.131.101 centos701
192.168.131.102 centos702
192.168.131.104 centos704
修改zoo.cfg
vim /opt/module/apache-zookeeper-3.9.1-bin/conf/zoo.cfg
#######################cluster#######################
server.1=centos701:2888:3888
server.2=centos702:2888:3888
server.4=centos704:2888:3888
在zkData目录下创建一个myid的文件
vim /opt/module/apache-zookeeper-3.9.1-bin/zkData/myid
在文件中添加与server对应的编号(注意:上下不要有空行,左右不要有空格)
centos701:
1
centos702:
2
centos704:
4
5 启动ZooKeeper
ZooKeeper,启动!
/opt/module/apache-zookeeper-3.9.1-bin/bin/zkServer.sh start
查看状态
/opt/module/apache-zookeeper-3.9.1-bin/bin/zkServer.sh status
[root@centos701 ~]# /opt/module/apache-zookeeper-3.9.1-bin/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/apache-zookeeper-3.9.1-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
[root@centos702 ~]# /opt/module/apache-zookeeper-3.9.1-bin/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/apache-zookeeper-3.9.1-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
[root@centos704 ~]# /opt/module/apache-zookeeper-3.9.1-bin/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/apache-zookeeper-3.9.1-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
6 ZooKeeper集群启停脚本
#!/bin/bash
hosts="centos701 centos702 centos704"
case $1 in
"start"){
for i in $hosts
do
echo ---------- zookeeper $i 启动 ----------
ssh $i "/opt/module/apache-zookeeper-3.9.1-bin/bin/zkServer.sh start"
done
};;
"stop"){
for i in $hosts
do
echo ---------- zookeeper $i 停止 ----------
ssh $i "/opt/module/apache-zookeeper-3.9.1-bin/bin/zkServer.sh stop"
done
};;
"status"){
for i in $hosts
do
echo ---------- zookeeper $i 状态 ----------
ssh $i "/opt/module/apache-zookeeper-3.9.1-bin/bin/zkServer.sh status"
done
};;
esac
7 配置参数解读
zoo.cfg中参数含义解读如下
7.1 tickTime
tickTime = 2000
通信心跳时间,Zookeeper服务器之间、服务器与客户端之间心跳时间,单位毫秒
7.2 initLimit
initLimit = 10
LF初始通信时限
Leader和Follower初始连接时能容忍的最多心跳数(tickTime的数量)
7.3 syncLimit
syncLimit = 5
LF同步通信时限
Leader和Follower之间通信时间如果超过syncLimit * tickTime,Leader认为Follwer死掉,从服务器列表中删除Follwer
7.4 dataDir
保存Zookeeper中的数据。
注意:默认的tmp目录,容易被Linux系统定期删除,所以一般不用默认的tmp目录
7.5 clientPort
clientPort = 2181
客户端连接端口,通常不做修改
7.6 server.A=B:C:D
A是一个数字,表示这个是第几号服务器
集群模式下,配置文件 myid 在 dataDir目录下,这个文件里面数据就是A的值, Zookeeper启动时读取此文件,与zoo.cfg里面的配置信息比较从而判断自己是哪个server。
B是这个服务器的地址;
C是这个服务器Follower与集群中的Leader服务器交换信息的端口;
D是万一集群中的 Leader服务器挂了,需要一个端口来重新进行选举,选出一个新的Leader,而这个端口就是用来执行选举时服务器相互通信的端口。