1.在主机node1,node2,node3上搭建zookeeper集群
2.解压zookeeper包
tar -zxvf zookeeper-3.4.6.tar.gz
3.创建配置文件zoo.cfg
vi /usr/zookeeper-3.4.6/conf/zoo.cfg
tickTime=2000
dataDir=/opt/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
server.1=node1:2888:3888 修改为对应的主机名
server.2=node2:2888:3888
server.3=node3:2888:3888
4.在node1,2,3的dataDir路径下分别创建myid文件
[root@node1 conf]# mkdir /opt/zookeeper
[root@node1 conf]# vi /opt/zookeeper/myid
将服务号写入myid中,比如node1机器中写入1;node2机器中写入2;与zoo.cfg配置相同
5.拷贝配置好的zookeeper到node2和node3中
scp -r zookeeper-3.4.6 root@node2:/usr/
6.配置zookeeper的环境变量
vi ~/.bash_profile
export PATH
export HADOOP_HOME=/usr/hadoop-2.5.1
export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:/usr/zookeeper-3.4.6/bin 在hadoop配置后添加,另配也可以
scp ~/.bash_profile root@node3:~/ 拷贝到node2和node3中
source ~/.bash_profile 加载配置
7.node1,2,3启动zookeeper
zkServer.sh start
当前目录下生成日志输出zookeeper.out
可能会报节点连接异常,这不重要,因为一个节点启动的时候,会尝试连接其余节点,但是这时其余节点还没有启动
8.验证1
执行zkCli.sh 进入zookeeper客户端,集群是一个整体所以可以再node2的客户端登录到到node1
[root@node2 ~]# zkCli.sh -server node1:2181
Connecting to node1:2181
2017-04-07 02:47:57,538 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2017-04-07 02:47:57,545 [myid:] - INFO [main:Environment@100] - Client environment:host.name=node2
2017-04-07 02:47:57,549 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.7.0_79
2017-04-07 02:47:57,553 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-04-07 02:47:57,553 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/java/jdk1.7.0_79/jre
2017-04-07 02:47:57,554 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/zookeeper-3.4.6/bin/../build/classes:/usr/zookeeper-3.4.6/bin/../build/lib/*.jar:/usr/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/usr/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/usr/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/usr/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/usr/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/usr/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/usr/zookeeper-3.4.6/bin/../conf:.:/usr/java/jdk1.7.0_79/lib/dt.jar:/usr/java/jdk1.7.0_79/lib/tools.jar
2017-04-07 02:47:57,554 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2017-04-07 02:47:57,555 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-04-07 02:47:57,555 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2017-04-07 02:47:57,555 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2017-04-07 02:47:57,556 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2017-04-07 02:47:57,557 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.32-431.el6.x86_64
2017-04-07 02:47:57,558 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2017-04-07 02:47:57,558 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2017-04-07 02:47:57,558 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/root
2017-04-07 02:47:57,561 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=node1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@383bdcbc
Welcome to ZooKeeper!
2017-04-07 02:47:57,624 [myid:] - INFO [main-SendThread(node1:2181):ClientCnxn$SendThread@975] - Opening socket connection to server node1/192.168.108.11:2181. Will not attempt to authenticate using SASL (unknown error)
2017-04-07 02:47:57,634 [myid:] - INFO [main-SendThread(node1:2181):ClientCnxn$SendThread@852] - Socket connection established to node1/192.168.108.11:2181, initiating session
JLine support is enabled
2017-04-07 02:47:57,697 [myid:] - INFO [main-SendThread(node1:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server node1/192.168.108.11:2181, sessionid = 0x15b448321200001, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: node1:2181(CONNECTED) 0] quit
Quitting...
2017-04-07 02:48:06,809 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@512] - EventThread shut down
2017-04-07 02:48:06,810 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x15b448321200001 closed
quit退出
9.验证2可以通过zookeeper的脚本来查看启动状态,包括集群中各个结点的角色(或是leader,或是follower),如下所示,是在zookeeper集群中的每个结点上查询的结果:
hadoop@slave-01:~/installation/zookeeper-3.3.4$ bin/zkserver.sh status
jmx enabled by default
using config: /home/hadoop/installation/zookeeper-3.3.4/bin/../conf/zoo.cfg
mode: follower
hadoop@slave-02:~/installation/zookeeper-3.3.4$ bin/zkserver.sh status
jmx enabled by default
using config: /home/hadoop/installation/zookeeper-3.3.4/bin/../conf/zoo.cfg
mode: leader
hadoop@slave-03:~/installation/zookeeper-3.3.4$ bin/zkserver.sh status
jmx enabled by default
using config: /home/hadoop/installation/zookeeper-3.3.4/bin/../conf/zoo.cfg
mode: follower
通过上面状态查询结果可见,node2是集群的leader,其余的两个结点是follower。