1、创建 /usr/local/services/zookeeper 文件夹:
mkdir -p /usr/local/services/zookeeper
2、进入到 /usr/local/services/zookeeper 目录中:
cd /usr/local/services/zookeeper
3、下载 zookeeper-3.4.14.tar.gz:
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
4、解压缩 zookeeper-3.4.14.tar.gz:
tar -zxvf zookeeper-3.4.14.tar.gz
5、添加环境变量:
vim /etc/profile
添加如下内容:
export ZOOKEEPER_HOME=/usr/local/zookeeper/zookeeper-3.4.14
export PATH=.:$PATH:$ZOOKEEPER_HOME/bin
配置文件生效:
source /etc/profile
6、修改ZooKeeper配置文件:
a.因为这里是在单服务器上面搭建zookeeper伪集群,所以需要分别在zookeeper路径(/usr/local/zookeeper)新建三个目录: server1 、server2 、server2。用于存储每个zookeeper节点的配置文件和数据
[root@localhost local]# cd /usr/local/zookeeper/
[root@localhost zookeeper]# mkdir server1 server2 server3
目录结构:
b.然后在每个server目录里面分别新建目录:data、dataLog
[root@localhost zookeeper]# cd /usr/local/zookeeper/server1/
[root@localhost server1]# mkdir data dataLog
[root@localhost zookeeper]# cd /usr/local/zookeeper/server2/
[root@localhost server2]# mkdir data dataLog
[root@localhost zookeeper]# cd /usr/local/zookeeper/server3/
[root@localhost server3]# mkdir data dataLog
c.在每个节点的data目录里面分别写节点序号
[root@localhost zookeeper]# echo '1' > /usr/local/zookeeper/server1/data/myid
[root@localhost zookeeper]# echo '2' > /usr/local/zookeeper/server2/data/myid
[root@localhost zookeeper]# echo '3' > /usr/local/zookeeper/server3/data/myid
d.每个节点分别编辑配置文件zoo.cfg
[root@localhost server1]# vim /usr/local/zookeeper/server1/zoo.cfg
其内容如下:
#server1
tickTime=2000
initLimit=5
syncLimit=2
dataDir=/usr/local/zookeeper/server1/data
dataLogDir=/usr/local/zookeeper/server1/dataLog
clientPort=2100
server.1=localhost:2200:2300
server.2=localhost:2201:2301
server.3=localhost:2202:2302
[root@localhost server2]# vim /usr/local/zookeeper/server2/zoo.cfg
其内容如下:
#server2
tickTime=2000
initLimit=5
syncLimit=2
dataDir=/usr/local/zookeeper/server2/data
dataLogDir=/usr/local/zookeeper/server2/dataLog
clientPort=2101
server.1=localhost:2200:2300
server.2=localhost:2201:2301
server.3=localhost:2202:2302
[root@localhost server3]# vim /usr/local/zookeeper/server3/zoo.cfg
其内容如下:
#server3
tickTime=2000
initLimit=5
syncLimit=2
dataDir=/usr/local/zookeeper/server3/data
dataLogDir=/usr/local/zookeeper/server3/dataLog
clientPort=2102
server.1=localhost:2200:2300
server.2=localhost:2201:2301
server.3=localhost:2202:2302
注意事项:
每个节点的dataDir和dataLogDir都需要配置成不一样,不然会提示此zookeeper节点已启动
每个节点的 clientPort 也需要配置成不一样,这是客户端连接端口,每个zookeeper节点不能冲突
端口2200和端口2300分别为各个节点的连接端口和选举端口,如果是在单个服务器上面搭建伪集群,则需要配置成不同的端口,不然启动时会爆端口已被占用的错误
7、启动:
[root@localhost zookeeper]# cd /usr/local/zookeeper/zookeeper-3.4.14/bin
分别启动三个节点:
[root@localhost bin]# ./zkServer.sh start /usr/local/zookeeper/server1/zoo.cfg
[root@localhost bin]# ./zkServer.sh start /usr/local/zookeeper/server2/zoo.cfg
[root@localhost bin]# ./zkServer.sh start /usr/local/zookeeper/server3/zoo.cfg
如果出现这种情况:
a.检查zoo.cfg,myid 文件是否有问题
b.检查端口冲突
c.关闭防火墙试试
d.切换root权限再试试
8、检查集群状态:
[root@localhost bin]# ./zkServer.sh status /usr/local/zookeeper/server1/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/server1/zoo.cfg
Mode: follower
[root@localhost bin]# ./zkServer.sh status /usr/local/zookeeper/server2/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/server2/zoo.cfg
Mode: follower
[root@localhost bin]# ./zkServer.sh status /usr/local/zookeeper/server3/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/server3/zoo.cfg
Mode: leader
最后,配置成功。