上个章节《深入浅出ZooKeeper(1)— 基本介绍》 对ZooKeeper进行了基本的介绍,这个章节咱们直接进入实战阶段,来进行ZooKeeper的集群安装。为了演示效果,笔者在阿里云申请三台按时付费的服务器。
安装步骤
进入zookeeper1的服务器,登陆到用户,为了演示方便 ,笔者使用root,在真实的工作环境中需要额外创建一个安装zookeeper的账户。
- 下载安装包
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz
如果大家下载的比较慢的话,可以尝试到CSDN上下载。
- 解压缩
mkdir /opt/zookeeper
tar -zxvf ./apache-zookeeper-3.6.2-bin.tar.gz -C /opt/zookeeper
3.修改配置文件
cd /opt/zookeeper/apache-zookeeper-3.6.2-bin/conf
cp zoo_sample.cfg zoo.cfg
- 修改zoo.cfg
zoo.cfg的默认内容为:
# 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
# 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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
创建一个新的目录 /opt/zookeeper/apache-zookeeper-3.6.2-bin/zkData
mkdir /opt/zookeeper/apache-zookeeper-3.6.2-bin/zkData
存放zookeeper的数据,前面的章节提到,在zookeeper把数据放到内存数据库里面前,先要持久化到磁盘上,次zkData就是存放需要持久化的数据。修改后的配置如下:
# 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=/opt/zookeeper/apache-zookeeper-3.6.2-bin/zkData
# 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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
- 启动Zookeeper,进入到/opt/zookeeper/apache-zookeeper-3.6.2-bin 目录
cd /opt/zookeeper/apache-zookeeper-3.6.2-bin
并执行./bin/zkServer.sh start 会发现,原来zookeeper需要依赖于JDK的环境。这个在第一章节也提到了。
[root@zookeeper01 apache-zookeeper-3.6.2-bin]# ./bin/zkServer.sh start
Error: JAVA_HOME is not set and java could not be found in PATH.
- 安装JDK
在阿里云的服务上,直接执行
yum install java
如果是别的操作系统,请用相应的源进行安装;或者从Oracle官方网站下载安装,网上有很多安装JDK的文章,本文省略1万字。执行java -version, 其默认安装了jdk1.8。
[root@zookeeper01 apache-zookeeper-3.6.2-bin]# java -version
openjdk version "1.8.0_262"
OpenJDK Runtime Environment (build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)
- 再次启动Zookeeper服务器
[root@zookeeper01 apache-zookeeper-3.6.2-bin]# ./bin/zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Zookeeper启动成功!
- 测试安装
[root@zookeeper01 apache-zookeeper-3.6.2-bin]# telnet 127.0.0.1 2181
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'
客户端端口2181 已经能够连接通!在通过 ./bin/zkServer.sh status 查看其状态,发现单机版已经启动成功!
[root@zookeeper01 apache-zookeeper-3.6.2-bin]# ./bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone
使用Zookeeper 客户端连接上去。
[root@zookeeper01 apache-zookeeper-3.6.2-bin]# ./bin/zkCli.sh
/usr/bin/java
Connecting to localhost:2181
2020-09-14 23:00:34,964 [myid:] - INFO [main:Environment@98] - Client environment:zookeeper.version=3.6.2--803c7f1a12f85978cb049af5e4ef23bd8b688715, built on 09/04/2020 12:44 GMT
2020-09-14 23:00:34,969 [myid:] - INFO [main:Environment@98] - Client environment:host.name=zookeeper01
2020-09-14 23:00:34,969 [myid:] - INFO [main:Environment@98] - Client environment:java.version=1.8.0_262
2020-09-14 23:00:34,972 [myid:] - INFO [main:Environment@98] - Client environment:java.vendor=Oracle Corporation
2020-09-14 23:00:34,972 [myid:] - INFO [main:Environment@98] - Client environment:java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.1.al7.x86_64/jre
2020-09-14 23:00:34,972 [myid:] - INFO [main:Environment@98] - Client environment:java.class.path=/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../zookeeper-server/target/classes:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../build/classes:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../zookeeper-server/target/lib/*.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../build/lib/*.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/zookeeper-prometheus-metrics-3.6.2.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/zookeeper-jute-3.6.2.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/zookeeper-3.6.2.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/snappy-java-1.1.7.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/slf4j-log4j12-1.7.25.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/slf4j-api-1.7.25.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/simpleclient_servlet-0.6.0.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/simpleclient_hotspot-0.6.0.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/simpleclient_common-0.6.0.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/simpleclient-0.6.0.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/netty-transport-native-unix-common-4.1.50.Final.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/netty-transport-native-epoll-4.1.50.Final.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/netty-transport-4.1.50.Final.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/netty-resolver-4.1.50.Final.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/netty-handler-4.1.50.Final.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/netty-common-4.1.50.Final.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/netty-codec-4.1.50.Final.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/netty-buffer-4.1.50.Final.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/metrics-core-3.2.5.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/log4j-1.2.17.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/json-simple-1.1.1.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/jline-2.14.6.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/jetty-util-9.4.24.v20191120.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/jetty-servlet-9.4.24.v20191120.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/jetty-server-9.4.24.v20191120.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/jetty-security-9.4.24.v20191120.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/jetty-io-9.4.24.v20191120.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/jetty-http-9.4.24.v20191120.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/javax.servlet-api-3.1.0.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/jackson-databind-2.10.3.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/jackson-core-2.10.3.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/jackson-annotations-2.10.3.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/commons-lang-2.6.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/commons-cli-1.2.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../lib/audience-annotations-0.5.0.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../zookeeper-*.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../zookeeper-server/src/main/resources/lib/*.jar:/opt/zookeeper/apache-zookeeper-3.6.2-bin/bin/../conf:
2020-09-14 23:00:34,972 [myid:] - INFO [main:Environment@98] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-09-14 23:00:34,973 [myid:] - INFO [main:Environment@98] - Client environment:java.io.tmpdir=/tmp
2020-09-14 23:00:34,973 [myid:] - INFO [main:Environment@98] - Client environment:java.compiler=<NA>
2020-09-14 23:00:34,973 [myid:] - INFO [main:Environment@98] - Client environment:os.name=Linux
2020-09-14 23:00:34,973 [myid:] - INFO [main:Environment@98] - Client environment:os.arch=amd64
2020-09-14 23:00:34,973 [myid:] - INFO [main:Environment@98] - Client environment:os.version=4.19.91-21.al7.x86_64
2020-09-14 23:00:34,973 [myid:] - INFO [main:Environment@98] - Client environment:user.name=root
2020-09-14 23:00:34,973 [myid:] - INFO [main:Environment@98] - Client environment:user.home=/root
2020-09-14 23:00:34,973 [myid:] - INFO [main:Environment@98] - Client environment:user.dir=/opt/zookeeper/apache-zookeeper-3.6.2-bin
2020-09-14 23:00:34,973 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.free=12MB
2020-09-14 23:00:34,976 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.max=247MB
2020-09-14 23:00:34,976 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.total=15MB
2020-09-14 23:00:34,983 [myid:] - INFO [main:ZooKeeper@1006] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@379619aa
2020-09-14 23:00:34,987 [myid:] - INFO [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2020-09-14 23:00:34,999 [myid:] - INFO [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2020-09-14 23:00:35,013 [myid:] - INFO [main:ClientCnxn@1716] - zookeeper.request.timeout value is 0. feature enabled=false
Welcome to ZooKeeper!
2020-09-14 23:00:35,037 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1167] - Opening socket connection to server localhost/127.0.0.1:2181.
2020-09-14 23:00:35,040 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1169] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2020-09-14 23:00:35,175 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@999] - Socket connection established, initiating session, client: /127.0.0.1:51124, server: localhost/127.0.0.1:2181
2020-09-14 23:00:35,201 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1433] - Session establishment complete on server localhost/127.0.0.1:2181, session id = 0x100002a642c0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
执行下列帮助命令查看详细帮助信息。
[zk: localhost:2181(CONNECTED) 0] help
ZooKeeper -server host:port -client-configuration properties-file cmd args
addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
addauth scheme auth
close
config [-c] [-w] [-s]
connect host:port
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
delete [-v version] path
deleteall path [-b batch size]
delquota [-n|-b] path
get [-s] [-w] path
getAcl [-s] path
getAllChildrenNumber path
getEphemerals path
history
listquota path
ls [-s] [-w] [-R] path
printwatches on|off
quit
reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
redo cmdno
removewatches path [-c|-d|-a] [-l]
set [-s] [-v version] path data
setAcl [-s] [-v version] [-R] path acl
setquota -n|-b val path
stat [-w] path
sync path
version
在第一章节提到的Zookeeper的简单API的方法 这里面都有。
Windows操作系统的安装
Windows操作系统的安装方式是类似的,都需要先安装JDK,需要配置zookeeper的数据持久化的文件夹目录,唯一需要注意的是,在Windows操作系统上,文件夹的路径需要以 “/” 形式,而不能以“\”形式。下面是正确的示范的例子。
dataDir=E:/bigdata/apache-zookeeper-3.6.2-bin/zkdata
在Windows启动的样子如下图所示意。
总结
这篇文章主要介绍了如何安装Zookeeper的单机版,下一单元,给大家分享如何安装Zookeeper的集群。根据Zookeeper的集群的Leader选举机制,一个集群至少要有一半以上的节点工作集群才能正常选举,才能正常工作,所以节点最好是大于3个,所以选择4个和选择三个的意义一样,选择三个机器,只能挂一台机器,选择四台机器,也只能挂1台,如果4台机器挂了2台,其剩下两台机器,只是一半的数,还是没有过半,所以集群还是不能工作。所以Zookeeper的集群数目一班选择奇数台!敬请关注下个章节《深入浅出ZooKeeper(3)— ZooKeeper集群的安装》!