ZooKeeper简介与安装
ZooKeeper是一个非常流行的分布式系统协调服务。目前的应用非常广泛。
ZooKeeper简介
一个分布式的协调服务
特点
- 分布式集群
- 高可用
- 奇数个节点
功能
- 管理应用的状态数据
- 提供对状态数据的监听服务
ZooKeeper的应用场景
服务器主从选举
目前有多个服务器提供服务,但是为了保证数据的一致性,每一时刻只能有一个服务器来提供服务,其他服务器作为主服务器的备份,如果主服务器挂了,备份的服务器成为主服务器。
批量更新配置
如果我们有多个服务器节点需要更新同样的配置文件内容,逐步的手工修改是非常繁琐的,而且也很容易出错。此时将配置文件存放在第三方,如果配置文件修改之后由ZooKeeper通知其他节点,各节点自己读取配置文件即可。
其他
- 分布式锁
- 客户端高可用
ZooKeeper集群的角色分配
一开始时并没有规定哪个节点是主节点,哪些节点是从节点。在ZooKeeper集群中各节点启动完成后各节点选举(Zab算法)来产生主节点。
在写入数据时,新的数据被传递给Leader,Leader写入后通知各Follower来更新数据来保证数据的一致。如果ZooKeeper集群规模很大,数据的一致性会有很长的延时,此时客户端看到的数据可能不一样,因此ZooKeeper不适合频繁更新,实时性要求很高的应用。
ZooKeeper安装
由于ZooKeeper的半数安装机制,所以至少装3台。主要的工作是配置myid和节点列表。
因为我们的集群不暴露给外界,而且其他的框架
安装JDK
这步省略了
安装过程
1.下载ZooKeeper
http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
2.安装到主节点
上传文件
put -r "C:\Users\Yang\Desktop\zookeeper-3.4.9.tar.gz"
解压到指定目录
tar -zxvf zookeeper-3.4.9.tar.gz -C /root/apps
修改配置文件zoo.cfg
cd /root/apps/zookeeper-3.4.9/conf
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
修改配置文件主要有几个地方,一个是dataDir,一个是server的列表。
下面给出一个配置模板。
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/root/apps/zookeeper-3.4.9/zkData
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=amaster:2888:3888
server.2=anode1:2888:3888
server.3=anode2:2888:3888
然后要把myid配置到数据目录下面。
mkdir -p /root/apps/zookeeper-3.4.9/zkData
cd /root/apps/zookeeper-3.4.9/zkData
echo 1 >> myid
3.将ZooKeeper安装到其他节点中
从主节点将ZooKeeper拷贝到其他节点中
scp -r /root/apps/zookeeper-3.4.9/ root@anode1:/root/apps/zookeeper-3.4.9
scp -r /root/apps/zookeeper-3.4.9/ root@anode2:/root/apps/zookeeper-3.4.9
在其他节点上修改myid的值分别为2和3
echo 2 > /root/apps/zookeeper-3.4.9/zkData/myid
关闭防火墙,在所有节点上执行
ufw disable
4.启动ZooKeeper
在所有节点上执行下列命令开启ZooKeeper。
/root/apps/zookeeper-3.4.9/bin/zkServer.sh start
然后可以通过下列命令查看ZooKeeper的状态。
/root/apps/zookeeper-3.4.9/bin/zkServer.sh status
如果出现类似的结果,则说明启动成功。
ZooKeeper JMX enabled by default
Using config: /root/apps/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: leader
否则可以在ZooKeeper.out文件中查看启动日志。
下面再给出一个自动启动所有机器上的ZooKeeper的脚本。
#!/bin/sh
SERVERS="amaster anode1 anode2"
start_zookeeper_all() {
for SERVER in $SERVERS
do
ssh $SERVER "source ~/.bashrc;/root/apps/zookeeper-3.4.9/bin/zkServer.sh stop"
ssh $SERVER "source ~/.bashrc;/root/apps/zookeeper-3.4.9/bin/zkServer.sh start"
done
for SERVER in $SERVERS
do
ssh $SERVER "source ~/.bashrc;/root/apps/zookeeper-3.4.9/bin/zkServer.sh status"
done
}
start_zookeeper_all
如果启动不了,可能是JAVA_HOME没有被导出,需要将~/.bashrc中的
export JAVA_HOME=/root/apps/jdk1.8.0_111
export PATH=$PATH:$JAVA_HOME/bin
放到
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
的前面。