环境要求
telnet 192.168.1.160 2181
telnet 192.168.1.160 20880
软件 | 版本 | 下载 |
---|---|---|
CentOS | CentOS Linux release 7.4.1708 (Core) | http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso |
jdk | Java(TM) SE Runtime Environment (build 1.8.0_151-b12) | http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html |
zookeeper | zookeeper-3.4.11 | https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz |
下载解压
CentOS7 Java 省略,已经安装好了
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz
tar xzf zookeeper-3.4.11.tar.gz
cp zookeeper-3.4.11 /usr/local/
tree /usr/local/zookeeper-3.4.11
- bin目录
zk的可执行脚本目录,包括zk服务进程,zk客户端,等脚本。其中,.sh是Linux环境下的脚本,.cmd是Windows环境下的脚本。
- conf目录
配置文件目录。zoo_sample.cfg为样例配置文件,需要修改为自己的名称,一般为zoo.cfg。log4j.properties为日志配置文件。
- contrib目录
一些用于操作zk的工具包。
- recipes目录
zk某些用法的代码示例
配置说明
cp zoo_sample.cfg zoo.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=/data/zookeeper/data
dataLogDir=/data/zookeeper/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=zk149:2888:3888
server.2=zk150:2888:3888
server.3=zk151:2888:3888
配置项说明
- initLimit
ZooKeeper集群模式下包含多个zk进程,其中一个进程为leader,余下的进程为follower。 当follower最初与leader建立连接时,它们之间会传输相当多的数据,尤其是follower的数据落后leader很多。initLimit配置follower与leader之间建立连接后进行同步的最长时间。
- syncLimit
配置follower和leader之间发送消息,请求和应答的最大时间长度。
- tickTime
tickTime则是上述两个超时配置的基本单位,例如对于initLimit,其配置值为5,说明其超时时间为 2000ms * 5 = 10秒。
- server.|
=host:port1:port2 其中id为一个数字,表示zk进程的id,这个id也是dataDir目录下myid文件的内容。host是该zk进程所在的IP地址,port1表示follower和leader交换消息所使用的端口,port2表示选举leader所使用的端口。
- dataDir
配置的含义跟单机模式下的含义类似,不同的是集群模式下还有一个myid文件。myid文件的内容只有一行,且内容只能为1 - 255之间的数字,这个数字亦即上面介绍server.id中的id,表示zk进程的id。
- dataLogDir
zookeeper日志存储路径
配置环境变量
vim /etc/profile
# /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 PATH=/usr/local/php/bin:/usr/local/nginx/sbin:/usr/local/openssl/bin:$PATH
export PS1="\[\e]0;\a\]\n\[\e[1;32m\]\[\e[1;33m\]\H\[\e[1;35m\]<\$(date +\"%Y-%m-%d %T\")> \[\e[32m\]\w\[\e[0m\]\n\u>\\$ "
#export CATALINA_HOME=/usr/local/java/apache-tomcat-8.5.24
export JAVA_HOME=/usr/local/java/jdk1.8.0_151
export JRE_HOME=/usr/local/java/jdk1.8.0_151/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH:$CATALINA_HOME
export PATH=$JAVA_HOME/bin:$PATH
ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.11
export PATH=$ZOOKEEPER_HOME/bin:$PATH
刷新环境变量
source /etc/profile
关闭系统防火墙或配置防火墙规则
直接关闭防火墙
systemctl stop firewalld.service
禁止firewall开机启动 禁用防火墙命令
systemctl disable firewalld.service
停止防火墙
systemctl stop firewalld.service
zookeeper命令
启动 zookeeper 服务
zkServer.sh start
如打印如下信息则表明启动成功:
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
查看zookeeper 服务状态
zkServer.sh status
关闭zookeeper服务
zkServer.sh stop
如打印如下信息则表明成功关闭:
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
重启zookeeper服务
zkServer.sh restart
如打印如下信息则表明重启成功:
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
zookeeper自启动服务
vim /etc/rc.d/init.d/zookeeper
#!/bin/bash
#chkconfig:2345 20 90
#description:zookeeper
#processname:zookeeper
export JAVA_HOME=/usr/local/java/jdk1.8.0_151
export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.11
case $1 in
start) su root $ZOOKEEPER_HOME/bin/zkServer.sh start;;
stop) su root $ZOOKEEPER_HOME/bin/zkServer.sh stop;;
status) su root $ZOOKEEPER_HOME/bin/zkServer.sh status;;
restart) su $ZOOKEEPER_HOME/bin/zkServer.sh restart;;
*) echo "require start|stop|status|restart" ;;
esac
给zookeeper脚本授可执行权限
chmod +x /etc/rc.d/init.d/zookeeper
添加开机自启动服务
chkconfig --add zookeeper
查看自启动状态
chkconfig --list