您的位置:首页 > 其它

ZooKeeper学习笔记:初识zookeeper

2017-08-16 20:10 381 查看

前言

ZooKeeper is a distributed, open-source coordination service for distributed applications. It exposes a simple set of primitives that distributed applications can build upon to implement
higher level services for synchronization, configuration maintenance, and groups and naming.

Coordination services are notoriously hard to get right. They are especially prone to errors such as race conditions
and deadlock. The motivation behind ZooKeeper is to relieve distributed applications the responsibility of implementing coordination services from scratch.

参考:http://zookeeper.apache.org/doc/r3.4.10/zookeeperStarted.html#ch_GettingStarted

           http://zookeeper.apache.org/doc/r3.4.10/zookeeperAdmin.html#sc_systemReq

安装JDK

下载安装zookeeper稳定版本

sudo cp zookeeper-3.4.10.tar.gz /usr/DevProgram/
cd /usr/DevProgram/
tar -vxzf zookeeper-3.4.10.tar.gz
rm -f zookeeper-3.4.10.tar.gz


把zookeeper复制到从机上:

scp -r /usr/DevProgram/zookeeper-3.4.10 daya@slave1:/usr/DevProgram/
scp -r /usr/DevProgram/zookeeper-3.4.10 daya@slave2:/usr/DevProgram/


修改环境变量,分别在文件中与PATH最后加入:

ZOOKEEPER_HOME=/usr/DevProgram/zookeeper-3.4.10
/usr/DevProgram/zookeeper-3.4.10/bin


更新:

source /etc/environment


独立模式

新建配置文件:

cd /usr/DevProgram/zookeeper-3.4.10/conf/
cp zoo_sample.cfg zoo.cfg
确保文件里有以下值:

# The number of milliseconds of each tick
tickTime=2000
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just example sakes.
dataDir=home/daya/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181


Replicated模式

(For replicated mode, a minimum of three servers are required, and it is strongly recommended that you have an odd number of servers. If you only have two servers, then you are in a situation where if one of them fails, there are not enough machines to form
a majority quorum. Two servers is inherently less stable than a single server, because there are two single points of failure.)

我们再以从机为模本复制一台虚拟机出来,修改好集群的hostname与hosts,以及hadoop与hbase相关的配置文件。

计划把三台slave从机配置成zookeeper的服务器,首先远程连接到slave1上操作。

在~/tmp/下新建名为zookeeper的文件夹:



然后在其下新建名为myid的文件,内容写入数字“1”,意为第一台从机。

(The myid file consists of a single line containing only the text of that machine's id. So myid of server 1 would contain the text "1" and nothing else. The id must be unique within the ensemble and should have a value between 1 and 255.)

修改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
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
#maxClientCnxns=60
# 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=slave1:2888:3888
server.2=slave2:2888:3888
server.3=slave3:2888:3888

(注意两个端口,Peers use the former port to connect to other peers,we currently require another port for leader election.)

slave2与slave3从机需要做同样的配置(myid与zoo.cfg)。

此时要注意在三台slave从机上修改环境变量,使得能够在任意目录启动zookeeper。

配置完成之后,可以在master端口远程连接依次启动:

ssh slave1
zkServer.sh start
exit
ssh slave2
zkServer.sh start
exit
ssh slave3
zkServer.sh start
exit
登录到从机可以使用命令查看服务运行状态:

zkServer.sh status
可以看到slave2是作为leader,而slave1与slave3是作为follower:





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: