一、准备工作
1.1 安装docker
我这是使用的centos 7.4 其他系统请自行查找安装方法
yum install docker
1.2 拉取zookeper镜像
docker pull zookeeper
二、搭建集群
使用docker-compose管理集群
2.1 创建docker-compose配置文件
version: '2'
services:
zoo1:
image: zookeeper
restart: always
container_name: zoo1
ports:
- "2281: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
container_name: zoo2
ports:
- "2282: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
container_name: zoo3
ports:
- "2283:2181"
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
此配置文件会告诉docker分别运行三个zookeper镜像
分别将本地的 2281、2282、2283映射到容器2181端口
ZOO_MY_ID 和 ZOO_SERVERS 是搭建 ZK 集群需要设置的两个环境变量
其中 ZOO_MY_ID 表示 ZK 服务的 id, 它是1-255 之间的整数, 必须在集群中唯一
ZOO_SERVERS 是ZK 集群的主机列表.
2.2 启动集群
ps:本操作需要在docker-compose.yml所在目录下执行
# 关闭把up 替换成 stop即可
COMPOSE_PROJECT_NAME=zk_test docker-compose up
如果发现这个错误:
-bash: docker-compose: command not found
解决办法:
yum -y install epel-release
# 安装插件
yum -y install python-pip
# 确认版本
pip --version
# 安装compose
pip install docker-compose
# 查看版本
docker-compose version
# 安装成功再执行命令就ok的
2.3 连接集群
ps:本操作需要在本地下载zk,然后在zk/bin 目录下执行
./zkCli.sh -server 123.0.0.1:2281,123.0.0.1:2282,123.0.0.1:2283
2.4 查看集群状态
ps:本操作需要在docker-compose.yml所在目录下执行
COMPOSE_PROJECT_NAME=zk_test docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------------
zoo1 /docker-entrypoint.sh zkSe ... Up 0.0.0.0:2281->2181/tcp, 2888/tcp, 3888/tcp
zoo2 /docker-entrypoint.sh zkSe ... Up 0.0.0.0:2282->2181/tcp, 2888/tcp, 3888/tcp
zoo3 /docker-entrypoint.sh zkSe ... Up 0.0.0.0:2283->2181/tcp, 2888/tcp, 3888/tcp
2.5 查看单个节点状态
我们可以看到, zoo1, zoo2 都是 follower, 而 zoo3 是 leader
[root@bogon docker]# echo stat | nc 127.0.0.1 2281
Zookeeper version: 3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT
Clients:
/127.0.0.1:44614[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/0
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x200000002
Mode: follower
Node count: 4
[root@bogon docker]# echo stat | nc 127.0.0.1 2282
Zookeeper version: 3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT
Clients:
/127.0.0.1:51982[1](queued=0,recved=6,sent=6)
/127.0.0.1:51996[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/0
Received: 7
Sent: 6
Connections: 2
Outstanding: 0
Zxid: 0x200000002
Mode: follower
Node count: 4
[root@bogon docker]# echo stat | nc 127.0.0.1 2283
Zookeeper version: 3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT
Clients:
/127.0.0.1:36822[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/0
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x300000000
Mode: leader
Node count: 4
暂时就安装完毕,后期使用问题再看
网友评论