[email protected] 2017-12-21 21:17
写在前面的废话
大数据之所以日渐崛起,是因为海量数据的处理,必须依靠集群的威力,而集群又带来了一个新问题,那就是成百上千的这么多机器,如何协调一致,如何满足既可用又一致的要求? ZooKeeper就是其中一个答案.
推荐一本书可以看看,阿里的人写的
开始
ZooKeeper环境搭建前提条件
- 已经按照之前的文章装好了三台虚拟机,并已配置好CentOS 7,保证三台机器之间可以网络互通,可以ssh免密登录
- JDK已安装,并配好环境变量
- 在ZooKeeper官网下载了软件包(zookeeper-3.4.10.tar.gz),并放在 /home/libing/softwares 目录下
ZooKeeper环境搭建步骤(都以提前建好的普通用户libing来操作)
1. 解压软件包至用户libing的家目录下
[libing@bd-1 ~]$ tar -xzf softwares/zookeeper-3.4.10.tar.gz -C /home/libing/
[libing@bd-1 ~]$ ll
总用量 8
drwxr-xr-x. 8 libing libing 255 9月 14 17:27 jdk1.8.0_152
drwxrwxr-x. 2 libing libing 148 12月 20 20:09 softwares
drwxr-xr-x 10 libing libing 4096 3月 23 2017 zookeeper-3.4.10
2. 编辑 /etc/profile 文件,给系统增加环境变量ZOOKEEPER_HOME,以及修改PATH环境变量
[libing@bd-1 ~]$ sudo vi /etc/profile
[sudo] libing 的密码:
在文件内容的最后添加以下内容
# Added for ZooKeeper
ZOOKEEPER_HOME=/home/libing/zookeeper-3.4.10
export ZOOKEEPER_HOME
PATH=${ZOOKEEPER_HOME}/bin:$PATH
export PATH
保存退出vi
3. 使之马上生效,以及进行验证
[libing@bd-1 ~]$ source /etc/profile
[libing@bd-1 ~]$ env | grep ZOOK
ZOOKEEPER_HOME=/home/libing/zookeeper-3.4.10
[libing@bd-1 ~]$ ls zookeeper-3.4.10/bin
README.txt zkCleanup.sh zkCli.cmd zkCli.sh zkEnv.cmd zkEnv.sh zkServer.cmd zkServer.sh
[libing@bd-1 ~]$ which zkServer.sh
~/zookeeper-3.4.10/bin/zkServer.sh
简单解释一下上面的命令
- source是执行脚本的命令,将读取/etc/profile的内容,并逐一执行
- env是输出当前用户所有的环境变量
- grep是搜索过滤的命令,env | grep ZOOK 表示把env的输出,作为grep命令的输入,并在其中查找包含ZOOK字符的行,打印出来
- which命令可以在当前的PATH环境变量包含的所有路径中,寻找某个文件,如果找到了,就把这个文件的路径加文件名打印出来
4. 进入ZooKeeper的conf目录,复制并编辑zoo.cfg配置文件
[libing@bd-1 ~]$ cd zookeeper-3.4.10/conf
[libing@bd-1 conf]$ ls
configuration.xsl log4j.properties zoo_sample.cfg
[libing@bd-1 conf]$ cp zoo_sample.cfg zoo.cfg
[libing@bd-1 conf]$ vi zoo.cfg
在文件的最后增加如下内容
# store snapshot files
dataDir=/home/libing/zookeeper-3.4.10/data
# store transaction log files
dataLogDir=/home/libing/zookeeper-3.4.10/log
server.1=bd-1:2888:3888
server.2=bd-2:2888:3888
server.3=bd-3:2888:3888
保存后退出vi.
注意上面内容中的路径,以及bd-1等主机名,需要根据你自己虚拟机的情况进行改动.
5. 在ZooKeeper软件目录下创建data和log目录,并修改权限属性
[libing@bd-1 conf]$ cd ~/zookeeper-3.4.10
[libing@bd-1 zookeeper-3.4.10]$ mkdir data log
[libing@bd-1 zookeeper-3.4.10]$ chmod 755 data log
6. 创建myid文件并填入内容(以bd-1虚拟机上操作为例)
[libing@bd-1 zookeeper-3.4.10]$ echo 1 > data/myid
[libing@bd-1 zookeeper-3.4.10]$ cat data/myid
1
7. 启动ZooKeeper服务,并查看状态
这是bd-1虚拟机的
[libing@bd-1 zookeeper-3.4.10]$ zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/libing/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[libing@bd-1 zookeeper-3.4.10]$ zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/libing/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
这是bd-2虚拟机的
[libing@bd-2 zookeeper-3.4.10]$ zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/libing/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[libing@bd-2 zookeeper-3.4.10]$ zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/libing/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader
这是bd-3虚拟机的
[libing@bd-3 zookeeper-3.4.10]$ zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/libing/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[libing@bd-3 zookeeper-3.4.10]$ zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/libing/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
可以看到bd-2虚拟机的ZooKeeper是leader,其他两台都是follower.
8. 停止ZooKeeper
在每台虚拟机上执行(以bd-1虚拟机为例)
[libing@bd-1 zookeeper-3.4.10]$ zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /home/libing/zookeeper-3.4.10/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
9. 使用一个脚本实现所有虚拟机的ZooKeeper启动/关闭
创建ZooKeeper集群启动脚本(存放于bd-1虚拟机并执行)
[libing@bd-1 ~]$ vi zkStartAll.sh
脚本内容如下
#!/bin/bash
echo "Start ZooKeeper service ..."
for i in 1 2 3
do
ssh bd-$i "source /etc/profile ; zkServer.sh start"
done
echo "ZooKeeper started!"
执行
[libing@bd-1 ~]$ chmod u+x zkStartAll.sh
[libing@bd-1 ~]$ ./zkStartAll.sh
创建ZooKeeper集群关闭脚本(存放于bd-1虚拟机并执行)
[libing@bd-1 ~]$ vi zkStopAll.sh
脚本内容如下
#!/bin/bash
echo "Stop ZooKeeper service ..."
for i in 1 2 3
do
ssh bd-$i "source /etc/profile ; zkServer.sh stop"
done
echo "ZooKeeper stopped!"
执行
[libing@bd-1 ~]$ chmod u+x zkStopAll.sh
[libing@bd-1 ~]$ ./zkStopAll.sh
可以分别在每台虚拟机上使用 zkServer.sh status
验证脚本的效果.
注意点: 三台虚拟机之间的ssh免密登录一定要配置好,包括每个虚拟机ssh登录本身自己也要免密
ZooKeeper运维操作实验
1. 进入客户端zkCli.sh
不使用任何参数的话,就是连接本地的ZooKeeper.
[libing@bd-1 ~]$ zkCli.sh
Connecting to localhost:2181
2017-12-25 10:31:20,623 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2017-12-25 10:31:20,628 [myid:] - INFO [main:Environment@100] - Client environment:host.name=bd-1
2017-12-25 10:31:20,628 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_152
2017-12-25 10:31:20,633 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-12-25 10:31:20,634 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/home/libing/jdk1.8.0_152/jre
2017-12-25 10:31:20,635 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/home/libing/zookeeper-3.4.10/bin/../build/classes:/home/libing/zookeeper-3.4.10/bin/../build/lib/*.jar:/home/libing/zookeeper-3.4.10/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/libing/zookeeper-3.4.10/bin/../lib/slf4j-api-1.6.1.jar:/home/libing/zookeeper-3.4.10/bin/../lib/netty-3.10.5.Final.jar:/home/libing/zookeeper-3.4.10/bin/../lib/log4j-1.2.16.jar:/home/libing/zookeeper-3.4.10/bin/../lib/jline-0.9.94.jar:/home/libing/zookeeper-3.4.10/bin/../zookeeper-3.4.10.jar:/home/libing/zookeeper-3.4.10/bin/../src/java/lib/*.jar:/home/libing/zookeeper-3.4.10/bin/../conf:
2017-12-25 10:31:20,636 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2017-12-25 10:31:20,636 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-12-25 10:31:20,636 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=
2017-12-25 10:31:20,637 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2017-12-25 10:31:20,637 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2017-12-25 10:31:20,637 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-693.11.1.el7.x86_64
2017-12-25 10:31:20,638 [myid:] - INFO [main:Environment@100] - Client environment:user.name=libing
2017-12-25 10:31:20,638 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/home/libing
2017-12-25 10:31:20,638 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/libing
2017-12-25 10:31:20,642 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@7aec35a
Welcome to ZooKeeper!
2017-12-25 10:31:20,692 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2017-12-25 10:31:20,836 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session
2017-12-25 10:31:20,855 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x1608b7c924d0001, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
退出zkCli
[zk: localhost:2181(CONNECTED) 0] quit
Quitting...
2017-12-25 10:31:26,565 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x1608b7c924d0001 closed
2017-12-25 10:31:26,569 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x1608b7c924d0001
[libing@bd-1 ~]$
在bd-1虚拟机上连接bd-2上的ZooKeeper
[libing@bd-1 ~]$ zkCli.sh -server bd-2:2181
Connecting to bd-2:2181
2017-12-25 10:31:57,966 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2017-12-25 10:31:57,970 [myid:] - INFO [main:Environment@100] - Client environment:host.name=bd-1
2017-12-25 10:31:57,970 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_152
2017-12-25 10:31:57,972 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-12-25 10:31:57,972 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/home/libing/jdk1.8.0_152/jre
2017-12-25 10:31:57,973 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/home/libing/zookeeper-3.4.10/bin/../build/classes:/home/libing/zookeeper-3.4.10/bin/../build/lib/*.jar:/home/libing/zookeeper-3.4.10/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/libing/zookeeper-3.4.10/bin/../lib/slf4j-api-1.6.1.jar:/home/libing/zookeeper-3.4.10/bin/../lib/netty-3.10.5.Final.jar:/home/libing/zookeeper-3.4.10/bin/../lib/log4j-1.2.16.jar:/home/libing/zookeeper-3.4.10/bin/../lib/jline-0.9.94.jar:/home/libing/zookeeper-3.4.10/bin/../zookeeper-3.4.10.jar:/home/libing/zookeeper-3.4.10/bin/../src/java/lib/*.jar:/home/libing/zookeeper-3.4.10/bin/../conf:
2017-12-25 10:31:57,973 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2017-12-25 10:31:57,973 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-12-25 10:31:57,973 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=
2017-12-25 10:31:57,973 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2017-12-25 10:31:57,973 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2017-12-25 10:31:57,973 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-693.11.1.el7.x86_64
2017-12-25 10:31:57,974 [myid:] - INFO [main:Environment@100] - Client environment:user.name=libing
2017-12-25 10:31:57,974 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/home/libing
2017-12-25 10:31:57,974 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/libing
2017-12-25 10:31:57,976 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=bd-2:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@7aec35a
Welcome to ZooKeeper!
2017-12-25 10:31:58,022 [myid:] - INFO [main-SendThread(bd-2:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server bd-2/192.168.206.133:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2017-12-25 10:31:58,152 [myid:] - INFO [main-SendThread(bd-2:2181):ClientCnxn$SendThread@876] - Socket connection established to bd-2/192.168.206.133:2181, initiating session
2017-12-25 10:31:58,173 [myid:] - INFO [main-SendThread(bd-2:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server bd-2/192.168.206.133:2181, sessionid = 0x2608b7c91640001, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: bd-2:2181(CONNECTED) 0]
查看help帮助信息
[zk: bd-2:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getAcl path
close
connect host:port
总结:
三台机器的配置和操作几乎完全一样,区别就在于myid文件内容不一样,bd-1虚拟机的myid文件内容是1,bd-2虚拟机的myid文件内容是2,bd-3虚拟机的myid文件内容是3,其他都完全一样.
其他的运维命令什么的,大家就把ZooKeeper集群服务起来后,照着文档慢慢尝试吧.