Zookeeper安装

单机部署

下载安装包

1
wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz

解压

1
tar -zxvf zookeeper-3.4.12.tar.gz 

检查Java环境

需要JDK1.6以上的环境

使用java -version检查是否安装JDK

1
java -version

输出结果

1
2
3
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)

配置环境变量

1
2
3
[root@iZbp19q9mzfovzwh6hk6rpZ zookeeper-3.4.12]# pwd
/usr/local/soft/zookeeper/zookeeper-3.4.12
[root@iZbp19q9mzfovzwh6hk6rpZ zookeeper-3.4.12]# vim /etc/profile

添加环境变量

1
2
3
ZK_HOME=/usr/local/soft/zookeeper/zookeeper-3.4.12
PATH=$PATH:$ZK_HOME/bin
export PATH ZK_HOME

使配置马上生效

1
[root@iZbp19q9mzfovzwh6hk6rpZ zookeeper-3.4.12]# source /etc/profile

配置zk

1
2
3
4
5
6
7
8
9
[root@iZbp19q9mzfovzwh6hk6rpZ zookeeper-3.4.12]# mkdir data
[root@iZbp19q9mzfovzwh6hk6rpZ zookeeper-3.4.12]# mkdir logs
[root@iZbp19q9mzfovzwh6hk6rpZ zookeeper-3.4.12]# cd conf/
[root@iZbp19q9mzfovzwh6hk6rpZ conf]# ll
total 12
-rw-rw-r-- 1 1000 1000 535 Mar 27 12:32 configuration.xsl
-rw-rw-r-- 1 1000 1000 2161 Mar 27 12:32 log4j.properties
-rw-rw-r-- 1 1000 1000 922 Mar 27 12:32 zoo_sample.cfg
[root@iZbp19q9mzfovzwh6hk6rpZ conf]# cp zoo_sample.cfg zoo.cfg

修改zoo.cfg中的配置,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=20
# 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/soft/zookeeper/zookeeper-3.4.12/data
dataLogDir=/usr/local/soft/zookeeper/zookeeper-3.4.12/logs
# 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=127.0.0.1:2888:3888

其中:
2888端口号是zookeeper服务之间通信的端口
3888是zookeeper与其他应用程序通信的端口

启动与停止

切换到zookeeper的bin目录下

启动
1
./zkServer.sh start
查看进程
1
jps 

其中QuorumPeerMain是zookeeper进程,启动正常

查看状态
1
./zkServer.sh status
停止
1
./zkServer.sh stop

设置开机启动

切换到/etc/rc.d/init.d/目录下

1
cd /etc/rc.d/init.d

创建zookeeper文件

1
touch zookeeper 

更新权限

1
chmod +x zookeeper 

编辑文件,写入一下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
#chkconfig:2345 20 90
#description:zookeeper
#processname:zookeeper
export JAVA_HOME=/usr/local/soft/jdk/jdk1.8.0_171
export PATH=$JAVA_HOME/bin:$PATH
ZK_HOME=/usr/local/soft/zookeeper/zookeeper-3.4.12
case $1 in
start)su root $ZK_HOME/bin/zkServer.sh start;;
stop)su root $ZK_HOME/bin/zkServer.sh stop;;
status)su root $ZK_HOME/bin/zkServer.sh status;;
restart)su root $ZK_HOME/bin/zkServer.sh restart;;
*) echo "require start|stop|status|restart" ;;
esac

这样就可以用service zookeeper start/stop来启动停止zookeeper服务了

使用命令把zookeeper添加到开机启动里面

1
2
chkconfig zookeeper on  
chkconfig --add zookeeper

然后通过chkconfig –list查看是否已成功添加到开机启动项

1
zookeeper      	0:off	1:off	2:on	3:on	4:on	5:on	6:off