准备zookeeper镜像
- 这里使用官方提供的镜像,版本为3.4.11
- docker-compose分布式管理组件,具体安装以及使用方法见github项目
准备docker-compose.yml
文件
这里要注意下
yml
语法,key冒号后面有空格
- 这里采用官方镜像提供默认配置,可根据docker-compose配置说明做调整
version: '3.1'
services:
zoo1:
image: zookeeper:3.4.11
restart: always
hostname: zoo1
container_name: zookeeper_1
#domainname:
ports:
- 2181:2181
volumes:
- /usr/local/docker_app/zookeeper/zoo1/data:/data
- /usr/local/docker_app/zookeeper/zoo1/datalog:/datalog
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:3.4.11
restart: always
hostname: zoo2
container_name: zookeeper_2
ports:
- 2182:2181
volumes:
- /usr/local/docker_app/zookeeper/zoo2/data:/data
- /usr/local/docker_app/zookeeper/zoo2/datalog:/datalog
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:3.4.11
restart: always
hostname: zoo3
container_name: zookeeper_3
ports:
- 2183:2181
volumes:
- /usr/local/docker_app/zookeeper/zoo3/data:/data
- /usr/local/docker_app/zookeeper/zoo3/datalog:/datalog
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
-
docker-compse
命令提供了一系列子命令
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file (default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name (default: directory name)
--verbose Show more output
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the name specified
in the client certificate (for example if your docker host
is an IP address)
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
- 校验指定配置文件正确性,默认配置文件名称
docker-compose.yml
或docker-compose.yml
,我这里使用自定义名称zookeeper-compose.yml
注意:如若需指定配置文件,必须在
docker-compose
后面指定,不能在config
等子命令后面指定,否则无效;如果你使用默认配置文件名称,不需要显式指定-f docker-compose.yml
// 查看config命令参数
$ docker-compose -f zookeeper-compose.yml config --help
// 校验配置文件,不打印
$ docker-compose -f zookeeper-compose.yml config -q
启动zookeeper集群
// -d 后台启动
$ docker-compose -f zookeeper-compose.yml up -d
- 查看容器启动情况
$ docker-compose -f zookeeper-compose.yml ps
Name Command State Ports
-------------------------------------------------------------------------------------------------
zookeeper_1 /docker-entrypoint.sh zkSe ... Up 0.0.0.0:2181->2181/tcp, 2888/tcp, 3888/tcp
zookeeper_2 /docker-entrypoint.sh zkSe ... Up 0.0.0.0:2182->2181/tcp, 2888/tcp, 3888/tcp
zookeeper_3 /docker-entrypoint.sh zkSe ... Up 0.0.0.0:2183->2181/tcp, 2888/tcp, 3888/tcp
查看zookeeper集群状态
zoo1
$ docker exec -it zookeeper_1 /bin/sh
/zookeeper-3.4.11 # zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Mode: follower // 这是个follower
zoo2
$ docker exec -it zookeeper_2 /bin/sh
/zookeeper-3.4.11 # zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Mode: leader // 这是个leader
zoo3
$ docker exec -it zookeeper_3 /bin/sh
/zookeeper-3.4.11 # zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Mode: follower // 这也是个follower哦
- 三个zookeeper服务都正常启动了