使用 docker-compose 在本地搭建一个测试用的zookeeper集群, 顺便带个web页面方便查看

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
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
version: '3'

services:
  zoo-web:
    image: tobilg/zookeeper-webui
    container_name: zk-web
    ports:
      - "8080:8080"
    networks:
      - frontend
    environment:
      ZK_DEFAULT_NODE: zk1:2181
      USER: admin
      PASSWORD: 123123
      HTTP_PORT: 8080
    depends_on: 
      - zoo1
  zoo1:
    image: zookeeper:latest
    container_name: zk1
    restart: always
    ports:
      - "2181:2181"
    networks:
      - frontend
    environment:
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=zk1:2888:3888;2181 server.2=zk2:2888:3888;2181 server.3=zk3:2888:3888;2181
  zoo2:
    image: zookeeper:latest
    container_name: zk2
    restart: always
    ports:
      - "2182:2181"
    networks:
      - frontend
    environment:
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zk1:2888:3888;2181 server.2=zk2:2888:3888;2181 server.3=zk3:2888:3888;2181
    depends_on: 
      - zoo1
  zoo3:
    image: zookeeper:latest
    container_name: zk3
    restart: always
    ports:
      - "2183:2181"
    networks:
      - frontend
    environment:
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zk1:2888:3888;2181 server.2=zk2:2888:3888;2181 server.3=zk3:2888:3888;2181
    depends_on: 
      - zoo1

    
networks:
  frontend:
    driver: bridge

运行 docker-compose up 执行即可

web容器默认绑定本地8080端口, 访问指定server, 则需要使用容器名, 例如 zk1:2181, zk2:2181

在宿主机访问时, 因为已经绑定了宿主机端口, 使用 0.0.0.0:2181,0.0.0.0:2182,0.0.0.0:2183就行

这个配置在本地测试够用了, 如果要做线上使用, 需要参考官方文档调一下参数了