。
Zookeeper为了防止出现二阶段和三阶段提交协议中出现的数据不一致的情况(例如:网络出现问题,可能会将zk集群分成多个小集群),规定了只有zk集群中半数以上的server存活时才能对外提供服务。所以我们一般安装zk集群时,一般设置为奇数台zk的server。
假如,我们的zk集群有6个server,那么我们最多允许有两个server挂掉zk集群还能继续对外提供服务。但是如果我们的zk集群有5个server的话,我们同样最多允许有两个server挂掉也能继续提供服务。所以一般zk集群安装时server的数量是奇数个。
下载地址:https://mirrors.cnnic.cn/apache/zookeeper/
zookeeper要求java运行环境:详情如
https://blog.csdn.net/meiLin_Ya/article/details/80650945
解压:
tar zxvf zookeeper-3.4.11.tar.gz -C /usr/local/
修改配置文件
1.将配置文件zoo_sample改名为cfg zoo.cfg
cp zoo_sample.cfg zoo.cfg
# 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=/usr/local/zookeeper-3.4.7/data
# 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
dataLogDir=/usr/local/zookeeper-3.4.7/log
#autopurge.purgeInterval=1
server.11=hu-hadoop1:2888:3888
server.12=hu-hadoop2:2888:3888
server.13=hu-hadoop3:2888:3888
tickTime:时长单位为毫秒,为zk使用的基本时间度量单位。例如,1
tickTime是客户端与zk服务端的心跳时间,2
tickTime是客户端会话的超时时间。tickTime的默认值为2000毫秒,更低的tickTime值可以更快地发现超时问题,但也会导致更高的网络流量(心跳消息)和更高的CPU使用率(会话的跟踪处理)。
clientPort:zk服务进程监听的TCP端口,默认情况下,服务端会监听2181端口。
dataDir:无默认配置,必须配置,用于配置存储快照文件的目录。如果没有配置dataLogDir,那么事务日志也会存储在此目录。
创建相应得目录,存放日志文件
dataDir=/usr/local/zookeeper-3.4.11/data
dataLogDir=/usr/local/zookeeper-3.4.11/log
记得创建那两个文件夹。。。。完成后将在data下创建一个文本 必须叫myid 里面填写的是你的zookeeper中的主机唯一标识。
而你的zookeeper在zoo.cfg中配置。
配置环境:
vi /ext/profile
export JAVA_HOME=/home/bigdata/jdk1.7.0_79
export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.7
export HADOOP_HOME=/home/bigdata/hadoop-2.6.0
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:
export PATH=$PATH:$JAVA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$ZOOKEEPER_HOME/bin:
if [ "x${ZOO_LOG_DIR}" = "x" ]
then
ZOO_LOG_DIR="$ZOOBINDIR/../log"
fi
if [ "x${ZOO_LOG4J_PROP}" = "x" ]
then
ZOO_LOG4J_PROP="INFO,ROLLINGFILE"
fi
zookeeper.root.logger=INFO, ROLLINGFILE
log4j.appender.ROLLINGFILE=org.apache.log4j.DailyRollingFileAppender
改为—每天一个log日志文件,而不是在同一个log文件中递增日志
日志输出为:
[root@hu-hadoop1 conf]# ls
configuration.xsl log4j.properties zoo.cfg
[root@hu-hadoop1 conf]# ll
total 12
-rw-rw-r--. 1 1000 1000 535 Nov 11 2015 configuration.xsl
-rw-rw-r--. 1 1000 1000 2326 Jun 11 15:08 log4j.properties
-rw-r--r--. 1 root root 1073 Jun 11 13:15 zoo.cfg
[root@hu-hadoop1 conf]#
完成后记得把数据同步到其他主机。。。。。。。
同步后记得要修改/etc/local/zookeeper/data/myid 里面是主机唯一标识 。如我上面的11,12,13
启动第一台:
zkServer.sh start
[root@hu-hadoop3 conf]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.7/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
查看状态:
zkServer.sh status
[root@hu-hadoop3 conf]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.7/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
这样是正确的。因为zookeeper最少安装2个以上有详细解释。再启动一台
启动后查看状态:
[root@hu-hadoop2 data]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.7/bin/../conf/zoo.cfg
Mode: follower
然后再返回了看上一个状态:
[root@hu-hadoop3 conf]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.7/bin/../conf/zoo.cfg
Mode: leader
然后再启动另一条:我这里配了3台
[root@hu-hadoop1 conf]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.7/bin/../conf/zoo.cfg
Mode: follower
这里的leader是随机的不是固定的。
肯用到的命令:
停止一个zookeeper:
zkService.sh stop
zkCli.sh:
1)查看当前节点列表
[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper]
2)创建节点
[zk: localhost:2181(CONNECTED) 2] create /test "test"
Created /test
[zk: localhost:2181(CONNECTED) 3] ls /
[zookeeper, test]
3)查看节点数据
[zk: localhost:2181(CONNECTED) 4] get /test
"test"
cZxid = 0x300000007
ctime = Thu Sep 24 05:54:51 PDT 2015
mZxid = 0x300000007
mtime = Thu Sep 24 05:54:51 PDT 2015
pZxid = 0x300000007
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
4)设置节点数据
[zk: localhost:2181(CONNECTED) 7] set /test "111111"
cZxid = 0x300000007
ctime = Thu Sep 24 05:54:51 PDT 2015
mZxid = 0x300000008
mtime = Thu Sep 24 05:57:40 PDT 2015
pZxid = 0x300000007
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
[zk: localhost:2181(CONNECTED) 8] get /test
"111111"
cZxid = 0x300000007
ctime = Thu Sep 24 05:54:51 PDT 2015
mZxid = 0x300000008
mtime = Thu Sep 24 05:57:40 PDT 2015
pZxid = 0x300000007
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
5)删除节点
[zk: localhost:2181(CONNECTED) 9] delete /test
[zk: localhost:2181(CONNECTED) 10] ls /
[zookeeper]