zookeeper集群搭建
安装环境:服务器操作系统 CentOS 7.0
目标:搭建一台三节点的zookeeper伪分布集群
1、下载与安装zookeeper
(1)、选择稳定版本(3.4.10)进行下载,官网下载,下载完成后则通过远程连接工具将安装包上传到服务器。
(2)、解压缩安装包
[dliang@itbean03 bigdata]$ mkdir zookeeper
[dliang@itbean03 bigdata]$ cd zookeeper/
[dliang@itbean03 zookeeper]$ ll
total 34224
-rw-rw-r--. 1 dliang dliang 35042811 Nov 27 22:37 zookeeper-3.4.10.tar.gz
[dliang@itbean03 zookeeper]$ tar -zxvf zookeeper-3.4.10.tar.gz
[dliang@itbean03 zookeeper]$ ll
total 34228
drwxr-xr-x. 10 dliang dliang 4096 Mar 23 2017 zookeeper-3.4.10
-rw-rw-r--. 1 dliang dliang 35042811 Nov 27 22:37 zookeeper-3.4.10.tar.gz
2、单机版启动与停止
进入到解压后的zookeeper-3.4.10目录下,可以看到如下结构
暂时需要关注的是 bin 与conf目录
bin目录:是zookeeper的server与client的可执行脚本文件
conf目录:则是zookeeper的配置文件目录
(1)、复制conf目录下的“zoo_sample.cfg”文件,并且将文件名修改为“zoo.cfg”
[aaa@qq.com zookeeper-3.4.10]$ cp conf/zoo_sample.cfg conf/zoo.cfg
(2)、修改单一节点的配置文件
[dliang@itbean03 zookeeper-3.4.10]$ vim conf/zoo.cfg
默认会有不少的选项,单机版只是测试,暂不做修改
(3)、通过zkServer启动zookeeper的服务端
[dliang@itbean03 zookeeper-3.4.10]$ bin/zkServer.sh
ZooKeeper JMX enabled by default
Using config: /home/dliang/bigdata/zookeeper/zookeeper-3.4.10/bin/../conf/zoo.cfg
Usage: bin/zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
当直接执行“bin/zkServer.sh ” 时,系统会有提示选择选项
接着我们就直接启动其他的方式暂不做测试
[aaa@qq.com zookeeper-3.4.10]$ bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/dliang/bigdata/zookeeper/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
(4)、通过zkCli客户端观察zookeeper
通过执行脚本,zkCli.sh 即可进行对zookeeper的操作
[dliang@itbean03 zookeeper-3.4.10]$ bin/zkCli.sh
通过在zkCli的shell输入help,则会有对应的提示,如下图
通过“ ls path [watch]”可以查看zookeeper的目录内容
[zk: localhost:2181(CONNECTED) 6] ls /zookeeper
[quota]
(5)、关闭zkServer
[dliang@itbean03 zookeeper-3.4.10]$ bin/zkServer.sh stop
3、集群配置文件的配置
(1)、创建集群配置文件的目录结构
(1.1)、创建zk-cluster-data
(1.2)、创建zk-cluster-conf
(1.3)、创建zk-cluster-log
#创建zk-cluster-data
[dliang@itbean03 zookeeper-3.4.10]$ mkdir -p zk-cluster-data/zk1
[dliang@itbean03 zookeeper-3.4.10]$ mkdir -p zk-cluster-data/zk2
[dliang@itbean03 zookeeper-3.4.10]$ mkdir -p zk-cluster-data/zk3
#创建zk-cluster-conf
[dliang@itbean03 zookeeper-3.4.10]$ mkdir zk-cluster-conf
#创建zk-cluster-log
[dliang@itbean03 zookeeper-3.4.10]$ mkdir zk-cluster-log/zk1
[dliang@itbean03 zookeeper-3.4.10]$ mkdir zk-cluster-log/zk2
[dliang@itbean03 zookeeper-3.4.10]$ mkdir zk-cluster-log/zk3
(2)、配置myid
在zk-cluster-data/zk1、zk-cluster-data/zk2、zk-cluster-data/zk3中创建文件名为”myid”,并在上述三个文件中分别填写1,2,3
#分别创建三个myid文件
[dliang@itbean03 zookeeper-3.4.10]$ touch zk-cluster-data/zk1/myid
[dliang@itbean03 zookeeper-3.4.10]$ touch zk-cluster-data/zk2/myid
[dliang@itbean03 zookeeper-3.4.10]$ touch zk-cluster-data/zk3/myid
#写入myid
[dliang@itbean03 zookeeper-3.4.10]$ echo 1 > zk-cluster-data/zk1/myid
[dliang@itbean03 zookeeper-3.4.10]$ echo 2 > zk-cluster-data/zk1/myid
[dliang@itbean03 zookeeper-3.4.10]$ echo 3 > zk-cluster-data/zk1/myid
(3)、配置zk-cluster-conf
(3.1)、复制zoo.cfg到zk-cluster-conf中三个配置文件,分别为zoo1.cfg、zoo2.cfg、zoo3.cfg
[aaa@qq.com zookeeper-3.4.10]$ cp conf/zoo.cfg zk-cluster-conf/zoo1.cfg
[aaa@qq.com zookeeper-3.4.10]$ cp conf/zoo.cfg zk-cluster-conf/zoo2.cfg
[aaa@qq.com zookeeper-3.4.10]$ cp conf/zoo.cfg zk-cluster-conf/zoo3.cfg
(3.2)、修改配置文件
三个配置文件类似,只需要修改个别不同的地方
(3.3)、启动集群
#启动集群
[aaa@qq.com zookeeper-3.4.10]$ bin/zkServer.sh start zk-cluster-conf/zoo1.cfg
ZooKeeper JMX enabled by default
Using config: zk-cluster-conf/zoo1.cfg
Starting zookeeper ... STARTED
[aaa@qq.com zookeeper-3.4.10]$ bin/zkServer.sh start zk-cluster-conf/zoo2.cfg
ZooKeeper JMX enabled by default
Using config: zk-cluster-conf/zoo2.cfg
Starting zookeeper ... STARTED
[aaa@qq.com zookeeper-3.4.10]$ bin/zkServer.sh start zk-cluster-conf/zoo3.cfg
ZooKeeper JMX enabled by default
Using config: zk-cluster-conf/zoo3.cfg
Starting zookeeper ... STARTED
#查看集群状态
[aaa@qq.com zookeeper-3.4.10]$ bin/zkServer.sh status zk-cluster-conf/zoo3.cfg
ZooKeeper JMX enabled by default
Using config: zk-cluster-conf/zoo3.cfg
Mode: follower
[aaa@qq.com zookeeper-3.4.10]$ bin/zkServer.sh status zk-cluster-conf/zoo2.cfg
ZooKeeper JMX enabled by default
Using config: zk-cluster-conf/zoo2.cfg
Mode: leader
[aaa@qq.com zookeeper-3.4.10]$ bin/zkServer.sh status zk-cluster-conf/zoo1.cfg
ZooKeeper JMX enabled by default
Using config: zk-cluster-conf/zoo1.cfg
Mode: follower