官网:https://zookeeper.apache.org/
下载地址:https://www.apache.org/dyn/closer.cgi/zookeeper/
一、单机:
1. 下载或上传安装包:
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.5/apache-zookeeper-3.5.5-bin.tar.gz
2. 解压:
tar xzvf /srv/ftp/apache-zookeeper-3.5.5-bin.tar.gz -C /usr/local/
3. 重命名:
mv /usr/local/apache-zookeeper-3.5.5-bin/ /usr/local/zookeeper
4. 进行配置文件模板的拷贝,后缀一定要注意是“.cfg”:
cp /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg
5. 创建zookeeper的保存目录,默认在“/tmp/zookeeper”下,但是temp目录会自动清空:
kdir -p /usr/data/zookeeper
6. 修改“zoo.cfg”配置文件:
dataDir=/usr/data/zookeeper //存储目录 clientPort=2181 //服务端口
7. 启动Zookeeper:
/usr/local/zookeeper/bin/zkServer.sh start
8. 检验是否启动:
jps //QuorumPeerMain /usr/local/zookeeper/bin/zkServer.sh status //显示文件路径、主机、运行状态
9. 连接客户端:
/usr/local/zookeeper/bin/zkCli.sh -server ip
二、集群:
1. 为了方便连接处理,配置SSH,是主机a能登录到b和c上:
rm -r ~/.ssh //删除ssh ssh-keygen -t rsa //生成ssh cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys //将ssh添加到本机 ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.28.220 //将ssh发送给b主机 ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.28.221 //将ssh发送给c主机
2. 修改zoo.cfg配置文件:
vim /usr/local/zookeeper/conf/zoo.cfg
添加主机列表:连接断开:leader端口
server.1=192.168.28.219:2888:3888 server.2=192.168.28.220:2888:3888 server.3=192.168.28.221:2888:3888
3. 将当前主机的zoo.cfg配置文件发送到其他主机:
scp /usr/local/zookeeper/conf/zoo.cfg 192.168.28.220:/usr/local/zookeeper/conf/
scp /usr/local/zookeeper/conf/zoo.cfg 192.168.28.221:/usr/local/zookeeper/conf/
4. 此时每一台主机上给出的zoo.cfg文件里面针对于服务主机都提供有一个编号的内容,但是这个编号的内容对应的是ZooKeeper数据目录下myid的内容,但是此时并没有这个myid的文件,所有在不同的主机上,配置上各自的mid,而对应的就是各自的配置:
echo 1 >> /usr/data/zookeeper/myid //主机A echo 2 >> /usr/data/zookeeper/myid //主机B echo 3 >> /usr/data/zookeeper/myid //主机C
5. 启动主机:
/usr/local/zookeeper/bin/zkServer.sh start
6. 查看状态:
/usr/local/zookeeper/bin/zkServer.sh status
会显示:Mode: follower、Mode: leader