Zookeeper-3.4.9
下載下傳Zookeeper-3.4.9
在/usr/local下建立hadoop檔案夾
将下載下傳的檔案遷移到該檔案夾下,并解壓
tar -zxvf zookeeper-3.4.9.tar.gz
進入conf配置檔案夾
cd zookeeper-3.4.9/conf/
複制zoo_sample.cfg檔案并命名為zoo.cfg
cp zoo_sample.cfg zoo.cfg
打開zoo.cfg,修改内容
#dataDir=/tmp/zookeeper
dataDir=/usr/local/hadoop/zookeeper-3.4.9/data
dataLogDir=/usr/local/hadoop/zookeeper-3.4.9/logs
修改後的完整内容:

# 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=/tmp/zookeeper
dataDir=/usr/local/hadoop/zookeeper-3.4.9/data # 資料持久化路徑
dataLogDir=/usr/local/hadoop/zookeeper-3.4.9/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
View Code
修改zookeeper的環境變量,打開/etc/profile檔案,并在末尾添加以下内容:
# idea - zookeeper-3.4.9 config start
export ZOOKEEPER_HOME=/usr/local/hadoop/zookeeper-3.4.9/
export PATH=$ZOOKEEPER_HOME/bin:$PATH
export PATH
# idea - zookeeper-3.4.9 config start
添加後的完整内容:

# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
# ksh workaround
EUID=`/usr/bin/id -u`
UID=`/usr/bin/id -ru`
fi
USER="`/usr/bin/id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022
fi
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
unset i
unset -f pathmunge
export JAVA_HOME=/usr/local/jdk/jdk1.8.0_121
export JRE_HOME=/usr/local/jdk/jdk1.8.0_121/jre
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
# idea - zookeeper-3.4.9 config start
export ZOOKEEPER_HOME=/usr/local/hadoop/zookeeper-3.4.9/
export PATH=$ZOOKEEPER_HOME/bin:$PATH
export PATH
# idea - zookeeper-3.4.9 config start
使環境變量生效
source /etc/profile
啟動Zookeeper服務:
[root@localhost ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/hadoop/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
查詢Zookeeper狀态:
[root@localhost ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/hadoop/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: standalone
停止Zookeeper服務:
[root@localhost ~]# zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /usr/local/hadoop/zookeeper-3.4.9/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
重新開機Zookeeper服務:
[root@localhost ~]# zkServer.sh restart
ZooKeeper JMX enabled by default
Using config: /usr/local/hadoop/zookeeper-3.4.9/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/hadoop/zookeeper-3.4.9/bin/../conf/zoo.cfg
Stopping zookeeper ... no zookeeper to stop (could not find file /usr/local/hadoop/zookeeper-3.4.9/data # 資料持久化路徑/zookeeper_server.pid)
ZooKeeper JMX enabled by default
Using config: /usr/local/hadoop/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
設定為開機自啟動:(都是網上找的,沒有操作!)
方法1:
/etc/rc.d/rc.local
添加:
su -root -c '//usr/local/services/zookeeper/zookeeper-3.4.9/bin/zkServer.sh start'
方法2:
在/etc/rc.d/init.d檔案夾下建立zookeeper檔案
賦予操作權限
[root@localhost init.d]# touch zookeeper
[root@localhost init.d]# chmod +x zookeeper
[root@localhost init.d]# ls
functions netconsole network README redis_6379 zookeeper
[root@localhost init.d]# ls -a
. .. functions netconsole network README redis_6379 zookeeper
[root@localhost init.d]# ls -all
總用量 40
drwxr-xr-x. 2 root root 99 4月 10 21:24 .
drwxr-xr-x. 10 root root 4096 4月 9 23:08 ..
-rw-r--r--. 1 root root 15131 9月 12 2016 functions
-rwxr-xr-x. 1 root root 2989 9月 12 2016 netconsole
-rwxr-xr-x. 1 root root 6643 9月 12 2016 network
-rw-r--r--. 1 root root 1160 3月 3 11:23 README
-rwxr-xr-x. 1 root root 1702 4月 8 22:52 redis_6379
-rwxr-xr-x. 1 root root 0 4月 10 21:24 zookeeper
[root@localhost init.d]# chkconfig -add zookeeper
-add: 未知的選項
[root@localhost init.d]# chkconfig --add zookeeper
zookeeper檔案内容:
#!/bin/bash
#chkconfig:2345 20 90
#description:zookeeper
#processname:zookeeper
case $1 in
start) su root /usr/local/hadoop/zookeeper-3.4.9/bin/zkServer.sh start;;
stop) su root /usr/local/hadoop/zookeeper-3.4.9/bin/zkServer.sh stop;;
status) su root /usr/local/hadoop/zookeeper-3.4.9/bin/zkServer.sh status;;
restart) su root /usr/local/hadoop/zookeeper-3.4.9/bin/zkServer.sh restart;;
*) echo "require start|stop|status|restart" ;;
esac
将zookeeper添加到開機啟動裡面
chkconfig --add zookeeper
開機啟動:
chkconfig zookeeper on
======
Centos7.2安裝zookeeper3.5.2-alpha
為了測試Curator3.2.1,重新安裝zookeeper-3.5.2-alpha.tar.gz
操作與上面一樣,隻是換了檔案夾名稱為zookeeper
[root@localhost conf]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/hadoop/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Error contacting service. It is probably not running.
[root@localhost conf]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/hadoop/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... FAILED TO START
解決:
檢視logs裡面的日志:
[root@localhost zookeeper-3.5.2]# cd logs
[root@localhost logs]# ls
version-2 zookeeper-hongdada-server-localhost.localdomain.out
[root@localhost logs]# vi version-2
[root@localhost logs]# vi zookeeper-hongdada-server-localhost.localdomain.out
logs:
2017-12-18 22:50:31,006 [myid:] - ERROR [main:ZooKeeperServerMain@76] - Unable to start AdminServer, exiting abnormally
org.apache.zookeeper.server.admin.AdminServer$AdminServerException: Problem starting AdminServer on address 0.0.0.0, port 8080 and command URL /commands
at org.apache.zookeeper.server.admin.JettyAdminServer.start(JettyAdminServer.java:100)
at org.apache.zookeeper.server.ZooKeeperServerMain.runFromConfig(ZooKeeperServerMain.java:127)
at org.apache.zookeeper.server.ZooKeeperServerMain.initializeAndRun(ZooKeeperServerMain.java:103)
at org.apache.zookeeper.server.ZooKeeperServerMain.main(ZooKeeperServerMain.java:61)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:125)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:79)
Caused by: java.net.BindException: 位址已在使用
at sun.nio.ch.Net.bind0(Native Method)
很明顯是由于端口占用。我伺服器中的tomcat占用了8080端口,而zookeeper最近的版本中有個内嵌的管理控制台是通過jetty啟動,也會占用8080 端口。
通過檢視zookeeper的官方文檔,發現有3種解決途徑:
(1).删除jetty。
(2)修改端口。
修改方法的方法有兩種,一種是在啟動腳本中增加 -Dzookeeper.admin.serverPort=你的端口号.一種是在zoo.cfg中增加admin.serverPort=沒有被占用的端口号
(3)停用這個服務,在啟動腳本中增加"-Dzookeeper.admin.enableServer=false"
在zoo.cfg中添加配置admin.serverPort=2182
修改後的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=/tmp/zookeeper
dataDir=/usr/local/hadoop/zookeeper-3.4.9/data
dataLogDir=/usr/local/hadoop/zookeeper-3.4.9/logs
# the port at which the clients will connect
clientPort=2181
admin.serverPort=2182
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
"zoo.cfg" 33L, 1045C
啟動:
[root@localhost hadoop]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/hadoop/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... already running as process 6664.
[root@localhost hadoop]# zkServer
bash: zkServer: 未找到指令...
[root@localhost hadoop]# zkServer.sh --version
ZooKeeper JMX enabled by default
Using config: /usr/local/hadoop/zookeeper/bin/../conf/zoo.cfg
Usage: /usr/local/hadoop/zookeeper//bin/zkServer.sh [--config <conf-dir>] {start|start-foreground|stop|restart|status|print-cmd}
[root@localhost hadoop]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/hadoop/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone