一、下载zookeeper
在apache的官网下载zookeeper,http://mirrors.cnnic.cn/apache/zookeeper/
当前stable的版本是3.4.6,安装文件zookeeper-3.4.6.tar.gz
二、安装zookeeper
如果安装文件没有执行权限,则执行
chmod +x zookeeper-3.4.6.tar.gz
执行以下命令:
tar -xzvf zookeeper-3.4.6.tar.gz
执行完,会有目录zookeeper-3.4.6
创建zookeeper的data目录,目录看个人而定的,我是创建跟zookeeper-3.4.6同级目录的,
mkdir zkdata
ls
zkdata zookeeper-3.4.6
接着修改zookeeper的配置文件conf/zoo.cfg
cd zookeeper-3.4.6/conf/; ls
会看到有文件zoo_sample.cfg,复制份zoo.cfg文件出来
cp zoo_sample.cfg zoo.cfg
修改zoo.cfg
vim zoo.cfg
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#dataDir=/tmp/zookeeper
dataDir=/root/zookeeper-a/zkdata
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
其中,
dataDir就指向zookeeper存放数据的目录,就是刚才创建的目录zkdata
clientPort=2181,是设定zookeeper的端口
启动zookeeper
cd ../bin/; ls
README.txt zkCli.cmd zkEnv.cmd zkServer.cmd zookeeper.out
zkCleanup.sh zkCli.sh zkEnv.sh zkServer.sh
其中,
zkServer.sh,启动zookeeper
zkCli.sh,客户端连接zookeeper的程序,后面再讲,
./zkServer.sh start
这样zookeeper就启动了。
如果想停止zookeeper
./zkServer.sh stop
zkServer.sh有以下用法
Usage: ./zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
如果想连接上zookeeper,查看,操作zookeeper,可执行以下命令:
./zkCli.sh -server 127.0.0.1:2181
以上是只有一个zookeeper跑的安装情况,如果有几个zookeeper跑,即分布式部署,下面讲。
假设是安装3个zookeeper,则分别在zookeeper的conf/zoo.cfg后面加上内容:
server.1=192.168.89.100:2888:3888
server.2=192.168.89.101:2888:3888
server.3=192.168.89.102:2888:3888
其中,
server.1,server.2,server.3,中的数字1,2,3分别是100,101,102节点的zookeeper的序号,
在目录zkdata下创建文件myid,并分别设置内容为1,2,3
#100节点
[root@localhost zkdata]# more myid
1
#101节点
[root@localhost zkdata]# more myid
2
#102节点
[root@localhost zkdata]# more myid
3
则分别启动100,101,102 节点就可以了。