ZooKeeper安装和集群配置

一、What is ZooKeeper?

zookeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。

Apache ZooKeeper is an effort to develop and maintain an open-source server which enables highly reliable distributed coordination.

ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them ,which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed.

分布式系统是由一组通过网络进行通信、为了完成共同的任务(一个任务)而协调工作的计算机节点组成的系统。分布式系统的出现是为了用廉价的、普通的机器完成单个计算机无法完成的计算、存储任务。其目的是利用更多的机器,处理更多的数据。



二、单机安装与配置

1.下载zookeeper

wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz

2.解压zookeeper-3.4.11.tar.gz

tar zxvf zookeeper-3.4.6.tar.gz

3.进入conf目录,复制一份配置文件

cd /usr/local/software/zookeeper/zookeeper-3.4.11/conf

cp zoo_sample.cfg zoo.cfg

4.zookeeper的配置文件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=/usr/local/software/zookeeper/zookeeper-3.4.11

#log info
dataLogDir=/usr/local/software/zookeeper/zookeeper-3.4.11/logs

# 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

5.启动、停止、连接

./zkServer.sh start

./zkServer.sh stop

/usr/local/software/zookeeper/zookeeper1/bin/zkCli.sh -server 127.0.0.1:2181

6.Managing ZooKeeper Storage[from http://zookeeper.apache.org/doc/current/zookeeperStarted.html]

For long running production systems ZooKeeper storage must be managed externally (dataDir and logs).
See the section on maintenance for more details.

Connecting to ZooKeeper
$ bin/zkCli.sh -server 127.0.0.1:2181

[zk: 127.0.0.1:2181(CONNECTED) 1] ls /
[zookeeper]

[zk: 127.0.0.1:2181(CONNECTED) 3] help
ZooKeeper -server host:port cmd args
        stat path [watch]
        set path data [version]
        ls path [watch]
        delquota [-n|-b] path
        ls2 path [watch]
        setAcl path acl
        setquota -n|-b val path
        history 
        redo cmdno
        printwatches on|off
        delete path [version]
        sync path
        listquota path
        rmr path
        get path [watch]
        create [-s] [-e] path data acl
        addauth scheme auth
        quit 
        getAcl path
        close 
        connect host:port

[zk: 127.0.0.1:2181(CONNECTED) 4] create /zk_test my_data
Created /zk_test

[zk: 127.0.0.1:2181(CONNECTED) 6] ls /
[zookeeper, zk_test]

[zk: 127.0.0.1:2181(CONNECTED) 7] get /zk_test
my_data
cZxid = 0x7
ctime = Mon Feb 05 23:12:33 PST 2018
mZxid = 0x7
mtime = Mon Feb 05 23:12:33 PST 2018
pZxid = 0x7
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0

#We can change the data associated with zk_test by issuing the set command, as in:

[zk: 127.0.0.1:2181(CONNECTED) 9] set /zk_test junk
cZxid = 0x7
ctime = Mon Feb 05 23:12:33 PST 2018
mZxid = 0x8
mtime = Mon Feb 05 23:14:56 PST 2018
pZxid = 0x7
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0

#Finally, let's delete the node by issuing:
[zk: 127.0.0.1:2181(CONNECTED) 10] delete /zk_test
[zk: 127.0.0.1:2181(CONNECTED) 11] ls /
[zookeeper]


三、集群配置【three servers】

集群模式有两种形式:

1)使用多台机器,在每台机器上运行一个ZooKeeper Server进程;
2)使用一台机器,在该台机器上运行多个ZooKeeper Server进程。
在生产环境中,一般使用第一种形式,在练习环境中,一般使用第二种形式。

我使用第二种方法,在一台机器上运行ZooKeeper Server进程

1.安装三个zookeeper

[root@localhost bin]# ls /usr/local/software/zookeeper/
zookeeper1  zookeeper2  zookeeper3  zookeeper-3.4.11.tar.gz

2.三个zookeeper的zoo.cfg文件

[root@localhost ~]# cat -n /usr/local/software/zookeeper/zookeeper1/conf/zoo.cfg 
     1  # The number of milliseconds of each tick
     2  tickTime=2000
     3  # The number of ticks that the initial 
     4  # synchronization phase can take
     5
     6  initLimit=10
     7  # The number of ticks that can pass between 
     8  # sending a request and getting an acknowledgement
     9
    10  syncLimit=5
    11  # the directory where the snapshot is stored.
    12  # do not use /tmp for storage, /tmp here is just 
    13  # example sakes.
    14
    15  dataDir=/usr/local/software/zookeeper/zookeeper1/data
    16
    17  #log info
    18  dataLogDir=/usr/local/software/zookeeper/zookeeper1/logs
    19
    20  # the port at which the clients will connect
    21  clientPort=2181
    22  # the maximum number of client connections.
    23  # increase this if you need to handle more clients
    24  #maxClientCnxns=60
    25  #
    26  # Be sure to read the maintenance section of the 
    27  # administrator guide before turning on autopurge.
    28  #
    29  # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
    30  keeper-3.4.11
    31  #
    32  # The number of snapshots to retain in dataDir
    33  #autopurge.snapRetainCount=3
    34  # Purge task interval in hours
    35  # Set to "0" to disable auto purge feature
    36  #autopurge.purgeInterval=1
    37
    38  #server.NUM=IP:port1:port2  NUM表示本机为第几号服务器;IP为本机ip地址;
    39  #port1为leader与follower通信端口;port2为参与竞选leader的通信端口
    40  server.1=127.0.0.1:2222:2225
    41  server.2=127.0.0.1:3333:3335
    42  server.3=127.0.0.1:4444:4445
[root@localhost ~]# cat -n /usr/local/software/zookeeper/zookeeper2/conf/zoo.cfg
     1  # The number of milliseconds of each tick
     2  tickTime=2000
     3  # The number of ticks that the initial 
     4  # synchronization phase can take
     5
     6  initLimit=10
     7  # The number of ticks that can pass between 
     8  # sending a request and getting an acknowledgement
     9
    10  syncLimit=5
    11  # the directory where the snapshot is stored.
    12  # do not use /tmp for storage, /tmp here is just 
    13  # example sakes.
    14
    15  dataDir=/usr/local/software/zookeeper/zookeeper2/data
    16
    17  #log info
    18  dataLogDir=/usr/local/software/zookeeper/zookeeper2/logs
    19
    20  # the port at which the clients will connect
    21  clientPort=2182
    22  # the maximum number of client connections.
    23  # increase this if you need to handle more clients
    24  #maxClientCnxns=60
    25  #
    26  # Be sure to read the maintenance section of the 
    27  # administrator guide before turning on autopurge.
    28  #
    29  # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
    30  #
    31  # The number of snapshots to retain in dataDir
    32  #autopurge.snapRetainCount=3
    33  # Purge task interval in hours
    34  # Set to "0" to disable auto purge feature
    35  #autopurge.purgeInterval=1
    36
    37  #server.NUM=IP:port1:port2  NUM表示本机为第几号服务器;IP为本机ip地址;
    38  #port1为leader与follower通信端口;port2为参与竞选leader的通信端口
    39  server.1=127.0.0.1:2222:2225
    40  server.2=127.0.0.1:3333:3335
    41  server.3=127.0.0.1:4444:4445

[root@localhost ~]# cat -n /usr/local/software/zookeeper/zookeeper3/conf/zoo.cfg
     1  # The number of milliseconds of each tick
     2  tickTime=2000
     3  # The number of ticks that the initial 
     4  # synchronization phase can take
     5
     6  initLimit=10
     7  # The number of ticks that can pass between 
     8  # sending a request and getting an acknowledgement
     9
    10  syncLimit=5
    11  # the directory where the snapshot is stored.
    12  # do not use /tmp for storage, /tmp here is just 
    13  # example sakes.
    14
    15  dataDir=/usr/local/software/zookeeper/zookeeper3/data
    16
    17  #log info
    18  dataLogDir=/usr/local/software/zookeeper/zookeeper3/logs
    19
    20  # the port at which the clients will connect
    21  clientPort=2183
    22  # the maximum number of client connections.
    23  # increase this if you need to handle more clients
    24  #maxClientCnxns=60
    25  #
    26  # Be sure to read the maintenance section of the 
    27  # administrator guide before turning on autopurge.
    28  #
    29  # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
    30  #
    31  # The number of snapshots to retain in dataDir
    32  #autopurge.snapRetainCount=3
    33  # Purge task interval in hours
    34  # Set to "0" to disable auto purge feature
    35  #autopurge.purgeInterval=1
    36
    37  #server.NUM=IP:port1:port2  NUM表示本机为第几号服务器;IP为本机ip地址;
    38  #port1为leader与follower通信端口;port2为参与竞选leader的通信端口
    39  server.1=127.0.0.1:2222:2225
    40  server.2=127.0.0.1:3333:3335
    41  server.3=127.0.0.1:4444:4445


3.添加myid文件

在zookeeper的data目录下添加myid文件,用于存储一个数值,用来作为该ZooKeeper Server进程的标识。即上面配置中的NUM。
[root@localhost data]# cat /usr/local/software/zookeeper/zookeeper1/data/myid 
1

[root@localhost data]# cat /usr/local/software/zookeeper/zookeeper2/data/myid 
2

[root@localhost ~]# cat /usr/local/software/zookeeper/zookeeper3/data/myid 
3


4.启动三个zookeeper

[root@localhost ~]# /usr/local/software/zookeeper/zookeeper1/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/software/zookeeper/zookeeper1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

[root@localhost ~]# /usr/local/software/zookeeper/zookeeper2/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/software/zookeeper/zookeeper2/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

[root@localhost ~]# /usr/local/software/zookeeper/zookeeper3/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/software/zookeeper/zookeeper3/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

5.查看三个zookeeper的状态

[root@localhost ~]#  /usr/local/software/zookeeper/zookeeper1/bin/zkServer.sh  status
ZooKeeper JMX enabled by default
Using config: /usr/local/software/zookeeper/zookeeper1/bin/../conf/zoo.cfg
Mode: follower
[root@localhost ~]#  /usr/local/software/zookeeper/zookeeper2/bin/zkServer.sh  status
ZooKeeper JMX enabled by default
Using config: /usr/local/software/zookeeper/zookeeper2/bin/../conf/zoo.cfg
Mode: leader
[root@localhost ~]#  /usr/local/software/zookeeper/zookeeper3/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/software/zookeeper/zookeeper3/bin/../conf/zoo.cfg
Mode: follower


6.三个zookeeper集群

配置多个实例共同构成一个集群对外提供服务以达到水平扩展的目的,每个服务器上的数据是相同的,每一个服务器均可以对外提供读和写的服务,这点和redis是相同的,即对客户端来讲每个服务器都是平等的。zookeeper选举机制详看https://www.cnblogs.com/ASPNET2008/p/6421571.html,zookeeper集群通常有三种角色:Leader,Follower,Observer。





猜你喜欢

转载自blog.csdn.net/thebigdipperbdx/article/details/79270332