前奏
拉取镜像
docker pull zookeeper
查看镜像的版本
docker inspect zookeeper
可以看到当前最新版是3.5.6,如果需要3.4.x版本的拉取镜像时要指定版本
单机
执行如下命令即可创建并运行zookeeper容器
docker run -di --name zookeeper-simple \
-p 2181:2181 \
--restart always \
-v /usr/local/zookeeper-simple/conf:/conf \
-v /usr/local/zookeeper-simple/data:/data \
-v /usr/local/zookeeper-simple/datalog:/datalog \
zookeeper
docker ps
集群搭建(方式一)
1. 创建自己的bridge网络,注册到同一个bridge容器间可以通过容器名进行通信,创建容器的时候还可以指定ip
docker network create -d bridge my-net
也可以创建的时候指定子网掩码和网关
docker network create -d bridge --subnet=172.20.0.0/16 --gateway=172.20.0.1 my-net2
查看自定的bridge
docker network ls
docker network inspect my-net
2. 创建并启动集群各节点
执行如下命令创建集群节点
-- 创建运行节点1
docker run -d -p 2181:2181 --name zookeeper_node1 --privileged --restart always --network my-net \
-v /usr/local/zookeeper-cluster/node1/volumes/data:/data \
-v /usr/local/zookeeper-cluster/node1/volumes/datalog:/datalog \
-v /usr/local/zookeeper-cluster/node3/volumes/conf:/conf \
-e ZOO_MY_ID=1 \
-e "ZOO_SERVERS=server.1=zookeeper_node1:2888:3888;2181 server.2=zookeeper_node2:2888:3888;2181 server.3=zookeeper_node3:2888:3888;2181" \
zookeeper
-- 创建运行节点2
docker run -d -p 2182:2181 --name zookeeper_node2 --privileged --restart always --network my-net \
-v /usr/local/zookeeper-cluster/node2/volumes/data:/data \
-v /usr/local/zookeeper-cluster/node2/volumes/datalog:/datalog \
-v /usr/local/zookeeper-cluster/node3/volumes/conf:/conf \
-e ZOO_MY_ID=2 \
-e "ZOO_SERVERS=server.1=zookeeper_node1:2888:3888;2181 server.2=zookeeper_node2:2888:3888;2181 server.3=zookeeper_node3:2888:3888;2181" \
zookeeper
-- 创建运行节点3
docker run -d -p 2183:2181 --name zookeeper_node3 --privileged --restart always --network my-net \
-v /usr/local/zookeeper-cluster/node3/volumes/data:/data \
-v /usr/local/zookeeper-cluster/node3/volumes/datalog:/datalog \
-v /usr/local/zookeeper-cluster/node3/volumes/conf:/conf \
-e ZOO_MY_ID=3 \
-e "ZOO_SERVERS=server.1=zookeeper_node1:2888:3888;2181 server.2=zookeeper_node2:2888:3888;2181 server.3=zookeeper_node3:2888:3888;2181" \
zookeeper
因为注册到同一个bridge所以可以用容器名字进行通信
如果你穿件bridge时手动指定了子网掩码和网关,那可以使用下面的语句构建,下面的语句跟上面的语句效果相同,不同之处在于创建容器时指定了ip
-- 创建运行节点1
docker run -d -p 2181:2181 --name zookeeper_node1 --privileged --restart always --network my-net2 --ip 172.19.0.2 \
-v /usr/local/zookeeper-cluster/node1/volumes/data:/data \
-v /usr/local/zookeeper-cluster/node1/volumes/datalog:/datalog \
-v /usr/local/zookeeper-cluster/node3/volumes/conf:/conf \
-e ZOO_MY_ID=1 \
-e "ZOO_SERVERS=server.1=172.20.0.2:2888:3888;2181 server.2=172.20.0.3:2888:3888;2181 server.3=172.20.0.4:2888:3888;2181" \
zookeeper
-- 创建运行节点2
docker run -d -p 2182:2181 --name zookeeper_node2 --privileged --restart always --network my-net2 --ip 172.19.0.3 \
-v /usr/local/zookeeper-cluster/node2/volumes/data:/data \
-v /usr/local/zookeeper-cluster/node2/volumes/datalog:/datalog \
-v /usr/local/zookeeper-cluster/node3/volumes/conf:/conf \
-e ZOO_MY_ID=2 \
-e "ZOO_SERVERS=server.1=172.20.0.2:2888:3888;2181 server.2=172.20.0.3:2888:3888;2181 server.3=172.20.0.4:2888:3888;2181" \
zookeeper
-- 创建运行节点3
docker run -d -p 2183:2181 --name zookeeper_node3 --privileged --restart always --network my-net2 --ip 172.19.0.4 \
-v /usr/local/zookeeper-cluster/node3/volumes/data:/data \
-v /usr/local/zookeeper-cluster/node3/volumes/datalog:/datalog \
-v /usr/local/zookeeper-cluster/node3/volumes/conf:/conf \
-e ZOO_MY_ID=3 \
-e "ZOO_SERVERS=server.1=172.20.0.2:2888:3888;2181 server.2=172.20.0.3:2888:3888;2181 server.3=172.20.0.4:2888:3888;2181" \
zookeeper
由于2888 、3888 不需要暴露,就不映射了;
指定自己的网络,并指定IP;
每个容器之间环境是隔离的,所以容器内所用的端口一样:2181/2888/3888
执行完上面的语句后,运行 docker ps查看运行中的容器列表:
3. 验证
进入容器内验证各个节点的运行状态
- 查看每个节点的服务状态
容器1:
[root@VM_0_2_centos ~]# docker exec -it zookeeper_node1 bash
root@4067f52e852c:/apache-zookeeper-3.5.6-bin# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
root@4067f52e852c:/apache-zookeeper-3.5.6-bin#
容器2:
[root@VM_0_2_centos ~]# docker exec -it zookeeper_node2 bash
root@15c08ea27551:/apache-zookeeper-3.5.6-bin# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
root@15c08ea27551:/apache-zookeeper-3.5.6-bin#
容器3:
[root@VM_0_2_centos ~]# docker exec -it zookeeper_node3 bash
root@0f667fa1d288:/apache-zookeeper-3.5.6-bin# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
root@0f667fa1d288:/apache-zookeeper-3.5.6-bin#
- 验证创建节点
在zookeeper_node1中新建节点
[zk: localhost:2181(CONNECTED) 0] create /hello
Created /hello
[zk: localhost:2181(CONNECTED) 1] ls /
[hello, zookeeper]
[zk: localhost:2181(CONNECTED) 2]
在zookeeper_node2中查看新建的节点
[zk: localhost:2181(CONNECTED) 0] ls /
[hello, zookeeper]
[zk: localhost:2181(CONNECTED) 1]
在zookeeper_node3中查看新建的节点
[zk: localhost:2181(CONNECTED) 1] ls /
[hello, zookeeper]
[zk: localhost:2181(CONNECTED) 2]
- 在本地利用zooleeper客户端访问,范文命令如下
.\zkCli.cmd -server 111.229.134.92:2181,111.229.134.92:2182,111.229.134.92:2183
执行结果如下:
PS D:\Environment\apache-zookeeper-3.5.6-bin\bin> .\zkCli.cmd -server 111.229.134.92:2181,111.229.134.92:2182,111.229.134.92:2183
'ZOO_LOG_FILE' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
Connecting to 111.229.134.92:2181,111.229.134.92:2182,111.229.134.92:2183
2020-01-16 22:02:37,245 [myid:] - INFO [main:Environment@109] - Client environment:zookeeper.version=3.5.6-c11b7e26bc554b8523dc929761dd28808913f091, built on 10/08/2019 20:18 GMT
2020-01-16 22:02:37,247 [myid:] - INFO [main:Environment@109] - Client environment:host.name=DESKTOP-74O8ALA
2020-01-16 22:02:37,248 [myid:] - INFO [main:Environment@109] - Client environment:java.version=1.8.0_192
2020-01-16 22:02:37,249 [myid:] - INFO [main:Environment@109] - Client environment:java.vendor=Oracle Corporation
2020-01-16 22:02:37,249 [myid:] - INFO [main:Environment@109] - Client environment:java.home=D:\ProgramFiles\Java\jdk1.8.0_192\jre
2020-01-16 22:02:37,249 [myid:] - INFO [main:Environment@109] - Client environment:java.class.path=D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\build\classes;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\build\lib\*;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\*;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\audience-annotations-0.5.0.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\commons-cli-1.2.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jackson-annotations-2.9.10.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jackson-core-2.9.10.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jackson-databind-2.9.10.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\javax.servlet-api-3.1.0.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jetty-http-9.4.17.v20190418.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jetty-io-9.4.17.v20190418.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jetty-security-9.4.17.v20190418.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jetty-server-9.4.17.v20190418.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jetty-servlet-9.4.17.v20190418.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jetty-util-9.4.17.v20190418.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jline-2.11.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\json-simple-1.1.1.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\log4j-1.2.17.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-buffer-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-codec-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-common-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-handler-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-resolver-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-transport-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-transport-native-epoll-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-transport-native-unix-common-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\slf4j-api-1.7.25.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\slf4j-log4j12-1.7.25.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\zookeeper-3.5.6.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\zookeeper-jute-3.5.6.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\conf
2020-01-16 22:02:37,250 [myid:] - INFO [main:Environment@109] - Client environment:java.library.path=D:\ProgramFiles\Java\jdk1.8.0_192\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;D:\ProgramFiles\TortoiseSVN\bin;D:\ProgramFiles\MySQL\bin;D:\ProgramFiles\Go\bin;D:\ProgramFiles\nodejs\;D:\ProgramFiles\Java\jdk1.8.0_192\bin;D:\ProgramFiles\Java\jdk1.8.0_192\jre\bin;D:\ProgramFiles\Maven\apache-maven-3.6.0\bin;C:\Users\along\go\bin;D:\ProgramFiles\Git\cmd;D:\ProgramFiles\TDM-GCC-64\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Users\along\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Users\along\AppData\Roaming\npm;D:\ProgramFiles\Microsoft VS Code\bin;D:\ProgramFiles\Tools\cmder;;.
2020-01-16 22:02:37,251 [myid:] - INFO [main:Environment@109] - Client environment:java.io.tmpdir=C:\Users\along\AppData\Local\Temp\
2020-01-16 22:02:37,251 [myid:] - INFO [main:Environment@109] - Client environment:java.compiler=
2020-01-16 22:02:37,251 [myid:] - INFO [main:Environment@109] - Client environment:os.name=Windows 10
2020-01-16 22:02:37,252 [myid:] - INFO [main:Environment@109] - Client environment:os.arch=amd64
2020-01-16 22:02:37,252 [myid:] - INFO [main:Environment@109] - Client environment:os.version=10.0
2020-01-16 22:02:37,253 [myid:] - INFO [main:Environment@109] - Client environment:user.name=along
2020-01-16 22:02:37,253 [myid:] - INFO [main:Environment@109] - Client environment:user.home=C:\Users\along
2020-01-16 22:02:37,253 [myid:] - INFO [main:Environment@109] - Client environment:user.dir=D:\Environment\apache-zookeeper-3.5.6-bin\bin
2020-01-16 22:02:37,253 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.free=237MB
2020-01-16 22:02:37,254 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.max=3630MB
2020-01-16 22:02:37,255 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.total=245MB
2020-01-16 22:02:37,258 [myid:] - INFO [main:ZooKeeper@868] - Initiating client connection, connectString=111.229.134.92:2181,111.229.134.92:2182,111.229.134.92:2183 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@4c98385c
2020-01-16 22:02:37,262 [myid:] - INFO [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2020-01-16 22:02:37,825 [myid:] - INFO [main:ClientCnxnSocket@237] - jute.maxbuffer value is 4194304 Bytes
2020-01-16 22:02:37,830 [myid:] - INFO [main:ClientCnxn@1653] - zookeeper.request.timeout value is 0. feature enabled=
Welcome to ZooKeeper!
JLine support is enabled
[zk: 111.229.134.92:2181,111.229.134.92:2182,111.229.134.92:2183(CONNECTING) 0]ls /
[hello, zookeeper]
[zk: 111.229.134.92:2181,111.229.134.92:2182,111.229.134.92:2183(CONNECTED) 2]
如果你集群搭建的没有问题,但是不能访问,请检查你的防火墙,将对应端口开放
firewall-cmd --zone=public --add-port=2181/tcp --permanent
firewall-cmd --zone=public --add-port=2182/tcp --permanent
firewall-cmd --zone=public --add-port=2183/tcp --permanent
-- 重启防火墙
systemctl restart firewalld
firewall-cmd --list-all
或者关闭防火墙
systemctl stop firewalld.service
集群安装(方式二):docker-compose安装
1. docker-compose的安装
方法一:此方法会经常因为网络的原因而无法安装,如果安装不成功请使用方法二。
curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
安装完成后进行授权:
chmod +x /usr/local/bin/docker-compose
检查版本(验证是否安装成功)
docker-compose --version
如果要卸载,删除对应的文件即可
rm /usr/local/bin/docker-compose
方法二:
- 安装python-pip
yum -y install epel-release
yum -y install python-pip
- 确认pip版本
pip --version
- 更新pip
pip install --upgrade pip
- 安装docker-compose
pip install docker-compose
- 检查版本(验证是否安装成功)
docker-compose --version
方法三:终极方法
如果以上两个方法都不能成功,就只能用这个方法了
进入下面的网址下载目前的最新版:
https://github.com/docker/compose/releases
然后将文件上传到 /usr/local/bin/ 文件夹下,然后将其重命名为docker-compose
mv docker-compose-Linux-x86_64 docker-compose
修改此文件的权限,增加可执行
chmod +x /usr/local/bin/docker-compose
然后再检查版本(验证是否安装成功)
[root@VM_0_2_centos bin]# docker-compose version
docker-compose version 1.25.1, build a82fef07
docker-py version: 4.1.0
CPython version: 3.7.4
OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019
呵!安装这个安装了我半天!!!
2. 编写zookeeperCluster.yml文件
在任意目录下新建一个文件
mkdir -p /usr/local/DockerComposeFolder
cd DockerComposeFolder
vim zookeeperCluster.yml
内容如下
version: '3.1' # 指定版本号
services:
zoo1: # 服务名
image: zookeeper # 指定run时运行的镜像
restart: always # 当 Docker 重启时,容器能自动启动
privileged: true # 使用container内的root拥有真正的root权限
hostname: zoo1 # 主机名
ports: # 指定端口映射
- 2181:2181
volumes: # 挂载数据
- /usr/local/zookeeper-cluster/node4/data:/data
- /usr/local/zookeeper-cluster/node4/datalog:/datalog
- /usr/local/zookeeper-cluster/node4/conf:/conf
environment: # 设置环境变量
ZOO_MY_ID: 4
ZOO_SERVERS: server.4=zoo1:2888:3888;2181 server.5=zoo2:2888:3888;2181 server.6=zoo3:2888:3888;2181
#networks:
#default:
#ipv4_address: 172.20.0.5
zoo2:
image: zookeeper
restart: always
privileged: true
hostname: zoo2
ports:
- 2182:2181
volumes: # 挂载数据
- /usr/local/zookeeper-cluster/node5/data:/data
- /usr/local/zookeeper-cluster/node5/datalog:/datalog
- /usr/local/zookeeper-cluster/node5/conf:/conf
environment:
ZOO_MY_ID: 5
ZOO_SERVERS: server.4=zoo1:2888:3888;2181 server.5=zoo2:2888:3888;2181 server.6=zoo3:2888:3888;2181
#networks:
#default:
#ipv4_address: 172.20.0.6
zoo3:
image: zookeeper
restart: always
privileged: true
hostname: zoo3
ports:
- 2183:2181
volumes: # 挂载数据
- /usr/local/zookeeper-cluster/node6/data:/data
- /usr/local/zookeeper-cluster/node6/datalog:/datalog
- /usr/local/zookeeper-cluster/node6/conf:/conf
environment:
ZOO_MY_ID: 6
ZOO_SERVERS: server.4=zoo1:2888:3888;2181 server.5=zoo2:2888:3888;2181 server.6=zoo3:2888:3888;2181
#networks:
#default:
#ipv4_address: 172.20.0.7
networks: # 自定义网络
default:
external:
name: my-net2
注意yaml文件里不能有tab,只能有空格。
3. 执行yaml文件(-d后台启动)
[root@VM_0_2_centos DockerComposeFolder]# docker-compose -f zookeeperCluster.yml up -d
Creating network "dockercomposefolder_default" with the default driver
Removing zoo1
Removing zoo2
Removing zoo3
Recreating 42d098f9abcf_zoo2 ... done
Recreating 40b2495d07e0_zoo3 ... done
Recreating 90f8d1ae3155_zoo1 ... done
[root@VM_0_2_centos DockerComposeFolder]#
查看已启动的容器
[root@VM_0_2_centos DockerComposeFolder]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d66f6628b47a zookeeper "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp zoo1
66b658c083fa zookeeper "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2183->2181/tcp zoo3
7003e1ac8245 zookeeper "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2182->2181/tcp zoo2
[root@VM_0_2_centos DockerComposeFolder]#
4. 验证
进入zoo1容器
[root@VM_0_2_centos DockerComposeFolder]# docker exec -it zoo1 bash
root@zoo1:/apache-zookeeper-3.5.6-bin# ./bin/zkCli.sh
Connecting to localhost:2181
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Welcome to ZooKeeper!
JLine support is enabled
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] create /hello
Created /hello
[zk: localhost:2181(CONNECTED) 2] ls /
[hello, zookeeper]
[zk: localhost:2181(CONNECTED) 3]
进入zoo2容器
[root@VM_0_2_centos ~]# docker exec -it zoo2 bash
root@zoo2:/apache-zookeeper-3.5.6-bin# ./bin/zkCli.sh
Connecting to localhost:2181
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Welcome to ZooKeeper!
JLine support is enabled
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[hello, zookeeper]
[zk: localhost:2181(CONNECTED) 1] create /hello2
Created /hello2
[zk: localhost:2181(CONNECTED) 2] ls /
[hello, hello2, zookeeper]
[zk: localhost:2181(CONNECTED) 3]
进入zoo3容器
[root@VM_0_2_centos ~]# docker exec -it zoo3 bash
root@zoo3:/apache-zookeeper-3.5.6-bin# ./bin/zkCli.sh
Connecting to localhost:2181
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Welcome to ZooKeeper!
JLine support is enabled
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[hello, hello2, zookeeper]
[zk: localhost:2181(CONNECTED) 1] create /hello3
Created /hello3
[zk: localhost:2181(CONNECTED) 2] ls /
[hello, hello2, hello3, zookeeper]
[zk: localhost:2181(CONNECTED) 3]
可以看到每个节点新建的节点都同步到了其他节点
在本地利用zooleeper客户端访问集群
.\zkCli.cmd -server 111.229.134.92:2181,111.229.134.92:2182,111.229.134.92:2183
执行结果如下
PS D:\Environment\apache-zookeeper-3.5.6-bin\bin> .\zkCli.cmd -server 111.229.134.92:2181,111.229.134.92:2182,111.229.134.92:2183
'ZOO_LOG_FILE' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
Connecting to 111.229.134.92:2181,111.229.134.92:2182,111.229.134.92:2183
2020-01-17 00:52:29,006 [myid:] - INFO [main:Environment@109] - Client environment:zookeeper.version=3.5.6-c11b7e26bc554b8523dc929761dd28808913f091, built on 10/08/2019 20:18 GMT
2020-01-17 00:52:29,009 [myid:] - INFO [main:Environment@109] - Client environment:host.name=DESKTOP-74O8ALA
2020-01-17 00:52:29,010 [myid:] - INFO [main:Environment@109] - Client environment:java.version=1.8.0_192
2020-01-17 00:52:29,011 [myid:] - INFO [main:Environment@109] - Client environment:java.vendor=Oracle Corporation
2020-01-17 00:52:29,011 [myid:] - INFO [main:Environment@109] - Client environment:java.home=D:\ProgramFiles\Java\jdk1.8.0_192\jre
2020-01-17 00:52:29,011 [myid:] - INFO [main:Environment@109] - Client environment:java.class.path=D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\build\classes;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\build\lib\*;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\*;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\audience-annotations-0.5.0.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\commons-cli-1.2.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jackson-annotations-2.9.10.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jackson-core-2.9.10.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jackson-databind-2.9.10.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\javax.servlet-api-3.1.0.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jetty-http-9.4.17.v20190418.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jetty-io-9.4.17.v20190418.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jetty-security-9.4.17.v20190418.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jetty-server-9.4.17.v20190418.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jetty-servlet-9.4.17.v20190418.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jetty-util-9.4.17.v20190418.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\jline-2.11.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\json-simple-1.1.1.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\log4j-1.2.17.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-buffer-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-codec-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-common-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-handler-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-resolver-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-transport-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-transport-native-epoll-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\netty-transport-native-unix-common-4.1.42.Final.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\slf4j-api-1.7.25.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\slf4j-log4j12-1.7.25.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\zookeeper-3.5.6.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\lib\zookeeper-jute-3.5.6.jar;D:\Environment\apache-zookeeper-3.5.6-bin\bin\..\conf
2020-01-17 00:52:29,011 [myid:] - INFO [main:Environment@109] - Client environment:java.library.path=D:\ProgramFiles\Java\jdk1.8.0_192\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;D:\ProgramFiles\TortoiseSVN\bin;D:\ProgramFiles\MySQL\bin;D:\ProgramFiles\Go\bin;D:\ProgramFiles\nodejs\;D:\ProgramFiles\Java\jdk1.8.0_192\bin;D:\ProgramFiles\Java\jdk1.8.0_192\jre\bin;D:\ProgramFiles\Maven\apache-maven-3.6.0\bin;C:\Users\along\go\bin;D:\ProgramFiles\Git\cmd;D:\ProgramFiles\TDM-GCC-64\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Users\along\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Users\along\AppData\Roaming\npm;D:\ProgramFiles\Microsoft VS Code\bin;D:\ProgramFiles\Tools\cmder;;.
2020-01-17 00:52:29,012 [myid:] - INFO [main:Environment@109] - Client environment:java.io.tmpdir=C:\Users\along\AppData\Local\Temp\
2020-01-17 00:52:29,012 [myid:] - INFO [main:Environment@109] - Client environment:java.compiler=
2020-01-17 00:52:29,012 [myid:] - INFO [main:Environment@109] - Client environment:os.name=Windows 10
2020-01-17 00:52:29,012 [myid:] - INFO [main:Environment@109] - Client environment:os.arch=amd64
2020-01-17 00:52:29,012 [myid:] - INFO [main:Environment@109] - Client environment:os.version=10.0
2020-01-17 00:52:29,012 [myid:] - INFO [main:Environment@109] - Client environment:user.name=along
2020-01-17 00:52:29,013 [myid:] - INFO [main:Environment@109] - Client environment:user.home=C:\Users\along
2020-01-17 00:52:29,013 [myid:] - INFO [main:Environment@109] - Client environment:user.dir=D:\Environment\apache-zookeeper-3.5.6-bin\bin
2020-01-17 00:52:29,013 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.free=237MB
2020-01-17 00:52:29,014 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.max=3630MB
2020-01-17 00:52:29,014 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.total=245MB
2020-01-17 00:52:29,017 [myid:] - INFO [main:ZooKeeper@868] - Initiating client connection, connectString=111.229.134.92:2181,111.229.134.92:2182,111.229.134.92:2183 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@4c98385c
2020-01-17 00:52:29,020 [myid:] - INFO [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2020-01-17 00:52:29,583 [myid:] - INFO [main:ClientCnxnSocket@237] - jute.maxbuffer value is 4194304 Bytes
2020-01-17 00:52:29,588 [myid:] - INFO [main:ClientCnxn@1653] - zookeeper.request.timeout value is 0. feature enabled=
Welcome to ZooKeeper!
JLine support is enabled
[zk: 111.229.134.92:2181,111.229.134.92:2182,111.229.134.92:2183(CONNECTING) 0] 2020-01-17 00:52:47,657 [myid:111.229.134.92:2182] - INFO [main-SendThread(111.229.134.92:2182):ClientCnxn$SendThread@1112] - Opening socket connection to server 111.229.134.92/111.229.134.92:2182. Will not attempt to authenticate using SASL (unknown error)
2020-01-17 00:52:47,663 [myid:111.229.134.92:2182] - INFO [main-SendThread(111.229.134.92:2182):ClientCnxn$SendThread@959] - Socket connection established, initiating session, client: /192.168.0.102:8878, server: 111.229.134.92/111.229.134.92:2182
2020-01-17 00:52:47,685 [myid:111.229.134.92:2182] - INFO [main-SendThread(111.229.134.92:2182):ClientCnxn$SendThread@1394] - Session establishment complete on server 111.229.134.92/111.229.134.92:2182, sessionid = 0x500529f92f50001, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 111.229.134.92:2181,111.229.134.92:2182,111.229.134.92:2183(CONNECTED) 0] ls /
[hello, hello2, hello3, zookeeper]
[zk: 111.229.134.92:2181,111.229.134.92:2182,111.229.134.92:2183(CONNECTED) 1]
参考链接:https://www.cnblogs.com/LUA123/p/11428113.html