码迷,mamicode.com
首页 > 其他好文 > 详细

部署 Zookeeper 高可用集群

时间:2018-11-17 10:27:30      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:final   快速   log4   可靠   rac   echo   src   uil   release   

一、Zookeeper原理简介
ZooKeeper是一个开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等。

Zookeeper设计目的
最终一致性:client不论连接到那个Server,展示给它的都是同一个视图。
可靠性:具有简单、健壮、良好的性能、如果消息m被到一台服务器接收,那么消息m将被所有服务器接收。
实时性:Zookeeper保证客户端将在一个时间间隔范围内获得服务器的更新信息,或者服务器失效的信息。但由于网络延时等原因,Zookeeper不能保证两个客户端能同时得到刚更新的数据,如果需要最新数据,应该在读数据之前调用sync()接口。
等待无关(wait-free):慢的或者失效的client不得干预快速的client的请求,使得每个client都能有效的等待。
原子性:更新只能成功或者失败,没有中间状态。
顺序性:包括全局有序和偏序两种:全局有序是指如果在一台服务器上消息a在消息b前发布,则在所有Server上消息a都将在消息b前被发布;偏序是指如果一个消息b在消息a后被同一个发送者发布,a必将排在b前面。

Zookeeper工作原理
1、在zookeeper的集群中,各个节点共有下面3种角色和4种状态:

角色:leader,follower,observer
状态:leading,following,observing,looking

Zookeeper的核心是原子广播,这个机制保证了各个Server之间的同步。实现这个机制的协议叫做Zab协议(ZooKeeper Atomic Broadcast protocol)。Zab协议有两种模式,它们分别是恢复模式(Recovery选主)和广播模式(Broadcast同步)。当服务启动或者在领导者崩溃后,Zab就进入了恢复模式,当领导者被选举出来,且大多数Server完成了和leader的状态同步以后,恢复模式就结束了。状态同步保证了leader和Server具有相同的系统状态。

为了保证事务的顺序一致性,zookeeper采用了递增的事务id号(zxid)来标识事务。所有的提议(proposal)都在被提出的时候加上了zxid。实现中zxid是一个64位的数字,它高32位是epoch用来标识leader关系是否改变,每次一个leader被选出来,它都会有一个新的epoch,标识当前属于那个leader的统治时期。低32位用于递增计数。

每个Server在工作过程中有4种状态:

LOOKING:当前Server不知道leader是谁,正在搜寻。

LEADING:当前Server即为选举出来的leader。

FOLLOWING:leader已经选举出来,当前Server与之同步。

OBSERVING:observer的行为在大多数情况下与follower完全一致,但是他们不参加选举和投票,而仅仅接受(observing)选举和投票的结果。

Zookeeper集群节点
Zookeeper节点部署越多,服务的可靠性越高,建议部署奇数个节点,因为zookeeper集群是以宕机个数过半才会让整个集群宕机的。
需要给每个zookeeper 1G左右的内存,如果可能的话,最好有独立的磁盘,因为独立磁盘可以确保zookeeper是高性能的。如果你的集群负载很重,不要把zookeeper和RegionServer运行在同一台机器上面,就像DataNodes和TaskTrackers一样。

实验环境:

主机名 系统 IP

linux-node1 CentOS Linux release 7.5 192.168.10.10

linux-node2 CentOS Linux release 7.5 192.168.10.20

linux-node3 CentOS Linux release 7.5 192.168.10.20

二、Zookeeper安装
Zookeeper运行需要java环境,需要安装 jdk,注:每台服务器上面都需要安装zookeeper、jdk,建议本地下载好需要的安装包然后上传到服务器上面,服务器上面下载速度太慢。

2.1 三台服务器安装 JDK
[root@linux-node1 tools]# tar -zxvf jdk-8u181-linux-x64.tar.gz -C /usr/local/

[root@linux-node1 tools]# cd /usr/local/
[root@linux-node1 local]# ll
总用量 0
drwxr-xr-x. 2 root root 6 4月 11 12:59 bin
drwxr-xr-x. 2 root root 6 4月 11 12:59 etc
drwxr-xr-x. 2 root root 6 4月 11 12:59 games
drwxr-xr-x. 2 root root 6 4月 11 12:59 include
drwxr-xr-x. 7 10 143 245 7月 7 2018 jdk1.8.0_181
drwxr-xr-x. 2 root root 6 4月 11 12:59 lib
drwxr-xr-x. 2 root root 6 4月 11 12:59 lib64
drwxr-xr-x. 2 root root 6 4月 11 12:59 libexec
drwxr-xr-x. 2 root root 6 4月 11 12:59 sbin
drwxr-xr-x. 5 root root 49 6月 25 2018 share
drwxr-xr-x. 2 root root 6 4月 11 12:59 src
[root@linux-node1 local]# mv jdk1.8.0_181 jdk

配置环境变量,增加以下内容:
[root@linux-node1 ~]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
[root@linux-node1 ~]# source /etc/profile

[root@linux-node1 local]# java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

2.2、Zookeeper安装
Zookeeper链接:http://zookeeper.apache.org/

[root@linux-node1 tools]# wget https://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz --no-check-certificate

[root@linux-node1 tools]# tar -zxvf zookeeper-3.4.13.tar.gz -C /usr/local/
[root@linux-node1 tools]# cd /usr/local && mv zookeeper-3.4.13 zookeeper

[root@linux-node1 local]# cp zookeeper/conf/zoo_sample.cfg zookeeper/conf/zoo.cfg

#把zookeeper加入到环境变量。
[root@linux-node1 local]# echo -e "# append zk_env\nexport PATH=$PATH:/usr/local//zookeeper/bin" >> /etc/profile

[root@linux-node1 local]# source /etc/profile

三、Zookeeper集群配置
注意:搭建zookeeper集群时,一定要先停止已经启动的zookeeper节点。

3.1、Zookeeper配置文件修改

[root@linux-node1 local]# egrep -v "^#|^$" zookeeper/conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataLogDir=/usr/local/zookeeper/logs
dataDir=/usr/local/zookeeper/data
clientPort=2181
autopurge.snapRetainCount=500
autopurge.purgeInterval=24
server.1= 192.168.10.10:2888:3888
server.2= 192.168.10.20:2888:3888
server.3= 192.168.10.30:2888:3888

#创建相关目录,三台节点都需要

[root@linux-node1 local]# mkdir -p /usr/local/zookeeper/{logs,data}

#其余zookeeper节点安装完成之后,同步配置文件zoo.cfg。

3.2、配置参数说明
tickTime这个时间是作为zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是说每个tickTime时间就会发送一个心跳。

initLimit这个配置项是用来配置zookeeper接受客户端(这里所说的客户端不是用户连接zookeeper服务器的客户端,而是zookeeper服务器集群中连接到leader的follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。

当已经超过10个心跳的时间(也就是tickTime)长度后 zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000=20秒。

syncLimit这个配置项标识leader与follower之间发送消息,请求和应答时间长度,最长不能超过多少个tickTime的时间长度,总的时间长度就是5*2000=10秒。

dataDir顾名思义就是zookeeper保存数据的目录,默认情况下zookeeper将写数据的日志文件也保存在这个目录里;

clientPort这个端口就是客户端连接Zookeeper服务器的端口,Zookeeper会监听这个端口接受客户端的访问请求;

server.A=B:C:D中的A是一个数字,表示这个是第几号服务器,B是这个服务器的IP地址,C第一个端口用来集群成员的信息交换,表示这个服务器与集群中的leader服务器交换信息的端口,D是在leader挂掉时专门用来进行选举leader所用的端口。

3.3、创建ServerID标识
除了修改zoo.cfg配置文件外,zookeeper集群模式下还要配置一个myid文件,这个文件需要放在dataDir目录下。

这个文件里面有一个数据就是A的值(该A就是zoo.cfg文件中server.A=B:C:D中的A),在zoo.cfg文件中配置的dataDir路径中创建myid文件。

#在192.168.10.10 服务器上面创建myid文件,并设置值为1,同时与zoo.cfg文件里面的server.1保持一致,如下:
[root@linux-node1 local]# echo "1" >/usr/local/zookeeper/data/myid

[root@linux-node1 local]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::1073:2cbf:aa21:920b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d3:c6:80 txqueuelen 1000 (Ethernet)
RX packets 188131 bytes 267811277 (255.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 51922 bytes 5316450 (5.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 283 bytes 24936 (24.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 283 bytes 24936 (24.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

#在192.168.10.20 服务器上面创建myid文件,并设置值为2,同时与zoo.cfg文件里面的server.1保持一致,如下:

[root@linux-node2 local]# echo "2" >/usr/local/zookeeper/data/myid
[root@linux-node2 local]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.20 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::e690:3c3c:62ba:c9e2 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:b5:43:96 txqueuelen 1000 (Ethernet)
RX packets 187362 bytes 267661578 (255.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 53707 bytes 5386328 (5.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 216 bytes 19064 (18.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 216 bytes 19064 (18.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

#在192.168.10.30 服务器上面创建myid文件,并设置值为3,同时与zoo.cfg文件里面的server.1保持一致,如下:

[root@linux-node3 local]# echo "3" >/usr/local/zookeeper/data/myid
[root@linux-node3 local]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.30 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::a3a:d1af:e5cb:3c86 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:42:e2:7f txqueuelen 1000 (Ethernet)
RX packets 187924 bytes 267761239 (255.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 53236 bytes 5392570 (5.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 242 bytes 21496 (20.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 242 bytes 21496 (20.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

到此,相关配置完成。

四、Zookeeper 集群查看

#在 linux-node1、linux-node2、linux-node3 启动服务。

[root@linux-node1 local]# /usr/local/zookeeper/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

[root@linux-node2 local]# /usr/local/zookeeper/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

[root@linux-node3 local]# /usr/local/zookeeper/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

2、启动完成之后查看每个节点的状态
#linux-node1
[root@linux-node1 conf]# /usr/local/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower

[root@linux-node1 conf]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::1073:2cbf:aa21:920b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d3:c6:80 txqueuelen 1000 (Ethernet)
RX packets 189426 bytes 267929552 (255.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 52879 bytes 5437430 (5.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 305 bytes 26388 (25.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 305 bytes 26388 (25.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

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

[root@linux-node2 conf]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.20 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::e690:3c3c:62ba:c9e2 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:b5:43:96 txqueuelen 1000 (Ethernet)
RX packets 188555 bytes 267768644 (255.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 55247 bytes 5534675 (5.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 230 bytes 20062 (19.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 230 bytes 20062 (19.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

linux-node3
[root@linux-node3 conf]# /usr/local/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[root@linux-node3 conf]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.30 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::a3a:d1af:e5cb:3c86 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:42:e2:7f txqueuelen 1000 (Ethernet)
RX packets 189101 bytes 267862278 (255.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 54037 bytes 5488590 (5.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 252 bytes 22258 (21.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 252 bytes 22258 (21.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

#从上面可以看出,linux-node1,linux-node3两台服务器zookeeper的状态是follow模式,linux-node2这台服务器zookeeper的状态是leader模式。

五、Zookeeper集群连接
Zookeeper集群搭建完毕之后,可以通过客户端脚本连接到zookeeper集群上面,对客户端来说,zookeeper集群是一个整体,连接到zookeeper集群实际上感觉在独享整个集群的服务。

[root@linux-node1 bin]# /usr/local/zookeeper/bin/zkCli.sh -server 192.168.10.20:2181
Connecting to 192.168.10.20:2181
2018-06-25 18:44:57,700 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 04:05 GMT
2018-06-25 18:44:57,704 [myid:] - INFO [main:Environment@100] - Client environment:host.name=linux-node1
2018-06-25 18:44:57,704 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_181
2018-06-25 18:44:57,706 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-06-25 18:44:57,707 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/jdk/jre
2018-06-25 18:44:57,707 [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.7.25.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/usr/local/zookeeper/bin/../lib/netty-3.10.6.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper/bin/../zookeeper-3.4.13.jar:/usr/local/zookeeper/bin/../src/java/lib/.jar:/usr/local/zookeeper/bin/../conf:.:/usr/local/jdk/lib/dt.jar:/usr/local/jdk/lib/tools.jar
2018-06-25 18:44:57,707 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-06-25 18:44:57,707 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-06-25 18:44:57,707 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2018-06-25 18:44:57,707 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2018-06-25 18:44:57,707 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2018-06-25 18:44:57,707 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-862.el7.x86_64
2018-06-25 18:44:57,707 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2018-06-25 18:44:57,707 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2018-06-25 18:44:57,708 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper/bin
2018-06-25 18:44:57,709 [myid:] - INFO [main:ZooKeeper@442] - Initiating client connection, connectString=192.168.10.20:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@4b9af9a9
Welcome to ZooKeeper!
JLine support is enabled
2018-06-25 18:44:57,801 [myid:] - INFO [main-SendThread(192.168.10.20:2181):ClientCnxn$SendThread@1029] - Opening socket connection to server 192.168.10.20/192.168.10.20:2181. Will not attempt to authenticate using SASL (unknown error)
2018-06-25 18:44:57,876 [myid:] - INFO [main-SendThread(192.168.10.20:2181):ClientCnxn$SendThread@879] - Socket connection established to 192.168.10.20/192.168.10.20:2181, initiating session
[zk: 192.168.10.20:2181(CONNECTING) 0] 2018-06-25 18:44:57,936 [myid:] - INFO [main-SendThread(192.168.10.20:2181):ClientCnxn$SendThread@1303] - Session establishment complete on server 192.168.10.20/192.168.10.20:2181, sessionid = 0x20000a259f20001, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

部署 Zookeeper 高可用集群

标签:final   快速   log4   可靠   rac   echo   src   uil   release   

原文地址:http://blog.51cto.com/13550113/2318147

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!