前言
本节内容我们主要介绍一下如何在centos系统下搭建一套高可用的zookeeper集群,zookeeper是我们常用的中间键之一,例如使用zookeeper实现分布式锁、Hadoop集群高可用、kafka集群高可用等等。我们以以下三台服务器为例:
IP地址 | 主机名 |
---|---|
192.168.10.101 | hadoop101 |
192.168.10.102 | hadoop102 |
192.168.10.103 | hadoop103 |
正文
①上传zookeeper安装包到101服务器/opt/software目录下
②解压zookeeper安装包到/opt/module目录下
命令:
tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/
③创建zookeeper数据存储目录zkdata
命令:
mkdir -p /opt/module/zookeeper-3.4.10/zkdata
④分发zookeeper安装包到其它俩台服务器
命令:
hsync zookeeper-3.4.10
⑤分别在三台服务的zookeeper数据目录下创建myid文件,并分别写入内容1、2、3
⑥ 修改zookeeper配置文件
# 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=/opt/module/zookeeper-3.4.10/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
#######################zookeeper cluster config##########################
server.1=hadoop101:2888:3888
server.2=hadoop102:2888:3888
server.3=hadoop103:2888:3888
配置参数说明:
server.A=B:C:D
- A:是一个数字,表示这个是第几号服务器,集群模式下配置一个文件myid,这个文件在dataDir目录下,这个文件里面有一个数据就是A的值,Zookeeper启动时读取此文件,拿到里面的数据与zoo.cfg里面的配置信息比较从而判断到底是哪个server
- B:这个服务器的ip地址或者主机名(需映射)
- C:服务器与集群中的Leader服务器交换信息的端口,用于传递数据
- D:集群中的Leader服务器挂了,需要一个端口来重新进行选举,选出一个新的Leader,而这个端口就是用来执行选举时服务器相互通信的端口
⑦同步zookeeper配置文件zoo.cfg到其它服务器
⑧分别启动zookeeper,并查看zookeeper集群状态
命令:
#启动
bin/zkServer.sh start
#查看状态
bin/zkServer.sh status
#停止
bin/zkServer.sh stop
⑨在/opt/module/zookeeper-3.4.10/bin目录下创建一个zookeeper集群启动、关闭、查看状态的脚本myzookeeper.sh
#!/bin/bash
if [ $# -lt 1 ]
then
echo "No Args Input..."
exit ;
fi
case $1 in
"start")
echo " =================== 启动 zookeeper 集群 ==================="
ssh hadoop101 "/opt/module/zookeeper-3.4.10/bin/zkServer.sh start"
ssh hadoop102 "/opt/module/zookeeper-3.4.10/bin/zkServer.sh start"
ssh hadoop103 "/opt/module/zookeeper-3.4.10/bin/zkServer.sh start"
;;
"stop")
echo " =================== 关闭 zookeeper 集群 ==================="
ssh hadoop101 "/opt/module/zookeeper-3.4.10/bin/zkServer.sh stop"
ssh hadoop102 "/opt/module/zookeeper-3.4.10/bin/zkServer.sh stop"
ssh hadoop103 "/opt/module/zookeeper-3.4.10/bin/zkServer.sh stop"
;;
"status")
echo " =================== 查看 zookeeper 集群状态 ==================="
ssh hadoop101 "/opt/module/zookeeper-3.4.10/bin/zkServer.sh status"
ssh hadoop102 "/opt/module/zookeeper-3.4.10/bin/zkServer.sh status"
ssh hadoop103 "/opt/module/zookeeper-3.4.10/bin/zkServer.sh status"
;;
*)
echo "Input Args Error..."
;;
esac
⑩配置zookeeper环境变量,并使环境变量生效source /etc/profile
结语
至此,关于zookeeper集群的搭建安装到这里就结束了,我们下期见。。。。。。