ZooKeeper集群安装
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等,Zookeeper集群安装时节点数最好为奇数,并且最少三个节点,至于为什么是奇数,这里和zookeeper的选举算法有关,此处不做讨论;
1、下载安装包:
下载地址:http://zookeeper.apache.org/releases.html#download,截至发表文章时,最新版本为3.5.1;
解压:tar -zxvf zookeeper-3.4.6.tar.gz;
2、添加环境变量:
cd 进去用户目录 vi .bash_profile,在文件末尾输入:
export ZOOKEEPER_HOME=/opt/app/zookeeper-3.4.6
export PATH=$PATH:$ZOOKEEPER_HOME/bin
3、修改配置文件:
cp zoo_sample.cfg zoo.cfg,编辑zoo.cfg如下:
# 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=/data/zookeeper/zkdata
dataLogDir=/data/zookeeper/zkdatalog</span>
# 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=master:2888:3888
server.2=salve1:2888:3888
server.3=salve2:2888:3888
注意:dataDir和dataLogDir配置路径必须得存在,如果不存在启动时会报错;
4、在dataDir目录下,创建一个名为myid的文件,并在文件中存储对应集群节点id,
如server.1=master:2888:3888这个节点,myid则内容则为1(echo 1 >> myid),
以此类推,该id不允许为0;
5、启动zkServer
在各节点上分别执行:zkServer.sh start 并通过jps可以看到:启动了QuorumpeerMain进程。
6、通过zkServer.sh status 命令查看节点启动状态,至少启动了三个节点之后,该命令才有效,
否则会显示:zookeeper Error contacting service. It is probably not running错误:
至此,zookeeper集群已正常运行;
7、关闭zookeeper服务
zkServer.sh stop 关闭
zkServer.sh restart 重启
zkServer.sh stop 关闭
zkServer.sh restart 重启