参考链接:http://zookeeper.apache.org/

https://www.cnblogs.com/lyhc/p/6560993.html

系统:

乌班图16.04

虚拟机(zk一般要求奇数台节点):

192.168.0.15     ubuntu15

192.168.0.16     ubuntu16

192.168.0.17     ubuntu17

1、standalone安装:

1、下载:

wget http://apache.osuosl.org/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz

2、解压:

tar -xzvf zookeeper-3.4.6.tar.gz -C   /usr/local

3、复制配置文件:

cd conf
cp zoo_sample.cfg zoo.cfg

4、standalone无需配置zoo.cfg,集群注意这里需要配置

5、启动:

sh zkServer.sh start //启动

这里报错:

zkServer.sh: 81: /usr/local/zookeeper-3.4.10/bin/zkEnv.sh: Syntax error: "(" unexpected (expecting "fi")

根据https://blog.csdn.net/u011404265/article/details/72869920提供的解决方案已解决

[email protected]:~# cd /bin/
[email protected]:/bin# ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Dec 23 22:30 /bin/sh -> dash(默认)
[email protected]:/bin# ln -sf bash /bin/sh
[email protected]:/bin# ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Dec 23 22:37 /bin/sh -> bash

查看

root@ubuntu:/usr/local/zookeeper-3.4.10/bin# ps -ef | grep 'zookeeper'
root 1215 1 0 23:39 pts/0 00:00:02 java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /usr/local/zookeeper-3.4.10/bin/../build/classes:/usr/local/zookeeper-3.4.10/bin/../build/lib/*.jar:/usr/local/zookeeper-3.4.10/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper-3.4.10/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper-3.4.10/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper-3.4.10/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper-3.4.10/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper-3.4.10/bin/../zookeeper-3.4.10.jar:/usr/local/zookeeper-3.4.10/bin/../src/java/lib/*.jar:/usr/local/zookeeper-3.4.10/bin/../conf: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
root 1444 1177 0 23:53 pts/0 00:00:00 grep --color=auto zookeeper

使用:

zkClient:

root@ubuntu:/usr/local/zookeeper-3.4.10/bin# ./zkCli.sh -timeout 5000 -server 192.168.0.17:2181

 

2、集群安装(3个节点)

因为leader是选举产生,算法原因一般配置奇数个节点

1、安装:分别在三台机器安装zk,博主直接装了一台然后克隆了三台虚拟机。

2、配置zoo.cfg:安装standalone版的时候复制了 conf/zoo.cfg 文件,但是没有进行配置,如果需要搭建集群,则需要对此文件进行配置!这里随便找一台(ubuntu15)对zoo.cfg进行配置:

vim zoo.cfg

dataDir=/usr/local/zk/data
dataLogDir=/usr/local/zk/logs
server.1=0.0.0.0:2881:3881
server.2=ubuntu16:2882:3882
server.3=ubuntu17:2883:3883
autopurge.snapRetainCount=3

3、根据配置的dataDir 和 dataLogDir 创建目录:

root@ubuntu15:/usr/local/zookeeper-3.4.10/bin# mkdir -p  /usr/local/zk/data
root@ubuntu15:/usr/local/zookeeper-3.4.10/bin# mkdir -p /usr/local/zk/logs

4、myid设置,因为这是集群中的第一台,这里给它id设置为1

root@ubuntu15:/usr/local/zk/data# touch /usr/local/zk/data/myid
root@ubuntu15:/usr/local/zk/data# echo > /usr/local/zk/data/myid

5、这样第一台配置完成,我直接复制第一台的配置文件到其它两台,略作修改后,创建data目录修改节点id

scp -r  [email protected]:/usr/local/zookeeper-3.4.10/conf/zoo.cfg ./

a、zoo.cfg 中本机ip需要设为0.0.0.0

b、第二台myid需要设置为2,第三台设置为3.。。。。

6、依次启动三个节点

ubuntu15

root@ubuntu15:/usr/local/zookeeper-3.4.10/bin# sh zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... >>>>>>>>>>>>>>>>>>>>
STARTED
root@ubuntu15:/usr/local/zookeeper-3.4.10/bin# sh zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
root@ubuntu15:/usr/local/zookeeper-3.4.10/bin# sh zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower

ubuntu16

root@ubuntu16:/usr/local/zookeeper-3.4.10/bin# sh zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... >>>>>>>>>>>>>>>>>>>>
STARTED
root@ubuntu16:/usr/local/zookeeper-3.4.10/bin# sh zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader

ubuntu17

root@ubuntu17:/usr/local/zookeeper-3.4.10/bin# sh zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... >>>>>>>>>>>>>>>>>>>>
STARTED
root@ubuntu17:/usr/local/zookeeper-3.4.10/bin# sh zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
05-23 20:03