ZooKeeper安装部署

0x00 环境说明

OS: CentOS-6.5-x86_64
JDK: jdk-8u111-linux-x64
Hadoop: hadoop-2.6.5
Zookeeper: zookeeper-3.4.6

0x01 简介

Zookeeper是一个很好的集群管理工具,被大量用于分布式计算。如Hadoop以及Storm系统中。
Apache ZooKeeper是一个为分布式应用所设计开源协调服务,其设计目是为了减轻分布式应用程序所承担的协调任务。可以为用户提供同步、配置管理、分组和命名服务。

0x02 部署规划

在三台装有centos6.5(64位)服务器上安装ZooKeeper,官网建议至少3个节点,本次实验3台(主机数:3(要求3+,且必须是奇数,因为ZooKeeper选举算法))。
需要提前安装JDK,选择版本是jdk-8u111-linux-x64。

0x03 安装配置

3.1 下载

下载ZooKeeper,选择稳定版,即stable这里我们下载zookeeper-3.4.6.tar.gz

3.2 安装

$ tar -zxvf zookeeper-3.4.6.tar.gz
//将文件移动到/usr/cloud/
$ mv zookeeper-3.4.6 ~/cloud/zookeeper-3.4.6
//建立软链
$ ln -s /home/hadoop/cloud/zookeeper-3.4.6 /home/hadoop/cloud/zookeeper

3.3 配置环境变量

# vim /etc/profile
//最后一行追加
# set zookeeper environment
export ZOOKEEPER_HOME=/home/hadoop/cloud/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin:$ZOOKEEPER_HOME/conf

使环境变量立即生效

$ source /etc/profile

3.4 集群部署

在Zookeeper集群环境下只要一半以上的机器正常启动了,那么Zookeeper服务将是可用的。因此,集群上部署Zookeeper最好使用奇数台机器,这样如果有5台机器,只要3台正常工作则服务将正常使用。
下面我们将对Zookeeper的配置文件zoo.cfg的参数进行设置。默认zoo.cfg是不存在的我们需要从zoo_sample.cfg复制出来一份。

$ cd /usr/cloud/zookeeper/conf
$ cp zoo_sample.cfg zoo.cfg
$ vim 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 
# 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=/home/hadoop/cloud/zookeeper/data
# the port at which the clients will connect
clientPort=2181

server.1=master:2888:3888
server.2=slave1:2888:3888
server.3=slave2:2888:3888
# 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

注意配置中masterslave1分别为主机名,具体的对应的主机可参见之前的Hadoop的安装与配置的博文。

在上面的配置文件中server.id=host:port:port中的第一个port是从机器(follower)连接到主机器(leader)的端口号,第二个port是进行leadership选举的端口号。

接下来在dataDir所指定的目录下创建一个文件名为myid的文件,文件中的内容只有一行,为本主机对应的id值,也就是上图中server.id中的id。例如:在服务器1中的myid的内容应该写入1

//master 节点
$ cd /home/hadoop/cloud/zookeeper
$ mkdir data
$ cd data
$ vim myid

1

参数说明:*

  • tickTime:心跳时间,毫秒为单位。
  • initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里客户端不是用户连接 Zookeeper服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000=20 秒。
  • syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime时间长度,总时间长度就是 5*2000=10 秒。
  • dataDir:存储内存中数据库快照的位置。
  • clientPort:监听客户端连接的端口
  • server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。

3.5 将文件复制到其它节点

$ scp -r /home/hadoop/cloud/zookeeper-3.4.6 hadoop@slave1:/home/hadoop/cloud/
$ scp -r /home/hadoop/cloud/zookeeper-3.4.6 hadoop@slave1:/home/hadoop/cloud/

3.6 修改对应机器上的myid

$ echo "1" > /home/hadoop/zookeeper/data/myid
$ cat /home/hadoop/zookeeper/data/myid

0x04 测试

在ZooKeeper集群的每个结点上,执行启动ZooKeeper服务的脚本:
各节点上启动 (这里启动顺序为master > slave1 > slave2

//启动
$ [hadoop@master bin]$ zkServer.sh start
$ [hadoop@slave1 bin]$ zkServer.sh start
$ [hadoop@slave2 bin]$ zkServer.sh start

//查看运行状态
$ zkServer.sh status

//停止
$ zkServer.sh stop

使用jps命令查看服务是否启动成功。

$ jps

3024 SecondaryNameNode
3170 ResourceManager
4099 QuorumPeerMain
2841 NameNode
3468 Master
4127 Jps

其中,QuorumPeerMain是zookeeper进程,启动正常。
如上依次启动了所有机器上的Zookeeper之后可以通过ZooKeeper的脚本来查看启动状态,包括集群中各个结点的角色(或是Leader,或是Follower),如下所示,是在ZooKeeper集群中的每个结点上查询的结果:

0x04 参考链接

http://blog.csdn.net/cruise_h/article/details/19046357
https://yq.aliyun.com/articles/36329

0x05 常见问题

5.1 Error contacting service. It is probably not running.

网上看了下'Error contacting service. It is probably not running.'类错误不外乎3种答案:

  1. 配置文件zoo.cfg中的datadir文件夹未创建导致
  2. 防火墙未关闭,建议永久关闭防火墙-->chkconfig iptables of
  3. 修改sh脚本里的一个nc的参数来解决,可在自己的版本中并没有找到nc的调用。-->nc属于老版本,新版本没有了nc

但是,我的都不是上述问题,我的问题是myid文件配置错误。

myid的文件,文件中的内容只有一行,为本主机对应的id值,也就是上图中server.id中的id

2017年1月22日, 星期日
update: 2017-10-18 17:17:00 星期三
修改部分内容,完善排版。

原文地址:https://www.cnblogs.com/ning-wang/p/6416479.html