zookeeper集群搭建

1.由于是第一次搭建zk的集群,过程中遇到些问题,给自己也给遇到问题的人提供一种可能解决问题的方法。

第一步:下载zk的最新版,我下的是3.4.9,在zk的官网,下载后解压到/usr/local

tar -zxvf zookeeper-3.4.9

第二部:在conf 目录下,复制zoo_sample.cfg 另起名zoo.cfg(为什么叫这个名字我也不清楚,应该是zk的程序检测需要这个文件),配置zoo.cfg

mv zoo_sample.cfg /usr/local/zookeeper-3.4.9/conf/zoo.cfg

gedit 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=/tmp/zookeeper
dataLogDir=/tmp/zookeeperLogs
# 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=192.168.154.129:2888:3888
server.2=192.168.154.133:2888:3888
server.3=192.168.154.132:2888:3888

上图中的红色部分是集群需要配置的,

集群中的每台机器都需要感知整个集群是由哪几台机器组成的,在配置文件中,可以按照这样的格式,每行写一个机器配置:server.id=host:port:port. 关于这个id,我们称之为Server ID,标识host机器在集群中的机器序号,在每个ZK机器上,我们需要在数据目录(数据目录就是dataDir参数指定的那个目录)下创建一个myid文件,myid中就是这个Server ID数字。

 

在ZooKeeper的设计中,集群中任意一台机器上的zoo.cfg文件的内容都是一致的。因此最好是用SVN把这个文件管理起来,保证每个机器都能共享到一份相同的配置。

关于myid文件。myid文件中只有一个数字,即一个Server ID。例如,server.1 的myid文件内容就是“1”。注意,请确保每个server的myid文件中id数字不同,并且和server.id=host:port:port中的id一致。另外,id的范围是1~255。

 

 

我就是因为之前不知道  myid 在哪里创建的,导致用bin/zkServer.sh 启动后输入jps 后找不到 zk的启动进程QuorumPeerMain,然后用bin/zkCli.sh -server 192.168.154.129:2181

一直报错:Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect 连接拒绝,就是因为myid 建的位置一定要和 dataDir 指定的目录没匹配上。

 

找到 dataDir指定的/tmp/zookeeper目录,vi myid ,为1

 

配置完后 配置环境变量,gedit /etc/hosts 加上

export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.9

 

 

第三步 :远程复制到另外两台机器,scp zookeeper-3.4.9.tar.gz root@192.168.154.132:/usr/local 输入密码 ,另外一台同样,解压文件,复制zoo.cfg,配置集群,新建dataDirLogs 指定的目录,并vi myid ,每个机器的myid 不同,且值在1-255。

第四部:启动

按照上面的配置后,正常情况下可以启动了 ,进入/usr/local/zookeeper-3.4.9,输入bin/zkServer.sh start  ,输入jps 如图表示启动成功

用bin/zkCli.sh   -server 192.168.154.129:2181 测试 输入如下:

启动成功。另外两台机器一样。

 另外,第二次重启后,本机提示找不到主机的路由,一个是查看/etc/hosts 有没有更改过(碰到过关机断电引起出现.host.swf的情况),再就是防火墙是否关闭;

原文地址:https://www.cnblogs.com/thinkingandworkinghard/p/6133476.html