您的位置:首页 > 运维架构 > Apache

Zookeeper集群环境搭建

2018-01-02 21:00 686 查看
        Zookeeper是一个很好的集群管理工具,被大量用于分布式,微服务等。如阿里的Dubbo,Hadoop以及Storm等。这里简单记录下Zookeeper集群环境的搭建过程。本文以Windows7做为搭建的系统环境。

        从Apache上(zookeeper.apache.org)下载zookeeper软件包,这里我选择最新稳定版3.4.11(zookeeper-3.4.11.tar),由于Zookeeper不区分Linux和Windows版,所以我们只需要解压zookeeper-3.4.11.tar,即可使用。如图:


bin目录如下:





一.准备工作

我们这里以本机三台zookeeper为例搭建一个最小的集群环境,三台服务器或者三台虚拟器均可。

将zookeeper拷贝三份,分别命名:zoopeeker-1,zoopeeker-2,zoopeeker-3。如图:



注意:zoopeeker集群环境只能配置为奇数台。

二.配置

(1).Host配置

为了配置与管理方便,最好给zookeeper服务IP配置Host地址。

在 C:\Windows\System32\drivers\etc 路径下面找到 hosts 文件,用编辑工具打开,添加host地址,如下:

#内网或者外网集群搭建
192.168.6.145 slave-01
192.168.6.145 slave-02
192.168.6.145 slave-03

#本地集群搭建
127.0.0.1 slave-01
127.0.0.1 slave-02
127.0.0.1 slave-03
注意:配置host只是为了方便管理与维护,可以不配置。如果需要配置,则每台zookeeper服务的host都需要配置。

(2).zoo.cfg配置

将conf/zoo_sample.cfg拷贝一份命名为zoo.cfg,也放在conf目录下。然后按照如下值修改其中的配置:

zookeeper-1配置:

# 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=D:/software/zookeeper/zookeeper-1/data
dataLogDir=D:/software/zookeeper/zookeeper-1/logs
# 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

#使用IP地址配置
server.1=localhost:2881:3881
server.2=localhost:2882:3882
server.3=localhost:2883:3883

#使用host地址配置
#server.1=slave-01:2881:3881
#server.2=slave-02:2882:3882
#server.3=slave-03:2883:3883


zookeeper-2配置:

# 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=D:/software/zookeeper/zookeeper-2/data
dataLogDir=D:/software/zookeeper/zookeeper-2/logs
# the port at which the clients will connect
clientPort=2182
# 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

#使用IP地址配置
server.1=localhost:2881:3881
server.2=localhost:2882:3882
server.3=localhost:2883:3883


zookeeper-3配置:

# 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=D:/software/zookeeper/zookeeper-3/data
dataLogDir=D:/software/zookeeper/zookeeper-3/logs
# the port at which the clients will connect
clientPort=2183
# 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

#使用IP地址配置
server.1=localhost:2881:3881
server.2=localhost:2882:3882
server.3=localhost:2883:3883


参数说明如下:

dataDir:数据目录。

dataLogDir:日志目录。

clientPort:zookeeper端口。

server.1/server.2/server.3(server.X):集群节点配置,前缀统一为“server.”,后面的数字表示集群节点编号,对应zookeeper集群的myid。

server.X=A:B:C

X:代表服务器编号。

A:代表ip。

B和C:代表端口,这个端口用来系统之间通信。

(3).创建myid文件

在创建myid文件之前,先在各个zookeeper-X文件里创建 data 和 logs 文件夹。

在 data 文件夹下创建名为 myid 的文件,内容为 X ,对应server.X。

例如:

zookeeper-1 的 myid 值为 1。

zookeeper-2 的 myid 值为 2。

zookeeper-3 的 myid 值为 3.



三.启动

依次启动三台服务器。

进入zookeeper-X/bin目录,点击zkServer.cmd启动。

三台服务器启动后如下:



注意:由于zookeeper集群启动的时候,每个节点都试图去连接集群中的其他节点,先启动的肯定连不上后面还没启动的,所以显示的日志前面部分的异常是可以忽略的。

通过后面部分可以看到,集群在选出一个Leader后,最后就稳定了。

四.验证

        Windows系统下,验证zookeeper的状态,需要在telnet下验证。使用命令telnet ip port 连接zookeeper,输入start查看当前节点状态。如图:

zookeeper-1状态:



zookeeper-2状态:



zookeeper-3状态:



        在zookeeper集群中,会有三种角色:leader、follower、observer分别对应着总统、议员、观察者。可以看到,在上面截图中,zookeeper-2是leader,zookeeper-1和zookeeper-3是follower。

        半数以上投票通过:可以这样理解。客户端的增删改操作无论访问到了哪台zookeeper服务器,最终都会被转发给leader服务器,再由leader服务器分给zookeeper集群中所有follower服务器去投票(投票指的是在内存中做增删改操作),半数投票通过就被认为操作可执行(commit),否则不可执行。

        observer观察者服务器是针对于查询操作做负载的。observer与follower服务器最大的不同在于observer没有投票权,在客户端发起的增删改操作中,leader服务器是不会把消息传递给observer服务器让其投票的。但是查询操作跟follower一样,客户端的查询到了observer服务器节点,observer服务器去访问leader服务器取最新的数据然后返回给客户端。

五.本人遇到的zookeeper启动出错情况

        双击运行zkServer.cmd出现闪退情况。我们编辑bin/zkServer.cmd文件,在文件的最后加上 “pause”。这样cmd窗口就不会闪退,我们就能看到zookeeper启动出错的原因了。

@echo off
REM Licensed to the Apache Software Foundation (ASF) under one or more
REM contributor license agreements. See the NOTICE file distributed with
REM this work for additional information regarding copyright ownership.
REM The ASF licenses this file to You under the Apache License, Version 2.0
REM (the "License"); you may not use this file except in compliance with
REM the License. You may obtain a copy of the License at
REM
REM http://www.apache.org/licenses/LICENSE-2.0 REM
REM Unless required by applicable law or agreed to in writing, software
REM distributed under the License is distributed on an "AS IS" BASIS,
REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
REM See the License for the specific language governing permissions and
REM limitations under the License.

setlocal
call "%~dp0zkEnv.cmd"

set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*

endlocal

pause

(1).myid文件缺失。



        这是因为myid文件没有配对。myid文件虽然存放的是服务器的编号 X ,但是它不是txt文件,注意文件格式。

(2)JAVA_HOME没有配或者是JAVA_HOME路径不对。



对于这个错误,第一行就显示说JAVA_HOME is not set,我们可以用编辑的方式打开bin/zkEnv.cmd,可以看到是JAVA_HOME的问题。



我们只需要在系统的环境变量中将JAVA_HOME配置好就OK了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息