欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Linux下zookeeper集群搭建

程序员文章站 2022-05-14 18:24:19
Linux下zookeeper集群搭建 部署前准备 1. 下载zookeeper的安装包 http://zookeeper.apache.org/releases.html 我下载的版本是zookeeper 3.4.10。 2. 准备三台服务器 ip地址为: 3. 检查jdk版本,安装jdk环境,j ......

linux下zookeeper集群搭建

部署前准备

  1. 下载zookeeper的安装包
     http://zookeeper.apache.org/releases.html 我下载的版本是zookeeper-3.4.10。

  2. 准备三台服务器
    ip地址为:

    172.16.18.198
    172.16.18.199
    172.16.18.200
  3. 检查jdk版本,安装jdk环境,jdk需要1.7以上。

安装zookeeper

1.三台服务器分别上传zookeeper安装包,上传到/opt/目录下,然后tar zxvf zookeeper-3.4.10.tar.gz

2.拷贝zoo_sample.cfg 为zoo.cfg 修改/opt/zookeeper-3.4.10/conf/zoo.cfg配置文件,添加如下内容:

server.1=172.16.18.198:2888:3888
server.2=172.16.18.199:2888:3888
server.3=172.16.18.200:2888:3888

3.修改zookeeper数据文件存放目录

datadir=/data/zookeeper

此时zoo.cfg 配置文件内容为:

# the number of milliseconds of each tick
ticktime=2000 ##zookeeper单位时间为2ms
# the number of ticks that the initial 
# synchronization phase can take
initlimit=10  ##对于从节点最初连接到主节点时的超时时间,单位为tick值的倍数。即20ms
# the number of ticks that can pass between 
# sending a request and getting an acknowledgement
synclimit=5   ##对于主节点与从节点进行同步操作时的超时时间,单位为tick值的倍数。即10ms
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
datadir=/data/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
server.1=172.16.18.198:2888:3888  
server.2=172.16.18.199:2888:3888
server.3=172.16.18.200:2888:3888

4.新建myid文件

在三台服务器的数据存放目录下新建myid文件,并写入对应的server.num 中的num数字
如:在172.16.18.198上将server.1中1写入myid

echo 1 >/data/zookeeper/myid

5.添加环境变量,方便我们执行脚本命令

vi etc/profile 在最后添加如下两个。

export zookeeper_home=/opt/zookeeper-3.4.9
export path=$path:$zookeeper_home/bin:$zookeeper_home/conf

保存后重新加载一下:

source /etc/profile

6.修改日志存放目录(可选)

vi /opt/zookeeper/bin/zkenv.sh 找到zoo_log_dir 和 zoo_log4j_prop位置

if [ "x${zoo_log_dir}" = "x" ] 
then 
    #配置zookeeper日志输出存放路径 
    zoo_log_dir="/var/applog/zookeeper" 
fi 

if [ "x${zoo_log4j_prop}" = "x" ] 
then 
    #配置日志输出级别,这里把几个级别一并配上 
    zoo_log4j_prop="info,console,rollingfile,tracefile" 
fi

编辑conf目录下log4j.properties

# define some default values that can be overridden by system properties 
zookeeper.root.logger=info, console, rollingfile, tracefile 
zookeeper.console.threshold=info 
zookeeper.log.dir=. 
zookeeper.log.file=zookeeper.log 
zookeeper.log.threshold=error 
zookeeper.tracelog.dir=. 
zookeeper.tracelog.file=zookeeper_trace.log 
log4j.rootlogger=${zookeeper.root.logger}

完成log的日志目录的修改。

7.启动zookeeper服务

zkserver.sh start来启动。

zkserver.sh restart  (重启)

zkserver.sh status  (查看状态)

zkserver.sh stop  (关闭)

zkserver.sh start-foreground  (以打印日志方式启动)
三台服务器分别执行:

zkserver.sh start

然后用 status 检查下状态 如果出现 mode:leader 或者mode:follower 表示搭建成功。否则前台执行看一下日志。

$ zkserver.sh status
zookeeper jmx enabled by default
using config: /opt/zookeeper-3.4.10/bin/../conf/zoo.cfg
mode: follower

如出现:

2019-04-29 14:04:05,992 [myid:3] - info  [listenerthread:quorumcnxmanager$listener@739] - my election bind port: /172.16.18.200:3888
2019-04-29 14:04:06,019 [myid:3] - info  [quorumpeer[myid=3]/0:0:0:0:0:0:0:0:2181:quorumpeer@865] - looking
2019-04-29 14:04:06,025 [myid:3] - info  [quorumpeer[myid=3]/0:0:0:0:0:0:0:0:2181:fastleaderelection@818] - new election. my id =  3, proposed zxid=0x0
2019-04-29 14:04:06,056 [myid:3] - warn  [workersender[myid=3]:quorumcnxmanager@588] - cannot open channel to 1 at election address /172.16.18.198:3888
java.net.noroutetohostexception: 没有到主机的路由
        at java.net.plainsocketimpl.socketconnect(native method)
        at java.net.abstractplainsocketimpl.doconnect(abstractplainsocketimpl.java:345)
        at java.net.abstractplainsocketimpl.connecttoaddress(abstractplainsocketimpl.java:206)
        at java.net.abstractplainsocketimpl.connect(abstractplainsocketimpl.java:188)
"zookeeper.log" 303l, 35429c

报这种异常一般有三种情况:

1):zoo.cfg配置文件中,server.x:2888:3888配置出现错误;

2):myid文件内容和server.x不对应,或者myid不在data目录下;

3):系统防火墙是否在启动。

我检查了三种原因后发现是防火墙running。

centos7下查看防火墙状态的命令:

firewall-cmd --state

关闭防火墙的命令:

systemctl stop firewalld.service
systemctl disable firewalld.service   (禁止开机启动,永久关闭防火墙)

关闭防火墙后重启即可。
8.验证是否成功
在命令行中输入:zkcli.sh -server 172.16.18.198:2181(由于本人在不同的办公地点在修改该文章,所以ip地址也在变化,知道原理即可)即可连接到其中一台zookeeper服务器。其他自动实现同步,客户端只需要和一台保持连接即可。出现如下表示链接成功

watcher::

watchedevent state:syncconnected type:none path:null
[zk: 172.16.18.198:2181(connected) 0]