什么是ZooKeeper,看看ZooKeeper官网怎么说:
Apache ZooKeeper is an effort to develop and maintain an open-source server which enables highly reliable distributed coordination.
ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.
点此进入:ZooKeeper Overview
ZooKeeper是分布式应用环境中的基础组件,在hadoop集群中,hdfs和yarn都要用到ZooKeeper来配置可自动切换的高可用(HA)系统,本文根据ZooKeeper官方文档,选用三台虚拟机,部署ZooKeeper集群,用到的虚拟机为前文提到的: hadoop01、hadoop02、hadoop03。以下为详细安装步骤。
1、在hadoop01机器上,创建zookeeper用户,加入hadoop组(本系列文档中有关于创建hadoop组的部分)
#以root用户登录hadoop01机器 [[email protected] hadoop]# useradd -m -g hadoop zookeeper #首次启用,设置zookeeper用户密码 |
2、下载最新的ZooKeeper发布包:官网推荐的镜像地址
#zookeeper-3.4.8.tar.gz拷贝至 /home/zookeeper 目录 [[email protected] ~]$ tar zxvf zookeeper-3.4.8.tar.gz 解压后多一个文件夹 zookeeper-3.4.8 |
3、使用zookeeper用户登录,设置环境变量
#在hadoop01机器上以zookeeper用户身份操作 [[email protected] hadoop]# su - zookeeper [[email protected] ~]$ vi .bash_profile export PATH #add end 20160628 #保存以上环境配置,退出vi后,执行以下命令是环境变量生效 [[email protected] ~]$ source .bash_profile |
4、在hadoop01机器上设置zookeeper相关参数
[[email protected] ~]$ cd zookeeper-3.4.8/conf/ #从样例复制一个配置文件模板 [[email protected] zookeeper-3.4.8]$ cp zoo_sample.cfg zoo.cfg # The number of milliseconds of each tick tickTime=2000 # example sakes. #modify start 20160628 #dataDir=/tmp/zookeeper dataDir=/home/zookeeper/zookeeper-3.4.8/zkdata #modify end 20160628 # the port at which the clients will connect clientPort=2181 #autopurge.purgeInterval=1 #添加的配置信息 #modify start 20160628 server.1=hadoop01:2888:3888 server.3=hadoop03:2888:3888 #modify end 20160628 |
5、在hadoop01机器上创建工作目录,设置zookeeper ID值(在不同服务器上ID不能重复)
#在hadoop01机器上,以zookeeper用户执行以下命令 [[email protected] ~]$ cd /home/zookeeper/zookeeper-3.4.8 [[email protected] zookeeper-3.4.8]$ mkdir zkdata |
6、在hadoop02上执行相关操作
#在hadoop02机器上,创建zookeeper用户 [[email protected] hadoop]# useradd -m -g hadoop zookeeper #在hadoop01机器上,执行以下操作 [[email protected] ~]$ cd /home/zookeeper [[email protected] ~]$ scp -r zookeeper-3.4.8 hadoop02:$PWD #在hadoop02机器上,切换zookeeper用户身份 [[email protected] hadoop]# su - zookeeper [[email protected] ~]$ vi .bash_profile export PATH #add end 20160628 #保存以上环境配置,退出vi后,执行以下命令使环境变量生效 [[email protected] ~]$ source .bash_profile |
7、创建工作目录,设置zookeeper ID值
#在hadoop02机器上,以zookeeper用户执行以下命令 [[email protected] ~]$ cd /home/zookeeper/zookeeper-3.4.8 [[email protected] zookeeper-3.4.8]$ mkdir zkdata |
8、在hadoop03上执行相关操作
#在hadoop03机器上,创建zookeeper用户 [[email protected] hadoop]# useradd -m -g hadoop zookeeper #在hadoop01机器上,执行以下操作 [[email protected] ~]$ cd /home/zookeeper [[email protected] ~]$ scp -r zookeeper-3.4.8 hadoop03:$PWD #在hadoop03机器上,切换zookeeper用户身份 [[email protected] hadoop]# su - zookeeper [[email protected] ~]$ vi .bash_profile export PATH #add end 20160628 #保存以上环境配置,退出vi后,执行以下命令使环境变量生效 [[email protected] ~]$ source .bash_profile |
9、创建工作目录,设置zookeeper ID值
#在hadoop03机器上,以zookeeper用户执行以下命令 [[email protected] ~]$ cd /home/zookeeper/zookeeper-3.4.8 [[email protected] zookeeper-3.4.8]$ mkdir zkdata |
10、启动zookeeper集群
#在hadoop01机器上运行 [[email protected] ~]$ zkServer.sh start ZooKeeper JMX enabled by default #在hadoop02机器上运行 [[email protected] ~]$ zkServer.sh start #在hadoop03机器上运行 [[email protected] ~]$ zkServer.sh start |
11、查看zookeeper集群状态
[[email protected] ~]$ zkServer.sh status ZooKeeper JMX enabled by default Using config: /home/zookeeper/zookeeper-3.4.8/bin/../conf/zoo.cfg Mode: follower [[email protected] ~]$ zkServer.sh status |
12、查看执行的进程
[[email protected] ~]$ jps -l 5449 org.apache.zookeeper.server.quorum.QuorumPeerMain |
13、关闭zookeeper集群
#在hadoop01机器上运行 [[email protected] ~]$ zkServer.sh stop #在hadoop02机器上运行 [[email protected] ~]$ zkServer.sh stop #在hadoop03机器上运行 [[email protected] ~]$ zkServer.sh stop |
14、下图演示了开启zookeeper集群,以及杀掉leader服务器的进程后,自动选举新的leader服务器情况。