環境:建議3-5臺服務器java
ip hostname idlinux
10.100.11.13 z1 1 web
10.100.11.14 z2 2shell
10.100.11.17 z3 3apache
綁定 ip hostname到 /etc/hostsbash
1 java環境部署服務器
1.1 下載jdk jdk-8u77-linux-x64.tar.gz http://download.oracle.com/otn-pub/java/jdk/8u77-b03/jdk-8u77-linux-x64.tar.gz oracle
把 jdk-8u77-linux-x64.tar.gz 解壓爲 /usr/local/jdkssh
添加環境變量ide
/etc/profile 文件中添加
## JAVA
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
並把 /usr/local/jdk/bin添加到 PATH中
. /etc/profile重載 profile配置
2 zookeeper安裝
以z1爲例:
把zookeeper-3.4.8.tar.gz (www.apache.org下載)解壓到到 /usr/local/zookeeper
/usr/local/zookeeper/bin添加到PATH環境變量中
2.1 配置zoo.cfg
/usr/local/zookeeper/conf/zoo.cfg
######
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
autopurge.snapRetainCount=20
autopurge.purgeInterval=48
server.1=z1:2888:3888
server.2=z2:2888:3888
server.3=z3:2888:3888
#####
注意server.後面的號要與myid號對應上,範圍0-255
autopurge.snapRetainCount=20: 保留20個文件
autopurge.purgeInterval=48: 保留48小時內的日誌,
snapshot、log文件將保存在dataDir目錄下,配置dataLogDir=/var/lib/zookeeper/logs,log將生成於dataLogDir目錄下
2.2 建立 myid 文件,保存id號。存放在zoo.cfg文件中指定的dataDir目錄下
echo 1 > /var/lib/zookeeper/myid
2.3 zookeeper啓動腳本
先在 /usr/local/zookeeper/bin/zkEnv.sh 在#!/usr/bin/env bash行下添加下面這行
export JAVA_HOME=/usr/local/java/jdk
vi /etc/init.d/zk
##############
#!/bin/sh
#
# Startup script for the ZooKeeper daemon
#
# processname: ZooKeeper
# pidfile: /var/run/ZooKeeper.pid
# config: /usr/local/zookeeper/conf/zoo.cfg
# chkconfig: - 21 79
# description: Start and stop ZooKeeper
# Source function library
. /etc/rc.d/init.d/functions
RETVAL=0
prog="ZooKeeper"
prog_bin=/usr/local/zookeeper/bin/zkServer.sh
start() {
echo -n $"Starting $prog: "
daemon $prog_bin start
#/usr/local/zookeeper/bin/zkServer.sh start
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}
stop() {
echo -n $"Stopping $prog: "
daemon $prog_bin stop
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
}
status() {
$prog_bin status
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit $RETVAL
##############
chmod +x /etc/init.d/zk
2.3.2 CentOS 7 zookeeper啓動腳本
vi /usr/lib/systemd/system/zk.service
##############
[Unit]
Description=Startup script for the ZooKeeper daemon
Documentation=http://zookeeper.apache.org/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/zookeeper/bin/zkServer.sh start
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
##############
systemctl enable zk
systemctl start zk
z2, z3參照z1,注意myid文件內容的 ID號要與配置文件中的 server.x對應上
PS:
zkServer.sh status 查看狀態
zkCli.sh -server ip:2181 # zk cli操做
ls / # 列出節點
create /web1 'webgroup' # 建立節點
默認狀況,啓動腳本啓動zookeeper失敗緣由:
首先須要知道 交互式shell和非交互式shell、登陸shell和非登陸shell是有區別的
在登陸shell裏,環境信息須要讀取/etc/profile和~ /.bash_profile, ~/.bash_login, and ~/.profile按順序最早的一個,並執行其中的命令。除非被 --noprofile選項禁止了; 在非登陸shell裏,環境信息只讀取 /etc/bash.bashrc和~/.bashrc
手工執行是屬於登錄shell,腳本執行數據非登錄shell,而個人linux環境配置中只對/etc/profile進行了jdk1.7等環境的配置,因此腳本執行/usr/local/zookeeper/bin/zkServer.sh start 啓動zookeeper失敗了
解決方法(下面3個方法任選1):
一、腳本代碼中添加「source /etc/profile;」 改成:ssh crxy$i "source /etc/profile;/usr/local/zookeeper/bin/zkServer.sh start"
二、把profile的配置信息echo到.bashrc中 echo 'source /etc/profile' >> ~/.bashrc
三、在/zookeeper/bin/zkEnv.sh的中開始位置添加 export JAVA_HOME=/usr/local/jdk1.7.0_45(就像hadoop中對hadoop-env.sh的配置同樣)
若其中一個節點故障,如忽然停電、磁盤故障再修復,致使zk啓動失敗,
```
zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
查看zookeeper.out
2018-02-23 14:10:29,993 [myid:] - INFO [main:QuorumPeerConfig@103] - Reading configuration from: /usr/local/zookeeper/bin/../conf/zoo.cfg
2018-02-23 14:10:30,003 [myid:] - INFO [main:QuorumPeer$QuorumServer@149] - Resolved hostname: 192.168.1.75 to address: /192.168.1.75
2018-02-23 14:10:30,004 [myid:] - INFO [main:QuorumPeer$QuorumServer@149] - Resolved hostname: 192.168.1.69 to address: /192.168.1.69
2018-02-23 14:10:30,004 [myid:] - INFO [main:QuorumPeer$QuorumServer@149] - Resolved hostname: 192.168.1.68 to address: /192.168.1.68
2018-02-23 14:10:30,004 [myid:] - INFO [main:QuorumPeer$QuorumServer@149] - Resolved hostname: 192.168.1.214 to address: /192.168.1.214
2018-02-23 14:10:30,004 [myid:] - INFO [main:QuorumPeer$QuorumServer@149] - Resolved hostname: 192.168.1.213 to address: /192.168.1.213
2018-02-23 14:10:30,004 [myid:] - INFO [main:QuorumPeerConfig@331] - Defaulting to majority quorums
2018-02-23 14:10:30,006 [myid:5] - INFO [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2018-02-23 14:10:30,006 [myid:5] - INFO [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2018-02-23 14:10:30,007 [myid:5] - INFO [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2018-02-23 14:10:30,014 [myid:5] - INFO [main:QuorumPeerMain@127] - Starting quorum peer
2018-02-23 14:10:30,021 [myid:5] - INFO [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:4180
2018-02-23 14:10:30,025 [myid:5] - INFO [main:QuorumPeer@1019] - tickTime set to 2000
2018-02-23 14:10:30,025 [myid:5] - INFO [main:QuorumPeer@1039] - minSessionTimeout set to -1
2018-02-23 14:10:30,026 [myid:5] - INFO [main:QuorumPeer@1050] - maxSessionTimeout set to -1
2018-02-23 14:10:30,026 [myid:5] - INFO [main:QuorumPeer@1065] - initLimit set to 5
2018-02-23 14:10:45,543 [myid:5] - ERROR [main:QuorumPeer@557] - Unable to load database on disk
java.io.IOException: Transaction log: /data/zookeeper/logs/version-2/log.24f7000209f2 has invalid magic number 1331917856 != 1514884167
at org.apache.zookeeper.server.persistence.FileTxnLog$FileTxnIterator.inStreamCreated(FileTxnLog.java:584)
at org.apache.zookeeper.server.persistence.FileTxnLog$FileTxnIterator.createInputArchive(FileTxnLog.java:600)
at org.apache.zookeeper.server.persistence.FileTxnLog$FileTxnIterator.goToNextLog(FileTxnLog.java:566)
at org.apache.zookeeper.server.persistence.FileTxnLog$FileTxnIterator.next(FileTxnLog.java:648)
at org.apache.zookeeper.server.persistence.FileTxnSnapLog.restore(FileTxnSnapLog.java:158)
```
加載數據數據失敗
處理方法:
刪除 dataDir 目錄下除文件myid外的其餘文件或目錄,而後重啓zk