Zookeeper集群及伪集群模式搭建(整理)

zookeeper简介
zookeeper单机模式搭建

前提
环境

机器1: centos6 ; hostname: cluster1; ip: 192.168.0.201
机器2: centos6 ; hostname: cluster2; ip: 192.168.0.203
机器3: centos6 ; hostname: cluster3; ip: 192.168.0.205

备注
  • 注意要关闭防火墙
    service iptables stop
    chkconfig iptables off
  • zookeeper需要至少3个节点,一个节点启动错误就无法正确启动。
    启动后最少需要2个节点才能正常运行

开始搭建

  1. 下载安装文件zookeeper-3.4.11.tar.gz

镜像地址1: http://apache.fayea.com/zookeeper/
镜像地址2: http://mirrors.hust.edu.cn/apache/zookeeper/

  1. 可使用wget直接在linux下载。(如果提示找不到wget命令,执行yum install wget下载wget包,其他环境可以使用其它命令,如ubantu的apt-get)
    如:
yum install wget
wget http://apache.fayea.com/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz
  1. 新建/usr/loca/zookeeper文件夹
[root@localhost ~]# mkdir -pv /usr/local/zookeeper
  1. 拷贝下载的zookeeper压缩包到指定文件夹,并解压,同时创建zk的数据文件目录和日志目录
[root@localhost ~]# cp ./zookeeper-3.4.11.tar.gz  /usr/local/zookeeper
[root@localhost ~]# cd /usr/local/zookeeper
[root@localhost ~]# mkdir /var/zookeeper/data
[root@localhost ~]# mkdir /var/zookeeper/log
[root@localhost zookeeper]# tar -xzvf ./zookeeper-3.4.11.tar.gz
  1. zookeeper运行在jvm上,需要提前安装java环境,链接:
    https://www.jianshu.com/p/6003393e1c78

  2. 配置环境变量

[root@localhost ~]#vi /etc/profile 
在文件末尾添加以下内容
export ZOOKEEPER_INSTALL=/usr/local/zookeeper/zookeeper-3.4.11/
export PATH=$PATH:$ZOOKEEPER_INSTALL/bin

使用source命令使环境变量生效

source /etc/profile
  1. 配置文件修改
[root@localhost zookeeper-3.4.11]# cd /usr/local/zookeeper/zookeeper-3.4.11/conf/ -- 进入文件所在路径
[root@localhost conf]# cp zoo_sample.cfg zoo.cfg -- 将zoo.sample.cfg拷贝出一份zoo.cfg出来

配置文件简单解析

  • tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
  • dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。必须手动创建。
  • dataLogDir:顾名思义就是 Zookeeper 保存日志文件的目录。必须手动创建。
  • clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
[root@localhost conf]# vi zoo.cfg 
-- 按i进入编辑模式. 修改完成之后, esc退出编辑模式, 大写字母Z双击即可保存.
# The number of milliseconds of each tick
# zookeeper 定义的基准时间间隔,单位:毫秒
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=/var/zookeeper/data -- 数据文件路径
dataLogDir=/var/zookeeper/log -- 日志路径
# the port at which the clients will connect
# 客户端访问 zookeeper 的端口号,如果是多机器的话,在clientPort处添加服务器的ip
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
# log文件保存20个,snap快照文件保存20个,若zookeeper启动时发现超出时,就会执行删除文件的操作
autopurge.snapRetainCount=20
# Purge task interval in hours
# Set to "0" to disable auto purge feature
# 间隔5小时清理一次
autopurge.purgeInterval=5
  1. 如果是单机模式,安装到这一步就可以保存配置文件,并启动了。
    而集群模式则需要在文件末尾继续添加如下内容
server.1=cluster1:2888:3888
server.2=cluster2:2888:3888
server.3=cluster3:2888:3888

每个server.x中,x代表该server节点的节点id

  1. 在dataDir对应目录创建myid文件
vi /var/zookeeper/data/myid

在文件中输入该节点对应的id后保存并退出

《Zookeeper集群及伪集群模式搭建(整理)》

  1. 打开/etc/hosts
vi /etc/hosts

在末尾加入所有节点的ip和主机名的映射,如

《Zookeeper集群及伪集群模式搭建(整理)》

安装另外两台服务器上的zk服务

在三台机器上分别执行如上操作,保证myid文件中的节点id不同且与配置文件对应即可。

检验

分别启动三台机器上的zookeeper实例,过数秒后查询状态

zkServer.sh start
zkServer.sh status

《Zookeeper集群及伪集群模式搭建(整理)》
《Zookeeper集群及伪集群模式搭建(整理)》

伪集群

伪集群通过配置多个zoo.cfg实现,设置多个数据目录和日志目录,并保证clientPort不重复且未占用即可。启动时执行zkServer.sh start zoo1.config指定配置文件。

    原文作者:普度众生的面瘫青年
    原文地址: https://www.jianshu.com/p/1f8051080f11
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞