1:首先将http://zookeeper.apache.org/
下载好的zookeeper-3.4.5.tar.gz上传到三台虚拟机上,之前博客搭建好的(安装Zookeeper之前记得安装好你的jdk哦)。
2:然后对zookeeper-3.4.5.tar.gz进行解压缩操作:
[hadoop@master ~]$ tar -zxvf zookeeper-3.4.5.tar.gz
3:然后进入cond目录(将zoo_sample.cfg修改为zoo.cfg):
mv修改名称或者cp一个,老的作为样本:
4:然后打开zoo.cfg文件:
修改一些配置:
tickTime=2000 心跳间隔
initLimit=10 初始容忍的心跳数
syncLimit=5 等待最大容忍的心跳数
dataDir=/tmp/zookeeper 本地保存数据的目录,tmp存放的临时数据,可以修改为自己的目录;
clientPort=2181 客户端默认端口号
如果有需要,我感觉加上日志很有必要,如果出错了,还有地方可以去查找:
dataLogDir=/home/hadoop/zookeeper/log
修改后的如下所示(红色圈起来的是修改的):
server.1=master:2888:3888 (主机名, 心跳端口、数据端口)
server.2=slave1:2888:3888
server.3=slave2:2888:3888
5:由于需要事先创建好data目录,所以现在创建data目录:
然后在data目录创建一个文件myid,里面写一个1,如下所示:
5:然后将修改好的复制到slaver1和slaver2上面
然后分别将slaver1和slaver2的myid修改为2和3,如下所示:
6:至此Zookeeper搭建结束,下面开始启动Zookeeper,分别启动:
如果你不想切换到Zookeeper目录启动,可以配置环境变量:
vi /etc/profile(修改文件)
export ZOOKEEPER_HOME=/home/hadoop/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin
重新编译文件:
source /etc/profile
注意:3台zookeeper都需要修改
少于三台不会正常工作的,可以通过命令查询状态:
7:接下来启动slaver1和slaver2的服务:
三台机器启动完成以后,可以查看其状态,开始报这个错,就是启动不了Zookeeper,然后百度呗,百度很多方法,还存在版本问题,新生事物永远在争议中成长,name百度的也没帮助我解决,最后重启三台机器,问题解决:
1 JMX enabled by default 2 Using config: /home/hadoop/zookeeper-3.4.5/bin/../conf/zoo.cfg 3 Error contacting service. It is probably not running.
上面的是master节点的,下面的是slaver1节点和slaver2节点的(对于出现这个启动不了的问题,我是这样想的,如果百度的方法不行,就重启一下,
重启以后我开始启动master节点的,然后查看状态,肯定没启动起来,正常,查看一下进程jps,
然后启动slaver1节点的,然后查看状态,肯定没启动起来,正常,查看一下进程jps,
然后启动slaver2节点的,然后查看状态,肯定启动起来,正常,查看一下进程jps,正常,
如果没启动起来,估计问题不好弄了都,我的就解决到这里吧!):
8:至此,Zookerper集群就启动起来了,然后就可以通过java的api往里面写数据,注入分布式应用让Zookerper协调的数据,或者使用命令行的客户端zkCli.sh模式,zkCli.sh连到集群上面去访问数据,可以用来做测试(不带参数连接到本节点上面去):
Connecting to localhost:2181
2017-10-11 02:39:39,035 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
2017-10-11 02:39:39,040 [myid:] - INFO [main:Environment@100] - Client environment:host.name=slaver2
2017-10-11 02:39:39,040 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.7.0_65
2017-10-11 02:39:39,040 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-10-11 02:39:39,040 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/home/hadoop/jdk1.7.0_65/jre
2017-10-11 02:39:39,040 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/home/hadoop/zookeeper-3.4.5/bin/../build/classes:/home/hadoop/zookeeper-3.4.5/bin/../build/lib/*.jar:/home/hadoop/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/hadoop/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/home/hadoop/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/home/hadoop/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/home/hadoop/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/home/hadoop/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/home/hadoop/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/home/hadoop/zookeeper-3.4.5/bin/../conf:
2017-10-11 02:39:39,041 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/i386:/lib:/usr/lib
2017-10-11 02:39:39,041 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-10-11 02:39:39,041 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2017-10-11 02:39:39,044 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2017-10-11 02:39:39,044 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=i386
2017-10-11 02:39:39,044 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.32-358.el6.i686
2017-10-11 02:39:39,044 [myid:] - INFO [main:Environment@100] - Client environment:user.name=hadoop
2017-10-11 02:39:39,045 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/home/hadoop
2017-10-11 02:39:39,046 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/hadoop/zookeeper-3.4.5/bin
2017-10-11 02:39:39,047 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@1a685ae
Welcome to ZooKeeper!
2017-10-11 02:39:39,096 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@966] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2017-10-11 02:39:39,111 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@849] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session
2017-10-11 02:39:39,241 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1207] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x35f0ac132690000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
连接到本节点的2181端口之后向集群里面写任何数据,在另外两个节点都可以看到(Zookerper管理客户所存放的数据采用的是类似于文件树的结构):
在其他节点也可以看到的:
9:ssh远程开启Zookeeper服务:
[root@master bin]# ssh slaver2 "source /etc/profile;/home/hadoop/soft/zookeeper-3.4.5/bin/zkServer.sh start"
10:Zookeeper启动自动化脚本:
#export a=1 定义的变量,会对自己所在的sheel进程及其子进程生效。 #a=1 定义的变量,只对自己所在的sheel进行生效。 #在script.sh中定义的变量,在当前登陆的sheel进行中,source script.sh时,脚本中定义的变量也会进入当前登陆的进程 #!/bin/bash echo "start zkServer..." for i in 1 2 3 do ssh slaver$i "source /etc/profile;/home/hadoop/soft/zookeeper-3.4.5/bin/zkServer.sh start" done
个人配置自动化脚本执行的操作步骤:
首先在我个人喜欢的目录下面/home/hadoop目录下面创建一个叫做脚本的文件夹,script,然后将自动化脚本拷贝到新建的startzk.sh文件中,然后配置环境变量:
export JAVA_HOME=/home/hadoop/soft/jdk1.7.0_65 export ZOOKEEPER_HOME=/home/hadoop/soft/zookeeper-3.4.5 #自动化脚本的目录 export SCRIPT=/home/hadoop/script #添加到全局环境变量中即可。 export PATH=$PATH:$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin:$SCRIPT
然后在任意目录,执行startzk.sh就可以启动三台Zookeeper了。
待续.....