zookeeper简介
ZooKeeper是一个开放源码的分布式应用程序协调服务,是Google Chubby的一个开源实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。更多详细的资料请自行了解。官方链接:http://zookeeper.apache.org/
Zookeeper的安装和配置十分简单,既可以配置成单机模式,也可以配置成集群模式。本文将简单介绍集群的配置步骤(单机模式烦请自行百度)
zookeeper集群搭建前提
- 请确认至少有两台+主机(如果硬件不够,可以试用虚拟机);
- 能够 常用Linux命令
第一步:zookeeper下载
下载地址:http://zookeeper.apache.org/releases.html,我选择的版本是zookeeper-3.4.9(如果版本不一致可能存在差异)。
下载下来压缩包为zookeeper-3.4.9.tar.gz。请使用tar -xzvf zookeeper-3.4.9.tar.gz解压
第二步:zookeeper环境变量配置
命令:vi /etc/profile
增加zookeeper的环境变量ZOOKEEPER_HOME的本地路径,并且增加到PATH中。如下图:
export ZOOKEEPER_HOME=/opt/zkServer1/zookeeper-3.4.9
export PATH=$ZOOKEEPER_HOME/bin:$PATH
export PATH
命令:source /etc/profile
修改完成后使配置生效。
第三步:zookeeper配置文件配置
进入zookeeper的conf子文件夹。/conf/zoo_sample.cfg文件复制一份,并更名为zoo.cfg。配置dataDir、dataLogDir、增加server节点。
# 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=/opt/zookeeper-3.4.9/data
dataLogDir=/opt/zookeeper-3.4.9/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
server.1 = node1:2888:3888
server.2 = node2:2888:3888
server.3 = node3:2888:3888
server.A=B:C:D 其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 IP 地址(如果做了域名映射为域名);C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。
第四步:zookeeper识别文件创建。创建logs目录、data目录、myid文件
在data目录下创建一个myid文件,然后分别在myid文件中按照zoo.cfg文件的server.A中A的数值,在不同机器上的该文件中填写相应的值。例如server.1 在当前机器的myid中就只写上1就可以了。
第五步:Zookeeper配置拷贝到另外一台机器
在另外一台机器上试用scp命令远程拷贝当前Zookeeper文件夹。然后配置对应的环境变量。注意修改myid文件的值为2或3。
第六步:Zookeeper启动验证
进入bin目录执行命令“zkServer.sh start”启动Zookeeper。不同机器上的Zookeeper需要单独启动。执行命令“zkServer.sh stop”将会停止Zookeeper。
第七步:zookeeper当前状态查看
进入bin目录执行命令“zkServer.sh status”。
zookeeper领导者(leader):
zookeeper跟随者1(follower):
zookeeper跟随者2(follower):
到此,完成Zookeeper集群配置工作。
后话
如果出现Error contacting service. It is probably not running的错误的时候,参考以下解决办法:
1、查看myid、server.A=B:C:D是否配置正确或者检查是否由于防火墙开启的原因;
2、systemctl stop iptables.service
systemctl disable iptables.service
3、注意集群服务器的启动顺序