一、什么是ZooKeeper?
ZooKeeper(动物园管理员),顾名思义,是用来管理Hadoop(大象)、Hive(蜜蜂)、Pig(小猪)的管理员,同时Apache HBase、Apache Solr、LinkedIn Sensei等众多项目中都采用了ZooKeeper。
ZooKeeper是一个集中式服务,用于维护配置信息、命名、提供分布式同步和提供组服务。所有这些类型的服务都以某种形式被分布式应用程序使用。每次它们被实现时,都有大量的工作用于修复不可避免的bug和竞选条件。由于实现这类服务的困难,应用程序最初通常跳过它们,这使得它们在存在更改时变得脆弱,并且难以管理。即使正确完成,这些服务的不同实现也会在部署应用程序时导致管理复杂性。
二、Zookeeper集群
Kafka使用Zookeeper来存储集群元数据以及消费者元数据。Zookeeper集群被称为群组。Zookeeper使用的是一致性协议,所以建议每个群组里应该包含奇数个节点(比如3个、5个等),因为只有当群组里的大多数节点(也就是法定人数)处于可用状态,Zookeeper才能处理外部的请求。也就是说,如果3个节点的集群,允许1个节点失效。如果5个节点的集群,允许2个节点失效。
三、Docker方式配置ZooKeeper
myid文件:标识要写到快照目录下面myid文件里
采用Docker镜像:wurstmeister/zookeeper,部署一个3节点的集群。
节点一:
- docker-compose.yml
1 |
|
- myid文件
1
1
- zoo.cfg文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36# 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=/opt/zookeeper-3.4.9/data
dataLogDir=/opt/zookeeper-3.4.9/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
#kafka01 (10.100.158.245)
server.1=0.0.0.0:2888:3888
#kafka02 (10.100.19.15)
server.2=10.150.19.15:2888:3888
#kafka03 (10.100.124.217)
server.3=10.100.124.217:2888:3888
节点二:
- docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14version: '2'
services:
zookeeper:
image: wurstmeister/zookeeper
volumes:
- ./zoo.cfg:/opt/zookeeper-3.4.9/conf/zoo.cfg
- ./data:/opt/zookeeper-3.4.9/data
- ./logs:/opt/zookeeper-3.4.9/logs
- ./myid:/opt/zookeeper-3.4.9/data/myid
ports:
- "2180:2181"
- "2888:2888"
- "3888:3888"
restart: always - myid文件
1
2
- zoo.cfg文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36# 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=/opt/zookeeper-3.4.9/data
dataLogDir=/opt/zookeeper-3.4.9/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
#kafka01 (10.100.158.245)
server.1=10.100.158.245:2888:3888
#kafka02 (10.100.19.15)
server.2=0.0.0.0:2888:3888
#kafka03 (10.100.124.217)
server.3=10.100.124.217:2888:3888节点三:
- docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14version: '2'
services:
zookeeper:
image: wurstmeister/zookeeper
volumes:
- ./zoo.cfg:/opt/zookeeper-3.4.9/conf/zoo.cfg
- ./data:/opt/zookeeper-3.4.9/data
- ./logs:/opt/zookeeper-3.4.9/logs
- ./myid:/opt/zookeeper-3.4.9/data/myid
ports:
- "2179:2181"
- "2888:2888"
- "3888:3888"
restart: always - myid文件
1
3
- zoo.cfg文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36# 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=/opt/zookeeper-3.4.9/data
dataLogDir=/opt/zookeeper-3.4.9/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
#kafka01 (10.100.158.245)
server.1=10.100.158.245:2888:3888
#kafka02 (10.100.19.15)
server.2=10.100.19.15:2888:3888
#kafka03 (10.100.124.217)
server.3=0.0.0.0:2888:3888
四、查看ZooKeeper状态
1 |
|
停掉leader,2个follower的状态有变化,如下:
1 |
|
再次查看status:
1 |
|
服务器3变成leader了!
重新启动服务器1,服务器1是follower
1 |
|