1.注册docker账号
https://www.docker.com/
2.安装docker
https://docs.docker.com/engine/install/
3.建文件夹 zk_cluster
4.在zk_cluster文件夹下创建文件docker-compose.yml,文件内容如下:
services:
zk01:
image: zookeeper
restart: always
hostname: zk01
ports:
- 2001:2181
- 3001:2888
- 4001:3888
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=0.0.0.0:2888:3888;2181 server.2=zk02:2888:3888;2181 server.3=zk03:2888:3888;2181
networks:
default:
ipv4_address: 10.0.0.101
zk02:
image: zookeeper
restart: always
hostname: zk02
ports:
- 2002:2181
- 3002:2888
- 4002:3888
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zk01:2888:3888;2181 server.2=0.0.0.0:2888:3888;2181 server.3=zk03:2888:3888;2181
networks:
default:
ipv4_address: 10.0.0.102
zk03:
image: zookeeper
restart: always
hostname: zk03
ports:
- 2003:2181
- 3003:2888
- 4003:3888
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zk01:2888:3888;2181 server.2=zk02:2888:3888;2181 server.3=0.0.0.0:2888:3888;2181
networks:
default:
ipv4_address: 10.0.0.103
networks:
default:
external:
name: zknet
5.在zk_cluster目录下运行如下命令,启动集群
docker-compose up?
6.下载zookeeper客户端(部署在真机上),连接集群进行测试
https://zookeeper.apache.org/releases.html
下载后的安装包bin目录有window和linux两个客户端,这里以macos上的客户端为例
./zkCli.sh -server localhsot:2001,localhost:2001,localhost:2003
连接上集群后可以创建节点、设置节点的值和查询节点的值,命令如下:
create /hello
set /hello world
get /hello
然后登录集群中的节点,查看是否存在我们设置的值。
zk01节点
docker exec -it zk01 bash
cd bin
./zkCli.sh
get /hello
?zk02节点
docker exec -it zk02 bash
cd bin
./zkCli.sh
get /hello
??zk03节点
docker exec -it zk03 bash
cd bin
./zkCli.sh
get /hello
|