Zookeeper:部署

  • 2018-09-01
  • 72
  • 0
  • 0

概念


官网地址:http://zookeeper.apache.org/

ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。

它的设计易于编程,它使用一个遵循文件系统中常见的目录树结构的数据模型。它在Java环境中运行,对Java和C都有绑定。

ZooKeeper运行在java环境下,所以需要先安装jdk,且要求版本高于1.6。

jdk安装过程:略。

ZooKeeper有三种安装模式:单机安装(standalone)、伪集群模式、集群模式:

  • standalone模式:只在一台机器上安装ZooKeeper,并只启动一个ZooKeeper实例。
  • 伪集群:在一台机器上开启多个ZooKeeper实例来模拟集群。通过加载不同配置文件,分配不同数据目录来实现这种模式。因为在同一机器上运行,所以机器宕机,整个ZooKeeper就挂了。基本上不会布置这样的ZooKeeper集群。
  • 集群模式:在至少3台机器上安装ZooKeeper构成集群,只要故障节点数量在一定范围内就能保证ZooKeeper服务的可用性。

 

单机安装


1.获取安装包

清华大学镜像:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/

cd /usr/local
mkdir -p zookeeper/data
cd zookeeper
# 选择最新稳定版本
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz

 

2.解压

tar zxvf zookeeper-3.4.13.tar.gz

 

3.创建环境变量

echo 'ZOOKEEPER_HOME=/usr/local/zookeeper/zookeeper-3.4.13' >/etc/profile.d/zk.sh
echo 'PATH=$ZOOKEEPER_HOME/bin:$PATH' >> /etc/profile.d/zk.sh
chmod +x /etc/profile.d/zk.sh
source /etc/profile.d/zk.sh

 

4.相关目录文件介绍

4.1 bin目录

[root@kafka-01 zookeeper-3.4.13]# ls bin/
README.txt    zkCli.cmd  zkEnv.cmd  zkServer.cmd  zkTxnLogToolkit.cmd
zkCleanup.sh  zkCli.sh   zkEnv.sh   zkServer.sh   zkTxnLogToolkit.sh

 

zkServer:用于启动、停止ZooKeeper,且能查看ZooKeeper状态。

zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}

zkEnv:设置ZooKeeper启动、关闭时的环境变量。在其余的每个脚本中都引用了这个脚本。

zkCleanup:清除ZooKeeper的事务日志和快照。

zkCli:ZooKeeper的一个命令行客户端。

 

4.2 conf目录

configuration.xsl:无视它。
log4f.properties:是ZooKeeper的日志配置文件。
zoo_sample.cfg:是ZooKeeper的示例配置文件。

zkServer.sh默认的配置文件名为zoo.cfg。所以,在conf目录下创建一个zoo.cfg。写入几个保证ZooKeeper能正常运行的配置项:

tickTime=2000
dataDir=/usr/local/zookeeper/data
clientPort=2181

 

其中:

  • tickTime:ZooKeeper中很多配置项(如心跳时间、连接超时时间等)都要用到的时间单位,这里配置为2000毫秒,即2秒。
  • dataDir:ZooKeeper实例的数据目录。
  • clientPort:ZooKeeper向外提供服务的端口。

5.启停

5.1 启动

使用zkServer.sh来启动ZooKeeper。

cd bin/
zkServer.sh start

 

也可以手动指定启动时读取的配置文件:

zkServer.sh start /usr/local/zookeeper/zookeeper-3.4.13/conf/zoo.cfg

 

查看启动状态:

[root@kafka-01 bin]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/zookeeper-3.4.13/bin/../conf/zoo.cfg
Mode: standalone

可以看到运行模式是”standalone”。

 

5.2 停止

zkServer.sh stop

 

6.连接

ZooKeeper启动后,就可以向外提供ZooKeeper服务了。这里用ZooKeeper提供的命令行客户端去连接一下ZooKeeper做个简单的测试。

[root@kafka-01 bin]# zkCli.sh -server localhost:2181
Connecting to localhost:2181
2018-08-28 09:42:48,880 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 04:05 GMT
2018-08-28 09:42:48,887 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=kafka-01
2018-08-28 09:42:48,887 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_181
2018-08-28 09:42:48,892 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-08-28 09:42:48,892 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/local/java/jdk1.8.0_181/jre
2018-08-28 09:42:48,892 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/zookeeper-3.4.13/bin/../build/classes:/usr/local/zookeeper/zookeeper-3.4.13/bin/../build/lib/*.jar:/usr/local/zookeeper/zookeeper-3.4.13/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/local/zookeeper/zookeeper-3.4.13/bin/../lib/slf4j-api-1.7.25.jar:/usr/local/zookeeper/zookeeper-3.4.13/bin/../lib/netty-3.10.6.Final.jar:/usr/local/zookeeper/zookeeper-3.4.13/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper/zookeeper-3.4.13/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/zookeeper-3.4.13/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper/zookeeper-3.4.13/bin/../zookeeper-3.4.13.jar:/usr/local/zookeeper/zookeeper-3.4.13/bin/../src/java/lib/*.jar:/usr/local/zookeeper/zookeeper-3.4.13/bin/../conf:.:/usr/local/java/jdk1.8.0_181/lib:/usr/local/java/jdk1.8.0_181/jre/lib
2018-08-28 09:42:48,892 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-08-28 09:42:48,892 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-08-28 09:42:48,893 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2018-08-28 09:42:48,893 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2018-08-28 09:42:48,893 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2018-08-28 09:42:48,893 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-862.el7.x86_64
2018-08-28 09:42:48,893 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2018-08-28 09:42:48,893 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2018-08-28 09:42:48,894 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper/zookeeper-3.4.13/bin
2018-08-28 09:42:48,899 [myid:] - INFO  [main:ZooKeeper@442] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@277050dc
2018-08-28 09:42:48,956 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1029] - 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
2018-08-28 09:42:49,112 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@879] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session
2018-08-28 09:42:49,168 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1303] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x100002654ed0000, negotiated timeout = 30000
 
WATCHER::
 
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]

 

集群部署


节点:

  • 10.0.10.32
  • 10.0.10.33
  • 10.0.10.34

 

1.获取安装包

清华大学镜像:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/

cd /usr/local
mkdir -p zookeeper/data
cd zookeeper
# 选择最新稳定版本
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz

 

2.解压

tar zxvf zookeeper-3.4.13.tar.gz

 

3.创建环境变量

echo 'ZOOKEEPER_HOME=/usr/local/zookeeper/zookeeper-3.4.13' >/etc/profile.d/zk.sh
echo 'PATH=$ZOOKEEPER_HOME/bin:$PATH' >> /etc/profile.d/zk.sh
chmod +x /etc/profile.d/zk.sh
source /etc/profile.d/zk.sh

 

4.zoo.cfg配置

三个节点配置文件(/usr/local/zookeeper/zookeeper-3.4.13/conf/zoo.cfg)内容如下:

tickTime=2000
dataDir=/usr/local/zookeeper/data
clientPort=2181
initLimit=5
syncLimit=2
server.1=10.0.10.32:2888:3888
server.2=10.0.10.33:2888:3888
server.3=10.0.10.34:2888:3888

 

在三个节点上写入myid文件:

# 节点1上执行
echo 1 >/usr/local/zookeeper/data/myid
 
# 节点2上执行
echo 2 >/usr/local/zookeeper/data/myid
 
# 节点3上执行
echo 3 >/usr/local/zookeeper/data/myid

 

cd /usr/local/zookeeper/zookeeper-3.4.13/bin
./zkServer.sh status

 

5.验证

# 节点1
[root@prod-kafka-1 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/zookeeper-3.4.13/bin/../conf/zoo.cfg
Mode: follower
 
# 节点2
[root@prod-kafka-2 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/zookeeper-3.4.13/bin/../conf/zoo.cfg
Mode: leader
 
 
# 节点3
[root@prod-kafka-3 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/zookeeper-3.4.13/bin/../conf/zoo.cfg
Mode: follower

 

 

评论

还没有任何评论,你来说两句吧