ZooKeeper安装部署记录

说明

此文仅记录Zookeeper单机和多节点搭建部署过程

操作系统使用的CentOS-7.6.1810 x86_64

JDK版本OpenJDK-1.8

ZooKeeper版本3.4.14

虚拟机配置1CPU 4G内存 20G系统盘 30G数据盘

环境准备

安装OpenJDK

1
yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

安装ZooKeeper

创建Zookeeper用户

1
2
groupadd zookeeper
useradd -r -g zookeeper -s /bin/false zookeeper

创建Zookeeper程序目录

1
mkdir -p /opt/zookeeper

切换工作目录

1
cd /opt/software

下载ZooKeeper

国内有Apache基金会的镜像源,这里用的是清华大学的源

1
wget -O - https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz | tar xz --directory=/opt/software

创建软连接

1
ln -sv zookeeper-3.4.14 zookeeper

创建数据目录

1
mkdir -p /opt/zk-data /opt/zk-log

创建配置文件

1
2
3
4
5
6
7
8
9
10
11
cat > /opt/software/zookeeper/conf/zoo.cfg <<EOF
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/opt/zk-data
dataLogDir=/opt/zk-log
clientPort=2181
maxClientCnxns=200
autopurge.snapRetainCount=3
autopurge.purgeInterval=1
EOF

授权目录

1
chown -R zookeeper:zookeeper /opt/zookeeper /opt/zk-data /opt/zk-log

启动Zookeeper

1
su -s /bin/sh -c "/opt/software/zookeeper/bin/zkServer.sh start /opt/software/zookeeper/conf/zoo.cfg" zookeeper

测试Zookeeper

使用客户端登陆

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
31
32
33
34
35
36
37
38
39
/opt/software/zookeeper/bin/zkCli.sh -server localhost:2181
# 输出示例
/bin/java
Connecting to localhost:2181
YYYY-MM-DD 10:53:57,868 [myid:] - INFO [main:Environment@109] - Client environment:zookeeper.version=3.5.5-390fe37ea45dee01bf87dc1c042b5e3dcce88653, built on 05/03/2019 12:07 GMT
YYYY-MM-DD 10:53:57,871 [myid:] - INFO [main:Environment@109] - Client environment:host.name=localhost
YYYY-MM-DD 10:53:57,871 [myid:] - INFO [main:Environment@109] - Client environment:java.version=1.8.0_212
YYYY-MM-DD 10:53:57,878 [myid:] - INFO [main:Environment@109] - Client environment:java.vendor=Oracle Corporation
YYYY-MM-DD 10:53:57,878 [myid:] - INFO [main:Environment@109] - Client environment:java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64/jre
YYYY-MM-DD 10:53:57,878 [myid:] - INFO [main:Environment@109] - Client environment:java.class.path=/opt/zookeeper/zookeeper/bin/../zookeeper-server/target/classes:/opt/zookeeper/zookeeper/bin/../build/classes:/opt/zookeeper/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/opt/zookeeper/zookeeper/bin/../build/lib/*.jar:/opt/zookeeper/zookeeper/bin/../lib/zookeeper-jute-3.5.5.jar:/opt/zookeeper/zookeeper/bin/../lib/zookeeper-3.5.5.jar:/opt/zookeeper/zookeeper/bin/../lib/slf4j-log4j12-1.7.25.jar:/opt/zookeeper/zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-all-4.1.29.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/log4j-1.2.17.jar:/opt/zookeeper/zookeeper/bin/../lib/json-simple-1.1.1.jar:/opt/zookeeper/zookeeper/bin/../lib/jline-2.11.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-util-9.4.17.v20190418.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-servlet-9.4.17.v20190418.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-server-9.4.17.v20190418.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-security-9.4.17.v20190418.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-io-9.4.17.v20190418.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-http-9.4.17.v20190418.jar:/opt/zookeeper/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/opt/zookeeper/zookeeper/bin/../lib/jackson-databind-2.9.8.jar:/opt/zookeeper/zookeeper/bin/../lib/jackson-core-2.9.8.jar:/opt/zookeeper/zookeeper/bin/../lib/jackson-annotations-2.9.0.jar:/opt/zookeeper/zookeeper/bin/../lib/commons-cli-1.2.jar:/opt/zookeeper/zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/opt/zookeeper/zookeeper/bin/../zookeeper-*.jar:/opt/zookeeper/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/opt/zookeeper/zookeeper/bin/../conf:
YYYY-MM-DD 10:53:57,879 [myid:] - INFO [main:Environment@109] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
YYYY-MM-DD 10:53:57,879 [myid:] - INFO [main:Environment@109] - Client environment:java.io.tmpdir=/tmp
YYYY-MM-DD 10:53:57,880 [myid:] - INFO [main:Environment@109] - Client environment:java.compiler=<NA>
YYYY-MM-DD 10:53:57,880 [myid:] - INFO [main:Environment@109] - Client environment:os.name=Linux
YYYY-MM-DD 10:53:57,880 [myid:] - INFO [main:Environment@109] - Client environment:os.arch=amd64
YYYY-MM-DD 10:53:57,880 [myid:] - INFO [main:Environment@109] - Client environment:os.version=3.10.0-957.12.2.el7.x86_64
YYYY-MM-DD 10:53:57,880 [myid:] - INFO [main:Environment@109] - Client environment:user.name=zookeeper
YYYY-MM-DD 10:53:57,881 [myid:] - INFO [main:Environment@109] - Client environment:user.home=/home/zookeeper
YYYY-MM-DD 10:53:57,881 [myid:] - INFO [main:Environment@109] - Client environment:user.dir=/home/zookeeper
YYYY-MM-DD 10:53:57,882 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.free=55MB
YYYY-MM-DD 10:53:57,887 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.max=247MB
YYYY-MM-DD 10:53:57,887 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.total=59MB
YYYY-MM-DD 10:53:57,890 [myid:] - INFO [main:ZooKeeper@868] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@3b95a09c
YYYY-MM-DD 10:53:57,906 [myid:] - INFO [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
YYYY-MM-DD 10:53:57,930 [myid:] - INFO [main:ClientCnxnSocket@237] - jute.maxbuffer value is 4194304 Bytes
YYYY-MM-DD 10:53:57,943 [myid:] - INFO [main:ClientCnxn@1653] - zookeeper.request.timeout value is 0. feature enabled=
YYYY-MM-DD 10:53:57,951 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1112] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
YYYY-MM-DD 10:53:58,115 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@959] - Socket connection established, initiating session, client: /0:0:0:0:0:0:0:1:56998, server: localhost/0:0:0:0:0:0:0:1:2181
YYYY-MM-DD 10:53:58,149 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1394] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x1000012d1c20002, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /zookeeper
[config, quota]

托管为systemd服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Unit]
Description=Zookeeper service
After=network.target

[Service]
Type=simple
User=zookeeper
Group=zookeeper
Environment=ZOO_LOG_DIR=/opt/zk-log
ExecStart=/opt/software/zookeeper/bin/zkServer.sh start-foreground /opt/software/zookeeper/conf/zoo.cfg
ExecStop=/opt/software/zookeeper/bin/zkServer.sh stop /opt/software/zookeeper/conf/zoo.cfg
WorkingDirectory=/opt/zk-data
RestartSec=60
Restart=on-failure

[Install]
WantedBy=multi-user.target

多节点部署Zookeeper

Zookeeper多节点部署比较简单

一般情况下使用三个节点,奇数节点方便集群内部选主

配置文件

这里需要在zoo.cfg添加集群节点信息

1
2
3
server.1=zk1:2888:3888
server.2=zk2:2888:3888
server.3=zk3:2888:3888

创建myid文件

每个节点都需要在zookeeper数据目录下创建myid,用于标记节点序号

  • zk1
1
echo '1' > /opt/zk-data/myid
  • zk2
1
echo '2' > /opt/zk-data/myid
  • zk3
1
echo '3' > /opt/zk-data/myid

启动Zookeeper

initLimit配置初始化时间内,在zk1zk2zk3启动Zookeeper即可