本文共 5637 字,大约阅读时间需要 18 分钟。
本文介绍的 Zookeeper 是以 3.4.10 这个稳定版本为基础,最新的版本可以通过。Zookeeper 不仅可以单机提供服务,同时也支持多机组成集群来提供服务。实际上 Zookeeper 还支持另外一种伪集群的方式,也就是可以在一台物理机上运行多个 Zookeeper实例。Zookeeper的安装非常简单,本文主要介绍集群模式的安装和配置。环境如下图所示:
1、安装
[hadoop@hdp01 ~]$ wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/zookeeper-3.4.10.tar.gz[hadoop@hdp01 ~]$ tar -xzf zookeeper-3.4.10.tar.gz[hadoop@hdp01 ~]$ mv zookeeper-3.4.10 /u01/zookeeper
创建两个文件夹分别存放data和log:
[hadoop@hdp01 ~]$ mkdir /u01/zookeeper/data/[hadoop@hdp01 ~]$ mkdir /u01/zookeeper/logs/
2、配置
2.1 创建配置文件zoo.cfg是zookeeper的主要配置文件,因为Zookeeper是一个集群服务,集群的每个节点都需要这个配置文件。为了避免出差错,zoo.cfg这个配置文件里没有跟特定节点相关的配置,所以每个节点上的这个zoo.cfg都是一模一样的配置。这样就非常便于管理了,比如我们可以把这个文件提交到版本控制里管理起来。其实这给我们设计集群系统的时候也是个提示:集群系统一般有很多配置,应该尽量将通用的配置和特定每个服务的配置(比如服务标识)分离,这样通用的配置在不同服务之间copy就可以了。[hadoop@hdp01 ~]$ vi /u01/zookeeper/conf/zoo.cfg # The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial# synchronization phase can takeinitLimit=10# The number of ticks that can pass between# sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just# example sakes.dataDir=/u01/zookeeper/datadataLogDir=/u01/zookeeper/logs# the port at which the clients will connectclientPort=2181# the maximum number of client connections.# increase this if you need to handle more clientsmaxClientCnxns=100## 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=1server.1=hdp01:2888:3888server.2=hdp02:2888:3888server.3=hdp03:2888:3888server.4=hdp04:2888:3888
[hadoop@hdp01 ~]$ for i in {2..4};do scp -r /u01/zookeeper hdp0$i:/u01/zookeeper;done[hadoop@hdp01 ~]$ for i in {1..4};do ssh hdp0$i "echo $i >/u01/zookeeper/data/myid";done
3、管理
3.1 启动集群在任意一台机器上操作即可。[hadoop@hdp01 ~]$ for i in {1..4};do ssh hdp0$i "source .bash_profile;zkServer.sh start";doneZooKeeper JMX enabled by defaultUsing config: /u01/zookeeper/bin/../conf/zoo.cfgStarting zookeeper ... STARTEDZooKeeper JMX enabled by defaultUsing config: /u01/zookeeper/bin/../conf/zoo.cfgStarting zookeeper ... STARTEDZooKeeper JMX enabled by defaultUsing config: /u01/zookeeper/bin/../conf/zoo.cfgStarting zookeeper ... STARTEDZooKeeper JMX enabled by defaultUsing config: /u01/zookeeper/bin/../conf/zoo.cfgStarting zookeeper ... STARTED
3.2 查看集群状态
[hadoop@hdp01 ~]$ for i in {1..4};do ssh hdp0$i "source .bash_profile;zkServer.sh status";doneZooKeeper JMX enabled by defaultUsing config: /u01/zookeeper/bin/../conf/zoo.cfgMode: followerZooKeeper JMX enabled by defaultUsing config: /u01/zookeeper/bin/../conf/zoo.cfgMode: followerZooKeeper JMX enabled by defaultUsing config: /u01/zookeeper/bin/../conf/zoo.cfgMode: leaderZooKeeper JMX enabled by defaultUsing config: /u01/zookeeper/bin/../conf/zoo.cfgMode: follower
3.3 连接集群
[hadoop@hdp01 ~]$ zkCli.sh -server hdp01:2181,hdp02:2181,hdp03:2181,hdp04:2181Connecting to localhost:21812017-12-13 10:08:15,949 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT2017-12-13 10:08:15,957 [myid:] - INFO [main:Environment@100] - Client environment:host.name=hdp01.thinkjoy.tt2017-12-13 10:08:15,957 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_1522017-12-13 10:08:15,961 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation2017-12-13 10:08:15,961 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/java/jdk1.8.0_152/jre......Welcome to ZooKeeper!2017-12-13 10:08:16,001 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)JLine support is enabled2017-12-13 10:08:16,081 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session2017-12-13 10:08:16,093 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x1604d9842db0006, negotiated timeout = 30000WATCHER::WatchedEvent state:SyncConnected type:None path:null[zk: localhost:2181(CONNECTED) 0]
参考文献:
1、2、3、转载于:https://blog.51cto.com/candon123/2050091