docker-compose搭建zookeeper集群

配置参考:https://docs.docker.com/compose/compose-file/

                  https://hub.docker.com/_/zookeeper/

1. 环境准备

[root@localhost usr]# docker version
Client:
 Version:           18.09.6
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        481bc77156
 Built:             Sat May  4 02:34:58 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.6
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       481bc77
  Built:            Sat May  4 02:02:43 2019
  OS/Arch:          linux/amd64
  Experimental:     false
[root@localhost usr]# docker-compose -version
docker-compose version 1.25.0-rc1, build 8552e8e2
[root@localhost usr]# 

2. 创建项目目录

/apps/usr/zookeeper  --zookeeper 集群配置、数据均在该目录下

3. 编写docker-compose.yml 文件

[root@localhost zookeeper]# more docker-compose.yml 
version: '3.7'

services:
  zoo1:
    image: zookeeper
    restart: always
    hostname: zoo1
    container_name: zookeeper_1
    #domainname: 
    ports:
      - 2181:2181
    volumes:
      - /apps/usr/zookeeper/zoo1/data:/data
      - /apps/usr/zookeeper/zoo1/datalog:/datalog
    environment:
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181

  zoo2:
    image: zookeeper
    restart: always
    hostname: zoo2
    container_name: zookeeper_2
    ports:
      - 2182:2181
    volumes:
      - /apps/usr/zookeeper/zoo2/data:/data
      - /apps/usr/zookeeper/zoo2/datalog:/datalog
    environment:
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181

  zoo3:
    image: zookeeper
    restart: always
    hostname: zoo3
    container_name: zookeeper_3
    ports:
      - 2183:2181
    volumes:
      - /apps/usr/zookeeper/zoo3/data:/data
      - /apps/usr/zookeeper/zoo3/datalog:/datalog
    environment:
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
// 校验配置文件,不打印
$ docker-compose -f zookeeper-compose.yml config -q

注意:如若需指定配置文件,必须在docker-compose后面指定,不能在config等子命令后面指定,否则无效;如果你使用默认配置文件名称,不需要显式指定-f docker-compose.yml

启动zookeeper集群

// -d 后台启动
[root@localhost zookeeper]# pwd
/apps/usr/zookeeper
[root@localhost zookeeper]# docker-compose -f docker-compose.yml up -d
Creating network "zookeeper_default" with the default driver
Creating zookeeper_2 ... done
Creating zookeeper_1 ... done
Creating zookeeper_3 ... done

查看容器启动情况

[root@localhost zookeeper]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                  NAMES
423084bde76d        zookeeper           "/docker-entrypoint.…"   8 seconds ago       Up 6 seconds        2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2183->2181/tcp   zookeeper_3
6d8ae165a370        zookeeper           "/docker-entrypoint.…"   8 seconds ago       Up 6 seconds        2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2182->2181/tcp   zookeeper_2
f21f07d40204        zookeeper           "/docker-entrypoint.…"   8 seconds ago       Up 6 seconds        2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp   zookeeper_1

查看zookeeper集群状态

[root@localhost zookeeper]# docker exec -it zookeeper_1 /bin/sh
# zkServ.sh status
/bin/sh: 1: zkServ.sh: not found
# ^[[A^[[B^H^C
# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
# exit
[root@localhost zookeeper]# docker exec -it zookeeper_2 /bin/sh
# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
# exit
[root@localhost zookeeper]# docker exec -it zookeeper_3 /bin/sh
# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
# exit

 

你可能感兴趣的:(docker,zookeeper)