Zookeeper部署

1、概述

  • Zookeeper是一个开源的分布式的,为分布式框架提供协调服务的Apache项目。

  • 工作机制:

    • Zookeeper从设计模式角度来理解,是一个基于观察者模式设计的分布式服务管理框架,它负责存储和管理大家都关心的数据,然后接受观察者的注册,一旦这些数据的状态发生变化,Zookeeper就将负责通知已经在Zookeeper上注册的那些观察者做出相应的反应
{% asset_img 1632208665503.png 1632208665503 %}
  • 特点:

    • Zookeeper:一个领导者(Leader),多个跟随者(Follower)组成的集群。
    • 集群中只要有半数以上节点存活,Zookeeper集群就能正常服务。所以Zookeeper适合安装奇数台服务器。
    • 全局数据一致:每个Server保存一份相同的数据副本,Client无论连接到哪个Server,数据都是一致的。
    • 更新请求顺序执行,来自同一个Client的更新请求按其发送顺序依次执行。
    • 数据更新原子性,一次数据更新要么成功,要么失败。
    • 实时性,在一定时间范围内,Client能读到最新数据。
  • 数据结构:

    • ZooKeeper数据模型的结构与Unix文件系统很类似,整体上可以看作是一棵树,每个节点称做一个ZNode。每一个ZNode默认能够存储1MB的数据,每个ZNode都可以通过其路径唯一标识。

  • 应用场景:

    • 提供的服务包括:统一命名服务、统一配置管理、统一集群管理、服务器节点动态上下 线、软负载均衡等。

      • ①统一命名服务。 在分布式环境下,经常需要对应用/服 务进行统一命名,便于识别。例如:IP不容易记住,而域名容易记住。

        1632209246320
      • ②统一配置管理。

        • 分布式环境下,配置文件同步非常常见。
          • 一般要求一个集群中,所有节点的配置信息是一致的,比如 Kafka 集群。
          • 对配置文件修改后,希望能够快速同步到各个节点上。
        • 配置管理可交由ZooKeeper实现。
          • 可将配置信息写入ZooKeeper上的一个Znode。
          • 各个客户端服务器监听这个Znode。
          • 一旦Znode中的数据被修改,ZooKeeper将通知各个客户端服务器。
        1632209823959
      • ③统一集群管理。

        • 分布式环境中,实时掌握每个节点的状态是必要的,可根据节点实时状态做出一些调整。
        • ZooKeeper可以实现实时监控节点状态变化。
          • 可将节点信息写入ZooKeeper上的一个ZNode。
          • 监听这个ZNode可获取它的实时状态变化。
        1632210372870
      • ④服务器动态上下线。 客户端能实时洞察到服务 器上下线的变化。

      • ⑤软负载均衡。 在Zookeeper中记录每台服务器的访问数,让访问数最少的服务器去处理最新的客户端请求

2、部署

2.1 docker单机部署

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
[root@iz2zecc4mo4hoc75p0frn8z ~]# docker pull zookeeper:3.5.7
3.5.7: Pulling from library/zookeeper
afb6ec6fdc1c: Pull complete
ee19e84e8bd1: Pull complete
6ac787417531: Pull complete
f3f781d4d83e: Pull complete
424c9e43d19a: Pull complete
f0929561e8a7: Pull complete
f1cf0c087cb3: Pull complete
2f47bb4dd07a: Pull complete
Digest: sha256:883b014b6535574503bda8fc6a7430ba009c0273242f86d401095689652e5731
Status: Downloaded newer image for zookeeper:3.5.7
[root@iz2zecc4mo4hoc75p0frn8z ~]# docker run -d -p 2181:2181 --name single-zookeeper zookeeper:3.5.7
6f0444fa75ce40fa63450d05ec9410954f85e6e2cc14dadccc978b899f2b5a1d
[root@iz2zecc4mo4hoc75p0frn8z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f0444fa75ce zookeeper:3.5.7 "/docker-entrypoint.…" 17 seconds ago Up 16 seconds 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp single-zookeeper
[root@iz2zecc4mo4hoc75p0frn8z ~]# docker exec -it single-zookeeper /bin/bash
root@6f0444fa75ce:/apache-zookeeper-3.5.7-bin# ./bin/zkCli.sh
Connecting to localhost:2181
2021-09-21 08:16:04,214 [myid:] - INFO [main:Environment@109] - Client environment:zookeeper.version=3.5.7-f0fdd52973d373ffd9c86b81d99842dc2c7f660e, built on 02/10/2020 11:30 GMT
2021-09-21 08:16:04,218 [myid:] - INFO [main:Environment@109] - Client environment:host.name=6f0444fa75ce
2021-09-21 08:16:04,218 [myid:] - INFO [main:Environment@109] - Client environment:java.version=11.0.7
2021-09-21 08:16:04,228 [myid:] - INFO [main:Environment@109] - Client environment:java.vendor=Oracle Corporation
2021-09-21 08:16:04,228 [myid:] - INFO [main:Environment@109] - Client environment:java.home=/usr/local/openjdk-11
2021-09-21 08:16:04,228 [myid:] - INFO [main:Environment@109] - Client environment:java.class.path=/apache-zookeeper-3.5.7-bin/bin/../zookeeper-server/target/classes:/apache-zookeeper-3.5.7-bin/bin/../build/classes:/apache-zookeeper-3.5.7-bin/bin/../zookeeper-server/target/lib/*.jar:/apache-zookeeper-3.5.7-bin/bin/../build/lib/*.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/zookeeper-jute-3.5.7.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/zookeeper-3.5.7.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/slf4j-log4j12-1.7.25.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/slf4j-api-1.7.25.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/netty-transport-native-unix-common-4.1.45.Final.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/netty-transport-native-epoll-4.1.45.Final.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/netty-transport-4.1.45.Final.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/netty-resolver-4.1.45.Final.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/netty-handler-4.1.45.Final.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/netty-common-4.1.45.Final.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/netty-codec-4.1.45.Final.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/netty-buffer-4.1.45.Final.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/log4j-1.2.17.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/json-simple-1.1.1.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/jline-2.11.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/jetty-util-9.4.24.v20191120.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/jetty-servlet-9.4.24.v20191120.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/jetty-server-9.4.24.v20191120.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/jetty-security-9.4.24.v20191120.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/jetty-io-9.4.24.v20191120.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/jetty-http-9.4.24.v20191120.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/javax.servlet-api-3.1.0.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/jackson-databind-2.9.10.2.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/jackson-core-2.9.10.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/jackson-annotations-2.9.10.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/commons-cli-1.2.jar:/apache-zookeeper-3.5.7-bin/bin/../lib/audience-annotations-0.5.0.jar:/apache-zookeeper-3.5.7-bin/bin/../zookeeper-*.jar:/apache-zookeeper-3.5.7-bin/bin/../zookeeper-server/src/main/resources/lib/*.jar:/conf:
2021-09-21 08:16:04,228 [myid:] - INFO [main:Environment@109] - Client environment:java.library.path=/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib
2021-09-21 08:16:04,228 [myid:] - INFO [main:Environment@109] - Client environment:java.io.tmpdir=/tmp
2021-09-21 08:16:04,229 [myid:] - INFO [main:Environment@109] - Client environment:java.compiler=<NA>
2021-09-21 08:16:04,229 [myid:] - INFO [main:Environment@109] - Client environment:os.name=Linux
2021-09-21 08:16:04,229 [myid:] - INFO [main:Environment@109] - Client environment:os.arch=amd64
2021-09-21 08:16:04,229 [myid:] - INFO [main:Environment@109] - Client environment:os.version=3.10.0-514.26.2.el7.x86_64
2021-09-21 08:16:04,229 [myid:] - INFO [main:Environment@109] - Client environment:user.name=root
2021-09-21 08:16:04,229 [myid:] - INFO [main:Environment@109] - Client environment:user.home=/root
2021-09-21 08:16:04,229 [myid:] - INFO [main:Environment@109] - Client environment:user.dir=/apache-zookeeper-3.5.7-bin
2021-09-21 08:16:04,229 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.free=22MB
2021-09-21 08:16:04,236 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.max=247MB
2021-09-21 08:16:04,236 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.total=29MB
2021-09-21 08:16:04,242 [myid:] - INFO [main:ZooKeeper@868] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@6e2c9341
2021-09-21 08:16:04,250 [myid:] - INFO [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-09-21 08:16:04,263 [myid:] - INFO [main:ClientCnxnSocket@237] - jute.maxbuffer value is 4194304 Bytes
2021-09-21 08:16:04,281 [myid:] - INFO [main:ClientCnxn@1653] - zookeeper.request.timeout value is 0. feature enabled=
Welcome to ZooKeeper!
2021-09-21 08:16:04,331 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1112] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2021-09-21 08:16:04,354 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@959] - Socket connection established, initiating session, client: /127.0.0.1:58356, server: localhost/127.0.0.1:2181
2021-09-21 08:16:04,447 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1394] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x106a99619880000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] quit

WATCHER::

WatchedEvent state:Closed type:None path:null
2021-09-21 08:18:36,472 [myid:] - INFO [main:ZooKeeper@1422] - Session: 0x106a99619880000 closed
2021-09-21 08:18:36,472 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down for session: 0x106a99619880000
root@6f0444fa75ce:/apache-zookeeper-3.5.7-bin# ls
LICENSE.txt NOTICE.txt README.md README_packaging.txt bin conf docs lib
root@6f0444fa75ce:/apache-zookeeper-3.5.7-bin# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone
  • 配置文件zoo.cfg说明:

    • ①tickTime=2000:通信心跳时间,Zookeeper服务器与客户端心跳时间,单位毫秒。

    • ②initLimit=10:LF初始通信时限。

      • Leader和Follower初始连接时能容忍的最多心跳数(tickTime的数量)
    • ③syncLimit=5:LF同步通信时限。

      • Leader和Follower之间通信时间如果超过syncLimit * tickTime,Leader认为Follwer死掉,从服务器列表中删除Follwer。
    • ④dataDir=/tmp/zookeeper:保存Zookeeper中的数据。(默认的tmp目录,容易被Linux系统定期删除,所以一般不用默认的tmp目录)

    • ⑤clientPort=2181:客户端连接端口,通常不做修改。

2.2 docker集群部署

  • 本案例是搭建一主二从的Zookeeper集群,所以要启动三个Zookeeper容器,由于这里使用桥接模式,所以需要虚拟一个自定义bridge网络,然后创建容器的时候指定ip,这样才能使得三个容器能够相互通信,具体有关docker网络模式的原理可参考:https://perfectcode.top/2021/04/23/docker%E7%BD%91%E7%BB%9C/

    • ①在本地创建目录。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      [root@iz2zecc4mo4hoc75p0frn8z ~]# mkdir /usr/local/zookeeper-cluster
      [root@iz2zecc4mo4hoc75p0frn8z ~]# mkdir /usr/local/zookeeper-cluster/node1
      [root@iz2zecc4mo4hoc75p0frn8z ~]# mkdir /usr/local/zookeeper-cluster/node2
      [root@iz2zecc4mo4hoc75p0frn8z ~]# mkdir /usr/local/zookeeper-cluster/node3
      [root@iz2zecc4mo4hoc75p0frn8z ~]# ll /usr/local/zookeeper-cluster/
      total 12
      drwxr-xr-x 2 root root 4096 Sep 21 20:40 node1
      drwxr-xr-x 2 root root 4096 Sep 21 20:40 node2
      drwxr-xr-x 2 root root 4096 Sep 21 20:40 node3
    • ②创建docker自定义bridge网络。

      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
      37
      38
      39
      [root@iz2zecc4mo4hoc75p0frn8z ~]# docker network create --driver bridge --subnet=172.18.0.0/16 --gateway=172.18.0.1 zoonet
      37297b2c4502835e622c4f9748b65c9bc5678cb67199d237c03c98a62670aca2
      [root@iz2zecc4mo4hoc75p0frn8z ~]# docker network ls
      NETWORK ID NAME DRIVER SCOPE
      b57513a4a23c bridge bridge local
      5bbd0c03d622 host host local
      463914162c9f none null local
      37297b2c4502 zoonet bridge local
      [root@iz2zecc4mo4hoc75p0frn8z ~]# docker network inspect 37297b2c4502
      [
      {
      "Name": "zoonet",
      "Id": "37297b2c4502835e622c4f9748b65c9bc5678cb67199d237c03c98a62670aca2",
      "Created": "2021-09-21T20:35:14.7142266+08:00",
      "Scope": "local",
      "Driver": "bridge",
      "EnableIPv6": false,
      "IPAM": {
      "Driver": "default",
      "Options": {},
      "Config": [
      {
      "Subnet": "172.18.0.0/16",
      "Gateway": "172.18.0.1"
      }
      ]
      },
      "Internal": false,
      "Attachable": false,
      "Ingress": false,
      "ConfigFrom": {
      "Network": ""
      },
      "ConfigOnly": false,
      "Containers": {},
      "Options": {},
      "Labels": {}
      }
      ]
    • ③创建三个Zookeeper容器,并使用刚创建的自定义网络zoonet。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      [root@iz2zecc4mo4hoc75p0frn8z ~]# docker images
      REPOSITORY TAG IMAGE ID CREATED SIZE
      zookeeper 3.5.7 6bd990489b09 16 months ago 245MB
      java 8 d23bdf5b1b1b 4 years ago 643MB
      [root@iz2zecc4mo4hoc75p0frn8z ~]# docker run -d -p 2181:2181 --name zookeeper_node1 --privileged --network zoonet --ip 172.18.0.2 -v /usr/local/zookeeper-cluster/node1/volumes/data:/data -v /usr/local/zookeeper-cluster/node1/volumes/datalog:/datalog -v /usr/local/zookeeper-cluster/node1/volumes/logs:/logs -e ZOO_MY_ID=1 -e "ZOO_SERVERS=server.1=172.18.0.2:2888:3888;2181 server.2=172.18.0.3:2888:3888;2181 server.3=172.18.0.4:2888:3888;2181" 6bd990489b09
      df11b76dc4fed4c254f29ed575cf42625950e4d36faf25e11e7db366d91016bd
      [root@iz2zecc4mo4hoc75p0frn8z ~]# docker run -d -p 2182:2181 --name zookeeper_node2 --privileged --network zoonet --ip 172.18.0.3 -v /usr/local/zookeeper-cluster/node2/volumes/data:/data -v /usr/local/zookeeper-cluster/node2/volumes/datalog:/datalog -v /usr/local/zookeeper-cluster/node2/volumes/logs:/logs -e ZOO_MY_ID=2 -e "ZOO_SERVERS=server.1=172.18.0.2:2888:3888;2181 server.2=172.18.0.3:2888:3888;2181 server.3=172.18.0.4:2888:3888;2181" 6bd990489b09
      0b443ec0cb28fd1497ebfc1f3cd7642d8f174dbde8e3ab4dab9963b37dd2d543
      [root@iz2zecc4mo4hoc75p0frn8z ~]# docker run -d -p 2183:2181 --name zookeeper_node3 --privileged --network zoonet --ip 172.18.0.4 -v /usr/local/zookeeper-cluster/node3/volumes/data:/data -v /usr/local/zookeeper-cluster/node3/volumes/datalog:/datalog -v /usr/local/zookeeper-cluster/node3/volumes/logs:/logs -e ZOO_MY_ID=3 -e "ZOO_SERVERS=server.1=172.18.0.2:2888:3888;2181 server.2=172.18.0.3:2888:3888;2181 server.3=172.18.0.4:2888:3888;2181" 6bd990489b09
      04115c6f70d2e6ac683f81554d16c89427d17f5dfb39340ebee43dc7cc825f20
      [root@iz2zecc4mo4hoc75p0frn8z ~]# docker ps
      CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
      04115c6f70d2 6bd990489b09 "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2183->2181/tcp zookeeper_node3
      0b443ec0cb28 6bd990489b09 "/docker-entrypoint.…" About a minute ago Up About a minute 2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2182->2181/tcp zookeeper_node2
      df11b76dc4fe 6bd990489b09 "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp zookeeper_node1
      • 配置参数解读(server.A=B:C:D):
        • A是一个数字,表示这个是第几号服务器。
          • 集群模式下配置一个文件myid,这个文件在dataDir目录下,这个文件里面有一个数据就是 A的值,Zookeeper启动时读取此文件,拿到里面的数据与zoo.cfg里面的配置信息比较从而判断到底是哪个server。
        • B是这个服务器的地址。
        • C是这个服务器Follower与集群中的Leader服务器交换信息的端口。
        • D是万一集群中的Leader服务器挂了,需要一个端口来重新进行选举,选出一个新的Leader,而这个端口就是用来执行选举时服务器相互通信的端口。
    • ④进入容器内部验证主从是否搭建成功。

      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
      [root@iz2zecc4mo4hoc75p0frn8z ~]# docker exec -it zookeeper_node1 /bin/bash
      root@df11b76dc4fe:/apache-zookeeper-3.5.7-bin# ./bin/zkServer.sh status
      ZooKeeper JMX enabled by default
      Using config: /conf/zoo.cfg
      Client port found: 2181. Client address: localhost.
      Mode: follower # 从机
      root@df11b76dc4fe:/apache-zookeeper-3.5.7-bin# exit
      exit
      [root@iz2zecc4mo4hoc75p0frn8z ~]# docker exec -it zookeeper_node2 /bin/bash
      root@0b443ec0cb28:/apache-zookeeper-3.5.7-bin# ./bin/zkServer.sh status
      ZooKeeper JMX enabled by default
      Using config: /conf/zoo.cfg
      Client port found: 2181. Client address: localhost.
      Mode: leader # 主机
      root@0b443ec0cb28:/apache-zookeeper-3.5.7-bin# exit
      exit
      [root@iz2zecc4mo4hoc75p0frn8z ~]# docker exec -it zookeeper_node3 /bin/bash
      root@04115c6f70d2:/apache-zookeeper-3.5.7-bin# ./bin/zkServer.sh status
      ZooKeeper JMX enabled by default
      Using config: /conf/zoo.cfg
      Client port found: 2181. Client address: localhost.
      Mode: follower # 从机
      root@04115c6f70d2:/apache-zookeeper-3.5.7-bin# exit
      exit
      [root@iz2zecc4mo4hoc75p0frn8z ~]#