ZooKeeper分布式安装与配置
1. 下载zookeeper二进制安装包
下载地址:http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.9/
2.解压zookeeper安装包
把zookeeper-3.4.3.tar.gz文件存放在/home/hadoop目录下,进行解压:
hadoop@ubuntu:~$ sudo tar -zxvf zookeeper-3.4.3.tar.gz
hadoop@ubuntu:~$ chown -R hadoop:hadoop zookeeper-3.4.3
3.设置环境变量
$ vi /etc/profile
添加环境变量
export ZK_HOME=/opt/zookeeper
export PATH=$PATH:$ZK_HOME/bin
source /etc/profile #使配置生效
4.配置
配置文件存放在$ZOOKEEPER_HOME/conf/目录下,将zoo_sample.cfg文件名称改为zoo.cfg, 缺省的配置内容如下: 下面这篇 需要注意的是不能有注释 不然 data后面 新建文件夹就会带上注释 导致zookeeper服务起不来 (底部我会介绍排错过程)
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# 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=/home/storm/zookeeper/data
dataLogDir=/home/storm/zookeeper/log
# 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
server.1=h1:2888:3888
server.2=h2:2888:3888
server.3=h3:2888:3888
dataDir=/home/hadoop/zookeeper-3.4.3/data,后面不能有空格其次是,后面不能跟注释*,不然创建的文件夹名字是 data#注释。由于我配置的时候没有删掉后面的空格,zookeeper在启动的时候回自动创建一个dataDir的文件夹,里面有系统自动生成的pid,与原来的myid冲突,启动会失败。
配置说明:
tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
5.启动zookeeper
当这些配置项配置好后,你现在就可以启动zookeeper了,如果配置了ZK_HOME变量,直接运行即可,不需要进入bin目录下:
hadoop@ubuntu:~/zookeeper-3.4.3/bin$ zkServer.sh start #启动
hadoop@ubuntu:~/zookeeper-3.4.3/bin$ jps #查看启动的服务名称
hadoop@ubuntu:~/zookeeper-3.4.3/bin$ zkServer.sh status #查看运行状态
hadoop@ubuntu:~/zookeeper-3.4.3/bin$ zkServer.sh stop #关闭
zkServer.sh start-foreground 查看日志
启动后要检查 Zookeeper 是否已经在服务,可以通过 netstat -at|grep 2181 命令查看是否有 clientPort 端口号在监听服务。
这里补充下ZooKeeper如果服务起不来的排查 http://www.myexception.cn/open-source/1906016.html