配置ZooKeeper集群

以3节点的集群为例:192.168.1.220、192.168.1.221、192.168.1.222。因为前面配置单机ZooKeeper和伪集群ZooKeeper已经解释过所有步骤和配置项的意义,所以这里就直接给步骤。

假设已经在3个节点上都安装好了jdk(这是前提),并下载好了ZooKeeper。

[root@master ~]# tar xf zookeeper-3.4.7.tar.gz 
[root@master ~]# mv zookeeper-3.4.7 /usr/local/zookeeper
# 3个节点都执行

添加ZooKeeper环境变量,非必须过程,但建议。

[root@master ~]# echo 'ZOOKEEPER_HOME=/usr/local/zookeeper' >/etc/profile.d/zk.sh
[root@master ~]# echo 'PATH=$ZOOKEEPER_HOME/bin:$PATH' >> /etc/profile.d/zk.sh
[root@master ~]# chmod +x /etc/profile.d/zk.sh
[root@master ~]# source /etc/profile.d/zk.sh
# 在3节点上都执行

提供配置文件。以下是3个节点上的配置文件:

[root@master ~]# vi /usr/local/zookeeper/conf/zoo.cfg
tickTime=2000
dataDir=/usr/local/zookeeper/data
clientPort=2181
initLimit=5
syncLimit=2
server.1=192.168.1.220:2888:3888
server.2=192.168.1.221:2888:3888
server.3=192.168.1.222:2888:3888

在三个节点上都创建好数据目录,并写入myid文件。

[root@master ~]# mkdir /usr/local/zookeeper/data
# 3个节点上都执行:
[root@master ~]# echo 1 >/usr/local/zookeeper/data/myid
#192.168.1.220上执行
[root@node02 ~]# echo 2 >/usr/local/zookeeper/data/myid
#192.168.1.222上执行
[root@node01 ~]# echo 3 >/usr/local/zookeeper/data/myid
#192.168.1.221上执行

启动这3个ZooKeeper实例。

[root@master ~]# zkServer.sh start
# 3个节点都执行:

然后在3个节点上都验证一下是否正确启动ZooKeeper。

# 节点1上执行:
[root@master ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
# 节点2上执行:
[root@node01 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: leader
# 节点3上执行:
[root@node02 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower

如果某个服务器实例出现了错误,请zkServer.sh stop关闭ZooKeeper实例,然后jps命令查看下是否还有ZooKeeper实例在运行,如果有,kill掉。然后再启动ZooKeeper。另外,建议在第一次启动ZooKeeper之前,先将dataDir/version-2目录删除(如果已存在,因为可能是以前的实例因为各种原因而留下来的)。

最后测试下,某节点上创建一个znode,其它节点是否也同步了该节点。

# 在192.168.1.220上创建一个znode:
[root@master ~]# zkCli.sh -server 192.168.1.220:2181 create /test_znode "hello world"
Connecting to 192.168.1.220:2181
2020-03-05 21:10:59,181 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.7-1713338, built on 11/09/2015 04:32 GMT
2020-03-05 21:10:59,195 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=master
2020-03-05 21:10:59,195 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_191
2020-03-05 21:10:59,204 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2020-03-05 21:10:59,205 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/local/jdk1.8.0_191/jre
2020-03-05 21:10:59,206 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/bin/../lib/netty-3.7.0.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/bin/../zookeeper-3.4.7.jar:/usr/local/zookeeper/bin/../src/java/lib/*.jar:/usr/local/zookeeper/bin/../conf:
2020-03-05 21:10:59,208 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-03-05 21:10:59,210 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2020-03-05 21:10:59,211 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2020-03-05 21:10:59,211 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2020-03-05 21:10:59,212 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2020-03-05 21:10:59,213 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-693.el7.x86_64
2020-03-05 21:10:59,213 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2020-03-05 21:10:59,213 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2020-03-05 21:10:59,213 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/root
2020-03-05 21:10:59,216 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=192.168.1.220:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@1a86f2f1
2020-03-05 21:10:59,271 [myid:] - INFO  [main-SendThread(192.168.1.220:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 192.168.1.220/192.168.1.220:2181. Will not attempt to authenticate using SASL (unknown error)
2020-03-05 21:10:59,346 [myid:] - INFO  [main-SendThread(192.168.1.220:2181):ClientCnxn$SendThread@876] - Socket connection established to 192.168.1.220/192.168.1.220:2181, initiating session
2020-03-05 21:10:59,378 [myid:] - INFO  [main-SendThread(192.168.1.220:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 192.168.1.220/192.168.1.220:2181, sessionid = 0x170aaceef8c0000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
Created /test_znode
# 在192.168.1.221上获取这个znode
[root@node01 ~]# zkCli.sh -server 192.168.1.221:2181 get /test_znode
Connecting to 192.168.1.221:2181
2020-03-05 21:12:22,652 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.7-1713338, built on 11/09/2015 04:32 GMT
2020-03-05 21:12:22,655 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=node01
2020-03-05 21:12:22,655 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_191
2020-03-05 21:12:22,657 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2020-03-05 21:12:22,657 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/local/jdk1.8.0_191/jre
2020-03-05 21:12:22,657 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/bin/../lib/netty-3.7.0.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/bin/../zookeeper-3.4.7.jar:/usr/local/zookeeper/bin/../src/java/lib/*.jar:/usr/local/zookeeper/bin/../conf:
2020-03-05 21:12:22,657 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-03-05 21:12:22,657 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2020-03-05 21:12:22,657 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2020-03-05 21:12:22,657 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2020-03-05 21:12:22,657 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2020-03-05 21:12:22,657 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-693.el7.x86_64
2020-03-05 21:12:22,657 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2020-03-05 21:12:22,657 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2020-03-05 21:12:22,658 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/root
2020-03-05 21:12:22,658 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=192.168.1.221:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@1a86f2f1
2020-03-05 21:12:22,695 [myid:] - INFO  [main-SendThread(192.168.1.221:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 192.168.1.221/192.168.1.221:2181. Will not attempt to authenticate using SASL (unknown error)
2020-03-05 21:12:22,814 [myid:] - INFO  [main-SendThread(192.168.1.221:2181):ClientCnxn$SendThread@876] - Socket connection established to 192.168.1.221/192.168.1.221:2181, initiating session
2020-03-05 21:12:22,826 [myid:] - INFO  [main-SendThread(192.168.1.221:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 192.168.1.221/192.168.1.221:2181, sessionid = 0x270aacedac50001, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
hello world
cZxid = 0x100000002
ctime = Thu Mar 05 21:10:54 CST 2020
mZxid = 0x100000002
mtime = Thu Mar 05 21:10:54 CST 2020
pZxid = 0x100000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 11
numChildren = 0
# 在192.168.1.222上获取这个znode
[root@node02 data]# zkCli.sh -server 192.168.1.222:2181 ls /
Connecting to 192.168.1.222:2181
2020-03-05 21:13:46,206 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.7-1713338, built on 11/09/2015 04:32 GMT
2020-03-05 21:13:46,222 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=node02
2020-03-05 21:13:46,222 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_191
2020-03-05 21:13:46,236 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2020-03-05 21:13:46,236 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/local/jdk1.8.0_191/jre
2020-03-05 21:13:46,237 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/bin/../lib/netty-3.7.0.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/bin/../zookeeper-3.4.7.jar:/usr/local/zookeeper/bin/../src/java/lib/*.jar:/usr/local/zookeeper/bin/../conf:
2020-03-05 21:13:46,237 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-03-05 21:13:46,237 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2020-03-05 21:13:46,237 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2020-03-05 21:13:46,238 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2020-03-05 21:13:46,238 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2020-03-05 21:13:46,238 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-693.el7.x86_64
2020-03-05 21:13:46,239 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2020-03-05 21:13:46,239 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2020-03-05 21:13:46,239 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper/data
2020-03-05 21:13:46,244 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=192.168.1.222:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@1a86f2f1
2020-03-05 21:13:46,294 [myid:] - INFO  [main-SendThread(192.168.1.222:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 192.168.1.222/192.168.1.222:2181. Will not attempt to authenticate using SASL (unknown error)
2020-03-05 21:13:46,414 [myid:] - INFO  [main-SendThread(192.168.1.222:2181):ClientCnxn$SendThread@876] - Socket connection established to 192.168.1.222/192.168.1.222:2181, initiating session
2020-03-05 21:13:46,432 [myid:] - INFO  [main-SendThread(192.168.1.222:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 192.168.1.222/192.168.1.222:2181, sessionid = 0x370aacef0570000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zookeeper, test_znode]

 

posted @ 2020-03-05 21:13  星火撩原  阅读(192)  评论(0编辑  收藏  举报