zookeeper简易安装

摘要

本文记录简易安装zookeeper单机过程。

安装zookeeper

设置环境变量

1
2
3
4
WORKDIR="/usr/local/src"
INSTALL_DIR="/usr/local/product"
SOFT_DIR="/usr/local"
VERSION="3.4.12"

创建zookeeper用户

1
useradd zookeeper

下载安装包

1
2
3
4
cd ${WORKDIR}
wget https://mirrors.aliyun.com/apache/zookeeper/zookeeper-${VERSION}/zookeeper-${VERSION}.tar.gz
tar xvf zookeeper-${VERSION}.tar.gz -C ${INSTALL_DIR}
ln -s ${INSTALL_DIR}/zookeeper-${VERSION} ${SOFT_DIR}/zookeeper

创建配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cd /usr/local/zookeeper/conf
cp zoo_sample.cfg zoo.cfg
echo -e "tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/zookeeper/data
clientPort=2181" > zoo.cfg
echo -e "ZOO_LOG_DIR=/data/logs/zookeeper" > zookeeper-env.sh
mkdir -p /data/zookeeper/data
mkdir -p /data/logs/zookeeper
chown zookeeper.zookeeper -R /data/zookeeper/
chown zookeeper.zookeeper -R /data/logs/zookeeper/
chown zookeeper.zookeeper -R /usr/local/zookeeper/

添加环境变量

1
2
3
echo -e "export ZOOKEEPER_HOME=/usr/local/zookeeper
export PATH=\$ZOOKEEPER_HOME/bin:\$PATH" >> /etc/profile
source /etc/profile

启动服务

1
2
3
4
5
6
su - zookeeper
zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

其他命令

1
2
3
4
5
6
7
8
9
zkServer.sh --help
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Usage: /usr/local/zookeeper/bin/zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
zkServer.sh status
zkServer.sh start-foreground
zkCli.sh -server localhost:2181

zookeeper集群模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cd /usr/local/zookeeper/conf
echo -e "tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/zookeeper/data
clientPort=2181
server.1=172.17.8.32:2889:3889
server.2=172.17.8.33:2889:3889
server.3=172.17.8.34:2889:3889" > zoo.cfg
cd /data/zookeeper/data
echo 1 > myid
echo 2 > myid
echo 3 > myid
chown zookeeper.zookeeper -R /data/zookeeper/
chown zookeeper.zookeeper -R /data/logs/zookeeper/
chown zookeeper.zookeeper -R /usr/local/zookeeper/

查看节点状态

1
2
3
4
5
6
7
8
9
10
11
echo srvr | nc 127.0.0.1 2181
Zookeeper version: 3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT
Latency min/avg/max: 0/0/0
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x1c000000cf
Mode: leader
Node count: 3003

扩容

新增两个节点,直接增加配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cd /usr/local/zookeeper/conf
echo -e "tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/zookeeper/data
clientPort=2181
server.1=172.17.8.32:2889:3889
server.2=172.17.8.33:2889:3889
server.3=172.17.8.34:2889:3889
server.4=172.17.8.35:2889:3889
server.5=172.17.8.36:2889:3889" > zoo.cfg
cd /data/zookeeper/data
echo 4 > myid
echo 5 > myid

先启动新增的节点,然后依次重启所有节点

1
zkServer.sh

查看状态

1
2
3
4
5
echo srvr | nc 127.0.0.1 2181
这种状态是整个集群没有全部重启时部分节点读取了新配置导致的,依次全部重启节点即可
# This ZooKeeper instance is not currently serving requests

测试

1
2
3
4
5
6
7
#zk1
zkCli.sh -server localhost:2181
create /zktest 'test'
# zk4
create /zktest