Zookeeper集群搭建

Zookeeper集群机制
半数机制:集群中半数以上机器存活,集群可用。
zookeeper集群适合搭建在奇数台机器上。

Zookeeper原理及内部选举机制
原理:zookeeper在配置文件中并没有指定master和slave,但是,zookeeper在工作时,只有一个节点为leader,其余节点为follower,leader是通过内部的选举机制临时产生的。

废话有点多了,上篇文章中介绍了Zookeeper单机部署。今天就不重复啰嗦了。想看单机部署的请看上篇文章。
在上篇中已经说明了单机部署zookeeper的详细过程。今天有时间给大家说点zookeeper集群的搭建。

环境介绍:Centos7
JDK版本:1.8
192.168.0.91   zoo-node1
192.168.0.92   zoo-node2
192.168.0.93   zoo-node3

Java、zookeeper环境变量就不多说了,上篇已经介绍了。
注意:三个节点都需要配置Java和zookeeper的环境变量

[root@localhost ~]# cat /etc/profile
#java 
JAVA_HOME=/usr/local/jdk1.8.0_171
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
# zookeeper
export ZK_HOME=/usr/local/zookeeper-3.4.12
export PATH=$ZK_HOME/bin:$PATH

node1上的操作:

[root@localhost src]# tar zxf zookeeper-3.4.12.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/zookeeper-3.4.12/conf/
[root@localhost conf]# mv zoo_sample.cfg zoo.cfg

[root@localhost conf]# vim zoo.cfg 
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper-3.4.12/data
clientPort=2181
server.1=192.168.0.91:2888:3888
server.2=192.168.0.92:2888:3888
server.3=192.168.0.93:2888:3888

[root@localhost conf]# mkdir /usr/local/zookeeper-3.4.12/data         #创建数据目录
[root@localhost conf]# cd /usr/local/zookeeper-3.4.12/data/
[root@localhost data]# echo "1" > myid
[root@localhost data]# cat myid            #这个ID和配置文件中的serverID一定要一致
1

zoo-node1 节点已经算是完成了,可以手动的进行在node2和node3操作!因为本人懒,所以直接复制过去。

[root@localhost data]# cd /usr/local/
[root@localhost local]# scp -r zookeeper-3.4.12 192.168.0.92:/usr/local/
[root@localhost local]# scp -r zookeeper-3.4.12 192.168.0.93:/usr/local/
到node2和node3服务器上把data目录中的myid进行修改。
[root@localhost data]# cd /usr/local/zookeeper-3.4.12/data/
[root@localhost data]# echo "2" > myid 
[root@localhost data]# echo "3" > myid 

注意:data目录中myid的编号要和配置文件中对应,如果不对应会导致失败。
在三台服务器进行启动zookeeper。

[root@localhost ~]# /usr/local/zookeeper-3.4.12/bin/zkServer.sh start       #node1
[root@localhost ~]# /usr/local/zookeeper-3.4.12/bin/zkServer.sh start       #node2
[root@localhost ~]# /usr/local/zookeeper-3.4.12/bin/zkServer.sh start       #node3

验证是否启动成功:

[root@localhost ~]# /usr/local/zookeeper-3.4.12/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: follower
[root@localhost local]# /usr/local/zookeeper-3.4.12/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: follower
[root@localhost ~]# /usr/local/zookeeper-3.4.12/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: leader


[root@localhost ~]# ps -ef | grep zookeeper
root      1970     1  0 8月16 ?       00:02:58 /usr/local/jdk1.8.0_171/bin/java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /usr/local/zookeeper-3.4.12/bin/../build/classes:/usr/local/zookeeper-3.4.12/bin/../build/lib/*.jar:/usr/local/zookeeper-3.4.12/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/local/zookeeper-3.4.12/bin/../lib/slf4j-api-1.7.25.jar:/usr/local/zookeeper-3.4.12/bin/../lib/netty-3.10.6.Final.jar:/usr/local/zookeeper-3.4.12/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper-3.4.12/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper-3.4.12/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper-3.4.12/bin/../zookeeper-3.4.12.jar:/usr/local/zookeeper-3.4.12/bin/../src/java/lib/*.jar:/usr/local/zookeeper-3.4.12/bin/../conf:.:/usr/local/jdk1.8.0_171/lib.tools.jar -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /usr/local/zookeeper-3.4.12/bin/../conf/zoo.cfg

zookeeper集群搭建已经完成。有什么疑问可以留言咨询

你可能感兴趣的:(Zookeeper集群搭建)