概述
Zookeeper 部署有三种方式,单机模式、集群模式、伪集群模式,以下采用 Docker 的方式部署
注意: 集群为大于等于3个奇数,如 3、5、7,不宜太多,集群机器多了选举和数据同步耗时长,不稳定。
单机模式
docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12
| version: '3.1'
services: zoo1: image: zookeeper restart: always hostname: zoo1 ports: - 2181:2181 environment: ZOO_MY_ID: 1 ZOO_SERVERS: server.1=zoo1:2888:3888
|
验证是否安装成功
1
| docker exec -it zookeeper_zoo1_1 /bin/bash
|
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
| bash-4.3# ./bin/zkCli.sh -server 192.168.75.130:2181 Connecting to 192.168.75.130:2181 2017-11-09 07:45:58,365 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT 2017-11-09 07:45:58,374 [myid:] - INFO [main:Environment@100] - Client environment:host.name=zoo1 2017-11-09 07:45:58,374 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_131 2017-11-09 07:45:58,380 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation 2017-11-09 07:45:58,381 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-1.8-openjdk/jre 2017-11-09 07:45:58,381 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/zookeeper-3.4.10/bin/../build/classes:/zookeeper-3.4.10/bin/../build/lib
|
1 2 3 4
| bash-4.3# ./bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Mode: standalone
|
集群模式
准备 3 台 Ubuntu Server 系统,并分别配置 Zookeeper
第一台主机
docker-compose.yml
1 2 3 4 5 6 7 8 9
| version: '3.1' services: zoo1: image: zookeeper restart: always environment: ZOO_MY_ID: 1 ZOO_SERVERS: server.1=192.168.75.130:2888:3888 server.2=192.168.75.134:2888:3888 server.3=192.168.75.135:2888:3888 network_mode: host
|
验证测试
1 2 3 4 5
| root@UbuntuBase:/usr/local/docker/zookeeper bash-4.3 ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Mode: leader
|
第二台主机
docker-compose.yml
1 2 3 4 5 6 7 8 9
| version: '3.1' services: zoo2: image: zookeeper restart: always environment: ZOO_MY_ID: 2 ZOO_SERVERS: server.1=192.168.75.130:2888:3888 server.2=192.168.75.134:2888:3888 server.3=192.168.75.135:2888:3888 network_mode: host
|
验证测试
1 2 3 4 5
| root@UbuntuBase:/usr/local/docker/zookeeper bash-4.3 ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Mode: follower
|
第三台主机
docker-compose.yml
1 2 3 4 5 6 7 8 9
| version: '3.1' services: zoo3: image: zookeeper restart: always environment: ZOO_MY_ID: 3 ZOO_SERVERS: server.1=192.168.75.130:2888:3888 server.2=192.168.75.134:2888:3888 server.3=192.168.75.135:2888:3888 network_mode: host
|
验证测试
1 2 3 4 5
| root@UbuntuBase:/usr/local/docker/zookeeper bash-4.3 ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Mode: follower
|
伪集群模式
docker-compose.yml
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
| version: '3.1' services: zoo1: image: zookeeper restart: always hostname: zoo1 ports: - 2181:2181 environment: ZOO_MY_ID: 1 ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
zoo2: image: zookeeper restart: always hostname: zoo2 ports: - 2182:2181 environment: ZOO_MY_ID: 2 ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
zoo3: image: zookeeper restart: always hostname: zoo3 ports: - 2183:2181 environment: ZOO_MY_ID: 3 ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
|
验证是否安装成功
1
| docker exec -it zookeeper_zoo1_1 /bin/bash
|
1
| docker exec -it zookeeper_zoo2_1 /bin/bash
|
1
| docker exec -it zookeeper_zoo3_1 /bin/bash
|
1 2 3 4 5
| root@UbuntuBase:/usr/local/docker/zookeeper bash-4.3 ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Mode: follower
|
1 2 3 4 5
| root@UbuntuBase:/usr/local/docker/zookeeper bash-4.3 ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Mode: follower
|
1 2 3 4 5
| root@UbuntuBase:/usr/local/docker/zookeeper bash-4.3 ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Mode: leader
|
从上面的验证结果可以看出:zoo1 为跟随者,zoo2 为跟随者,zoo3 为领导者