zookeeper 安装、配置、使用

来源:互联网 发布:淘宝店铺怎么上传宝贝 编辑:程序博客网 时间:2024/06/05 11:23

zookeeper 安装、配置、使用

使用版本:zookeeper-3.4.9
下载地址:http://apache.org/dist/zookeeper/zookeeper-3.4.9/
官方文档:http://zookeeper.apache.org/doc/r3.4.9/
系统环境:centos6.5

解压:tar -zxf zookeeper-3.4.9.tar.gz

Standalone:

mkdir -p /data/zookeeper/zookeeper

zookeeper-3.4.9/conf 路径下

cp zoo_sample.cfg zoo.cfg

zoo.cfg

tickTime=2000initLimit=10 syncLimit=5dataDir=/data/zookeeper/zookeeperclientPort=2181

zookeeper-3.4.9/bin 路径下

./zkServer.sh start./zkServer.sh status./zkCli.sh -server 127.0.0.1:2181ls /quit./zkServer.sh stop

Cluster (Multi-Server):
mkdir /data/zookeeper/zookeeper1
目录下创建myid文件,文件里写1
mkdir /data/zookeeper/zookeeper2
目录下创建myid文件,文件里写2
mkdir /data/zookeeper/zookeeper2
目录下创建myid文件,文件里写3

zookeeper-3.4.9/conf 路径下(方便起见,公用一套代码只配置不同配置文件···由于我笔记本上给虚拟机分配的硬盘比较小···)

cp zoo_sample.cfg zoo1.cfgcp zoo_sample.cfg zoo2.cfgcp zoo_sample.cfg zoo3.cfg

zoo1.cfg

tickTime=2000initLimit=10 syncLimit=5dataDir=/data/zookeeper/zookeeper1clientPort=2181server.1=127.0.0.1:2888:3888server.2=127.0.0.1:4888:5888server.3=127.0.0.1:6888:7888

zoo2.cfg

tickTime=2000initLimit=10 syncLimit=5dataDir=/data/zookeeper/zookeeper2clientPort=2182server.1=127.0.0.1:2888:3888server.2=127.0.0.1:4888:5888server.3=127.0.0.1:6888:7888

zoo3.cfg

tickTime=2000initLimit=10 syncLimit=5dataDir=/data/zookeeper/zookeeper3clientPort=2183server.1=127.0.0.1:2888:3888server.2=127.0.0.1:4888:5888server.3=127.0.0.1:6888:7888

zookeeper-3.4.9/bin 路径下

./zkServer.sh start zoo1.cfg./zkServer.sh start zoo2.cfg./zkServer.sh start zoo3.cfg./zkServer.sh status zoo1.cfg./zkServer.sh status zoo2.cfg./zkServer.sh status zoo3.cfgjps

连接一个server,添加一个新节点:create /zk_test my_data,在其他两个server上也能看到这个新节点(/zk_test)的存在了

./zkCli.sh -server 127.0.0.1:2181ls /create /zk_test my_datals /get /zk_testquit./zkCli.sh -server 127.0.0.1:2182ls /get /zk_testquit./zkCli.sh -server 127.0.0.1:2183ls /get /zk_testquit

关掉server1,查看server2和server3的状态是否还稳定,这里server2和server3应该还是正常的,只要挂掉的server数量小于集群总数的一半就不会有影响

./zkServer.sh stop zoo1.cfg./zkServer.sh status zoo2.cfg./zkServer.sh status zoo3.cfg

再关掉server2,看server3的状态,发现server3是error的

./zkServer.sh stop zoo2.cfg./zkServer.sh status zoo3.cfg

关掉server3

./zkServer.sh stop zoo3.cfgjps

概述:
zookeeper里面使用的是层次命名空间(the hierarchical namespace),存储的数据就是节点(ZNode);
集群最少3台server,最好数量是奇数,server间通过领导选举(Leader Election)来选出来一个Leader,其它的都为follower


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.


然后复制就是follower与leader的数据要进行同步,具体是follower去请求leader的数据还是leader主动去给follower同步数据这个我并不知道···但是这个同步是有最大心跳间隔的,就是syncLimit参数设置的值

server.1=127.0.0.1:2888:3888server.2=127.0.0.1:4888:5888server.3=127.0.0.1:6888:7888

第一个端口(2888,4888,6888)是用来Leader与follower连接同步数据的,第二个端口(3888,5888,7888)是用来进行领导选举的,在领导挂了的时候~


The new entry, initLimit is timeouts ZooKeeper uses to limit the length of time the ZooKeeper servers in quorum have to connect to a leader. The entry syncLimit limits how far out of date a server can be from a leader.

With both of these timeouts, you specify the unit of time using tickTime. In this example, the timeout for initLimit is 5 ticks at 2000 milleseconds a tick, or 10 seconds.

The entries of the form server.X list the servers that make up the ZooKeeper service. When the server starts up, it knows which server it is by looking for the file myid in the data directory. That file has the contains the server number, in ASCII.

Finally, note the two port numbers after each server name: ” 2888” and “3888”. Peers use the former port to connect to other peers. Such a connection is necessary so that peers can communicate, for example, to agree upon the order of updates. More specifically, a ZooKeeper server uses this port to connect followers to the leader. When a new leader arises, a follower opens a TCP connection to the leader using this port. Because the default leader election also uses TCP, we currently require another port for leader election. This is the second port in the server entry.


关于server.id


You can find the meanings of these and other configuration settings in the section Configuration Parameters. A word though about a few here:
Every machine that is part of the ZooKeeper ensemble should know about every other machine in the ensemble. You accomplish this with the series of lines of the form server.id=host:port:port. The parameters host and port are straightforward. You attribute the server id to each machine by creating a file named myid, one for each server, which resides in that server’s data directory, as specified by the configuration file parameter dataDir.
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.


参数:
tickTime:时间单位,滴答~


the basic time unit in milliseconds used by ZooKeeper. It is used to do heartbeats and the minimum session timeout will be twice the tickTime.


dataDir:数据路径


the location to store the in-memory database snapshots and, unless specified otherwise, the transaction log of updates to the database.


dataLogDir:日志路径,没有配置则写到dataDir路径下,官方强烈建议配个专用的···


This option will direct the machine to write the transaction log to the dataLogDir rather than the dataDir. This allows a dedicated log device to be used, and helps avoid competition between logging and snaphots.
Having a dedicated log device has a large impact on throughput and stable latencies. It is highly recommened to dedicate a log device and set dataLogDir to point to a directory on that device, and then make sure to point dataDir to a directory not residing on that device.


clientPort:监听客户端连接的端口


the port to listen for client connections


initLimit:follower与leader建立tcp连接的限制时间(单位:tickTime)


Amount of time, in ticks (see tickTime), to allow followers to connect and sync to a leader. Increased this value as needed, if the amount of data managed by ZooKeeper is large.


syncLimit:follower与leader同步数据的最大心跳时间(单位:tickTime)


Amount of time, in ticks (see tickTime), to allow followers to sync with ZooKeeper. If followers fall too far behind a leader, they will be dropped.


zkCli命令使用:

ZooKeeper host:port cmd args        get path [watch]        ls path [watch]        set path data [version]        delquota [-n|-b] path        quit        printwatches on|off        createpath data acl        stat path [watch]        listquota path        history        setAcl path acl        getAcl path        sync path        redo cmdno        addauth scheme auth        delete path [version]        setquota -n|-b val path
0 0
原创粉丝点击