Zookeeper 是一个开源的分布式服务协调框架,由Apache软件基金会开发和维护。以下是对Zookeeper的主要概述:
设计目标:
功能特性:
工作原理:
应用场景:
总的来说,Zookeeper在分布式系统中扮演着关键的角色,通过提供可靠的数据管理和协调服务,简化了分布式应用程序的开发和运维。
1)集群中半数以上机器存活,集群可用。所以 Zookeeper 一般安装奇数台服务器,因为假设有 2N + 1 台机器只要 N + 1 台存活,集群就可用,再加一台也还是 N + 1 台,并不会提高集群可用性能,浪费资源。
2)服务器具有4
种状态,分别是LOOKING
、FOLLOWING
、LEADING
、OBSERVING
。
LOOKING
:寻找Leader
状态。当服务器处于该状态时,当前集群中没有Leader
,因此需要进入Leader
选举状态。FOLLOWING
:跟随者状态。表明当前服务器角色是Follower
。LEADING
:领导者状态。表明当前服务器角色是Leader
。OBSERVING
:观察者状态。表明当前服务器角色是Observer
,Observer
不参与投票和选举过程。3)选举过程:假设有五台服务器组成的 Zookeeper 集群,它们的 id 从 1 - 5。如果这些服务器依序启动**(第一次启动)**
LOOKING
;id
值更大的服务器 2 胜出,服务器 1 将选票改投给 服务器 2,此时服务器 1 有 0 票,服务器 2 有 2 票,还不够半数,选举无法完成,服务器状态 1、2 保持为 LOOKING
;Leader
。服务器状态 1、2 更改状态为 FOLLOWDING
,服务器 3 更改状态为 LEADING
;Leader
了,服务器 1、2、3 不会再改投了。所以投票结果为:服务器 3 有 3 票,服务器 4 有 1 票。服务器 4 服从多数,更改状态为 FOLLOWING
;FOLLOWING
。下载地址:Index of /dist/zookeeper (apache.org)
文档地址:Apache ZooKeeper
这里选择 Zookeeper-3.5.7
版本进行搭建,自行配置 JDK7
或更高版本
分布式配置与伪分布式配置,主要有以下区别,其余配置均相同:
IP
,分布式需要根据当前网络IP
配置。以下为分布式与伪分布式配置对比(以3
个服务器节点为例):这里选择分布式配置
配置 | 伪分布式 | 分布式 |
---|---|---|
clienPort |
2181;2182;2183 |
2181;2181;2181 |
server.1 |
127.0.0.1:2888:3888 |
hadoop102:2888:3888 |
server.2 |
127.0.0.1:2889:3889 |
hadoop103:2888:3888 |
server.3 |
127.0.0.1:2890:3890 |
hadoop104:2888:3888 |
先在 hadoop102 上操作最后分发文件,改个 myid 就行,也可以使用 MultiExec 功能一次向所有终端发送命令
tar -zxvf apache-zookeeper-3.5.7-bin.tar.gz -C /opt/
mv /opt/apache-zookeeper-3.5.7-bin /opt/zookeeper-3.5.7
vim /etc/profile.d/zookeeper.sh
添加下述内容:
export ZOOKEEPER_HOME=/opt/zookeeper-3.5.7
export PATH=$PATH:$ZOOKEEPER_HOME/bin
更新环境变量
source /etc/profile
hadoop102 的 myid 设置为 1,hadoop103 为 2,hadoop104 为 3
mkdir -p $ZOOKEEPER_HOME/tmp/data
echo 1 > $ZOOKEEPER_HOME/tmp/data/myid
把 $ZOOKEEPER_HOME/conf
这个目录下的 zoo_sample.cfg
复制为 zoo.cfg
cp $ZOOKEEPER_HOME/conf/zoo_sample.cfg $ZOOKEEPER_HOME/conf/zoo.cfg
修改配置
vim $ZOOKEEPER_HOME/conf/zoo.cfg
修改数据存储路径
dataDir=/opt/zookeeper-3.5.7/tmp/data
添加下述配置
server.1=hadoop102:2888:3888
server.2=hadoop103:2888:3888
server.3=hadoop104:2888:3888
下述配置参数含义
server.A=B:C:D
myid
;IP
地址;Leader
服务器交换信息的端口;Leader
服务器挂了,需要重新选举出一个新的 Leader
时,在这个端口进行服务器相互通信;Leader
挂了,重新选举端口。集群分发 zookeeper
目录和配置文件
xsync $ZOOKEEPER_HOME
xsync /etc/profile.d/zookeeper.sh
最后不要忘记去 hadoop102,hadoop104 上将 myid 改为 2,3 ,其他集群如果使用 $ZOOKEEPER_HOME
需要先 source /etc/profile
更新一下环境变量
[user@hadoop103 ~]$ echo 2 > $ZOOKEEPER_HOME/tmp/data/myid
[user@hadoop104 ~]$ echo 3 > $ZOOKEEPER_HOME/tmp/data/myid
在每个服务器上启动 Zookeeper
[user@hadoop102 zookeeper-3.5.7]$ zkServer.sh start
[user@hadoop103 zookeeper-3.5.7]$ zkServer.sh start
[user@hadoop104 zookeeper-3.5.7]$ zkServer.sh start
使用 jpsall
查看进程,QuorumPeerMain
就是 Zookeeper
集群的启动入口类,用来加载配置启动 QuorumPeer
线程的
[user@hadoop102 ~]$ ./jpsall
=============== hadoop102 ===============
11507 QuorumPeerMain
12988 Jps
=============== hadoop103 ===============
14498 Jps
12887 QuorumPeerMain
=============== hadoop104 ===============
12471 Jps
11086 QuorumPeerMain
查看集群状态,按照半数选举机制三个服务器中第二个启动的集群就是 Leader
[user@hadoop102 zookeeper-3.5.7]$ zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.7/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
[user@hadoop103 zookeeper-3.5.7]$ zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.7/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
[user@hadoop104 zookeeper-3.5.7]$ zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.7/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
此时可以尝试将 hadoop103
断掉按照投票机制应当是 hadoop104
当选 Leader
vim zk.sh
给执行权限
chmod 777 zk.sh
添加如下内容:
#!/bin/bash
if [ $# -lt 1 ]
then
echo "No Args Input..."
exit ;
fi
case $1 in
"start")
echo "=========== 启动 zookeeper 集群 ==========="
echo "------------- hadoop102 -----------------"
ssh hadoop102 "$ZOOKEEPER_HOME/bin/zkServer.sh start"
echo "------------- hadoop103 -----------------"
ssh hadoop103 "$ZOOKEEPER_HOME/bin/zkServer.sh start"
echo "------------- hadoop104 -----------------"
ssh hadoop104 "$ZOOKEEPER_HOME/bin/zkServer.sh start"
;;
"stop")
echo "=========== 关闭 zookeeper 集群 ==========="
echo "------------- hadoop102 -----------------"
ssh hadoop102 "$ZOOKEEPER_HOME/bin/zkServer.sh stop"
echo "------------- hadoop103 -----------------"
ssh hadoop103 "$ZOOKEEPER_HOME/bin/zkServer.sh stop"
echo "------------- hadoop104 -----------------"
ssh hadoop104 "$ZOOKEEPER_HOME/bin/zkServer.sh stop"
;;
"status")
echo "=========== 查看 zookeeper 状态 ==========="
echo "------------- hadoop102 -----------------"
ssh hadoop102 "$ZOOKEEPER_HOME/bin/zkServer.sh status"
echo "------------- hadoop103 -----------------"
ssh hadoop103 "$ZOOKEEPER_HOME/bin/zkServer.sh status"
echo "------------- hadoop104 -----------------"
ssh hadoop104 "$ZOOKEEPER_HOME/bin/zkServer.sh status"
;;
*)
echo "Input Args Error..."
;;
esac
1)启动 zookeeper 集群
./zk.sh start
=========== 启动 zookeeper 集群 ===========
------------- hadoop102 -----------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.7/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
------------- hadoop103 -----------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.7/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
------------- hadoop104 -----------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.7/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
2)查看集群各服务器状态
./zk.sh status
=========== 查看 zookeeper 状态 ===========
------------- hadoop102 -----------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.7/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
------------- hadoop103 -----------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.7/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
------------- hadoop104 -----------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.7/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
3)关闭 zookeeper
集群
./zk.sh stop
=========== 关闭 zookeeper 集群 ===========
------------- hadoop102 -----------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.7/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
------------- hadoop103 -----------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.7/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
------------- hadoop104 -----------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.7/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
Zookeeper 集群搭建过程中常见错误-CSDN博客