文章目录
- Ububtu18.04安装Zookeeper3.5.7
- Zookeeper安装
- standlone模式安装
- 配置伪分布式的Zookeeper
- 配置全分布式的Zookeeper
- Zookeeper安装
Ububtu18.04安装Zookeeper3.5.7
Zookeeper安装
这里采用的ubuntu18.04环境的基本配置为:
代码语言:javascript复制hostname 为master
用户名为hadoop
静态IP为 192.168.33.130
网关为 192.168.33.2
防火墙已经关闭
/etc/hosts已经配置
standlone模式安装
全版本下载地址: https://archive.apache.org/dist/zookeeper/ 这里我选择3.5.7这个版本 https://archive.apache.org/dist/zookeeper/zookeeper-3.5.7/
下载到本地后,将apache-zookeeper-3.5.7-bin.tar.gz上传到ubuntu18.04的/home/hadoop/opt/app目录下, 该目录下的目录作用如下:
代码语言:javascript复制bin目录 框架启动停止,客户端和服务端的
conf 配置文件信息
docs文档
lib 配置文档的依赖
然后执行
代码语言:javascript复制hadoop@master:~$ cd /home/hadoop/opt/app/
hadoop@master:~/opt/app$ tar -zxf apache-zookeeper-3.5.7-bin.tar.gz # 解压缩
hadoop@master:~/opt/app$ ln -s apache-zookeeper-3.5.7-bin zookeeper # 软连接
查看当前目录结构:
代码语言:javascript复制hadoop@master:~/opt/app$ pwd # 查看当前目录
/home/hadoop/opt/app
hadoop@master:~/opt/app$ ll
总用量 9120
drwxrwxr-x 6 hadoop hadoop 4096 11月 12 10:42 ./
drwxrwxr-x 3 hadoop hadoop 4096 11月 4 14:53 ../
drwxrwxr-x 6 hadoop hadoop 4096 11月 12 10:41 apache-zookeeper-3.5.7-bin/
-rw-rw-r-- 1 hadoop hadoop 9311744 11月 12 10:39 apache-zookeeper-3.5.7-bin.tar.gz
切换到~/opt/app/zookeeper,配置zookeeper的数据存储-data1目录
代码语言:javascript复制hadoop@master:~/opt/app$ cd zookeeper # 切换到zookeeper中
hadoop@master:~/opt/app/zookeeper$ ls
bin conf docs lib LICENSE.txt NOTICE.txt README.md README_packaging.txt
hadoop@master:~/opt/app/zookeeper$ mkdir data0 # 创建data0
hadoop@master:~/opt/app/zookeeper$ ls
bin conf data0 docs lib LICENSE.txt NOTICE.txt README.md README_packaging.txt
进入~/opt/app/zookeeper/conf目录下,编辑zoo.cfg
代码语言:javascript复制hadoop@master:~/opt/app/zookeeper$ cd conf/
hadoop@master:~/opt/app/zookeeper/conf$ ls
configuration.xsl log4j.properties zoo_sample.cfg
hadoop@master:~/opt/app/zookeeper/conf$ cp zoo_sample.cfg zoo.cfg #复制zoo.cfg
hadoop@master:~/opt/app/zookeeper/conf$ vi zoo.cfg # 编辑zoo.cfg文件,编辑内容如下:
# dataDir=/tmp/zookeeper 注释掉这句默认配置,然后添加下面的配置
dataDir=/home/hadoop/opt/app/zookeeper/data0
配置环境变量
代码语言:javascript复制hadoop@master:~/opt/app/zookeeper/conf$ vi ~/.bashrc
#这一步会打开bashrc文件,编辑内容如下:
export ZOOKEEPER_HOME=/home/hadoop/opt/app/zookeeper
export PATH=$ZOOKEEPER_HOME/bin:$PATH
hadoop@master:~/opt/app/zookeeper/conf$ source ~/.bashrc # 生效环境变量
启动zk服务端(单机版本)
代码语言:javascript复制hadoop@master:~/opt/app/zookeeper/conf$ zkServer.sh status #查看zk启动状态
ZooKeeper JMX enabled by default
Using config: /home/hadoop/opt/app/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Error contacting service. It is probably not running.
hadoop@master:~/opt/app/zookeeper/conf$ zkServer.sh start #启动zkServer
ZooKeeper JMX enabled by default
Using config: /home/hadoop/opt/app/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
hadoop@master:~/opt/app/zookeeper/conf$ zkServer.sh status #查看zk启动状态,单机启动为standalone模式
ZooKeeper JMX enabled by default
Using config: /home/hadoop/opt/app/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone
启动zk客户端
代码语言:javascript复制hadoop@master:~/opt/app/zookeeper/conf$ zkCli.sh #启动zkCli,默认连接端口2181
Connecting to localhost:2181
...
...
...
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] help
ZooKeeper -server host:port cmd args
addauth scheme auth
close
config [-c] [-w] [-s]
connect host:port
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
delete [-v version] path
deleteall path
delquota [-n|-b] path
get [-s] [-w] path
getAcl [-s] path
history
listquota path
ls [-s] [-w] [-R] path
ls2 path [watch]
printwatches on|off
quit
reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
redo cmdno
removewatches path [-c|-d|-a] [-l]
rmr path
set [-s] [-v version] path data
setAcl [-s] [-v version] [-R] path acl
setquota -n|-b val path
stat [-w] path
sync path
Command not found: Command not found help
[zk: localhost:2181(CONNECTED) 2] quit
WATCHER::
WatchedEvent state:Closed type:None path:null
2021-11-12 11:05:29,497 [myid:] - INFO [main:ZooKeeper@1422] - Session: 0x1000029eae60000 closed
2021-11-12 11:05:29,497 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down for session: 0x1000029eae60000
hadoop@master:~/opt/app/zookeeper/conf$
关闭zk服务端(单机版本)
代码语言:javascript复制zkServer.sh start # 启动zk服务端
zkServer.sh stop # 关闭zk服务端
zkServer.sh status # 查看zk服务端状态
配置伪分布式的Zookeeper
本小节的伪分布式集群部署是指在一台机器(master)上配置多个配置文件(zoo1.cfg,zoo2.cfg,zoo3.cfg),三个配置文件中设置不一样clientPort和dataDir,然后根据不同的配置文件来启动Zookeeper服务。 备注:本部分是在standlone已经完成安装的基础上进行的。
- 创建并生成各自的配置文件: 配置各个zk节点的配置文件,具体如下:
hadoop@master:~/opt/app/zookeeper/conf$ pwd
/home/hadoop/opt/app/zookeeper/conf
hadoop@master:~/opt/app/zookeeper/conf$ cp zoo.cfg zoo1.cfg
hadoop@master:~/opt/app/zookeeper/conf$ cp zoo.cfg zoo2.cfg
hadoop@master:~/opt/app/zookeeper/conf$ cp zoo.cfg zoo3.cfg
分布修改这三个配置文件: clientPort分别为 2181、2182、2183 dataDir分别为 dataDir=/home/hadoop/opt/app/zookeeper/data1 dataDir=/home/hadoop/opt/app/zookeeper/data2 dataDir=/home/hadoop/opt/app/zookeeper/data3 手动创建这三个目录,并添加写的权限。
代码语言:javascript复制hadoop@master:~/opt/app/zookeeper$ mkdir data1
hadoop@master:~/opt/app/zookeeper$ mkdir data2
hadoop@master:~/opt/app/zookeeper$ mkdir data3
hadoop@master:~/opt/app/zookeeper$ ls
bin conf data0 data1 data2 data3 docs lib LICENSE.txt logs NOTICE.txt README.md README_packaging.txt
最后这三个配置文件的末尾均添加以下信息: zoo1.cfg 配置文件
代码语言:javascript复制# dataDir=/tmp/zookeeper 注释掉这句默认配置,然后修改下面的配置
dataDir=/home/hadoop/opt/app/zookeeper/data1
# clientPort=2181 注释掉15行这句话,把这句内容放在文件尾部
clientPort=2181
# server.1=master:2888(数据同步端口):3888(Leader选举端口)
server.1=master:2888:3888
server.2=master:2889:3889
server.3=master:2890:3890
zoo2.cfg 配置文件
代码语言:javascript复制# dataDir=/tmp/zookeeper 注释掉这句默认配置,然后修改下面的配置
dataDir=/home/hadoop/opt/app/zookeeper/data2
# clientPort=2181 注释掉15行这句话,把这句内容放在文件尾部
clientPort=2182
# server.1=master:2888(数据同步端口):3888(Leader选举端口)
server.1=master:2888:3888
server.2=master:2889:3889
server.3=master:2890:3890
zoo3.cfg 配置文件
代码语言:javascript复制# dataDir=/tmp/zookeeper 注释掉这句默认配置,然后修改下面的配置
dataDir=/home/hadoop/opt/app/zookeeper/data3
# clientPort=2183 注释掉15行这句话,把这句内容放在文件尾部
clientPort=2183
# server.1=master:2888(数据同步端口):3888(Leader选举端口)
server.1=master:2888:3888
server.2=master:2889:3889
server.3=master:2890:3890
server.A=B:C:D中A是一个数字,表示这个是第几号服务器,B是这个服务器的IP地址,C表示这个服务器与集群中的Leader服务器交换信息的端口,D表示集群中的Leader服务器挂了,需要一个端口来重新进行选举,选出一个新的Leader,而这个端口就是用来执行选举时服务器相互通信的端口。对于伪分布集群的配置方式,由于B都是一样,需要为不同的Zookeeper服务通信端口分配不同的端口号。
在各个data目录下创建myid文件,并存入当前服务器的编号,如下:
代码语言:javascript复制hadoop@master:~/opt/app/zookeeper$ pwd
/home/hadoop/opt/app/zookeeper
hadoop@master:~/opt/app/zookeeper$ touch data1/myid
hadoop@master:~/opt/app/zookeeper$ touch data2/myid
hadoop@master:~/opt/app/zookeeper$ touch data3/myid
hadoop@master:~/opt/app/zookeeper$ echo 1 > data1/myid
hadoop@master:~/opt/app/zookeeper$ echo 2 > data2/myid
hadoop@master:~/opt/app/zookeeper$ echo 3 > data3/myid
- 启动并测试Zookeeper伪分布式集群
依次启动Zookeeper服务,在启动的时候,选举算法依次投票,那么Leader 服务就是2号配置文件启动的服务。其他的节点服务都是Fllower,当启动第一台服务器的时候查看状态status是不可用的,因为集群中节点未在半数以上。集群中奇数和偶数对故障的容忍度是一致的,所以建议配置奇数个Zookeeper节点。
如下依次使用配置文件启动Zookeeper服务:
代码语言:javascript复制hadoop@master:~/opt/app/zookeeper$ zkServer.sh start /home/hadoop/opt/app/zookeeper/conf/zoo1.cfg # 通过zoo1.cfg启动zkServer
ZooKeeper JMX enabled by default
Using config: /home/hadoop/opt/app/zookeeper/conf/zoo1.cfg
Starting zookeeper ... STARTED
hadoop@master:~/opt/app/zookeeper$ zkServer.sh status /home/hadoop/opt/app/zookeeper/conf/zoo1.cfg # 查看通过zoo1.cfg启动的zkServer状态
ZooKeeper JMX enabled by default
Using config: /home/hadoop/opt/app/zookeeper/conf/zoo1.cfg
Client port found: 2181. Client address: localhost.
Error contacting service. It is probably not running.
hadoop@master:~/opt/app/zookeeper$ zkServer.sh start /home/hadoop/opt/app/zookeeper/conf/zoo2.cfg # 通过zoo2.cfg启动zkServer
ZooKeeper JMX enabled by default
Using config: /home/hadoop/opt/app/zookeeper/conf/zoo2.cfg
Starting zookeeper ... STARTED
hadoop@master:~/opt/app/zookeeper$ zkServer.sh status /home/hadoop/opt/app/zookeeper/conf/zoo2.cfg # 查看通过zoo2.cfg启动的zkServer状态,为leader
ZooKeeper JMX enabled by default
Using config: /home/hadoop/opt/app/zookeeper/conf/zoo2.cfg
Client port found: 2182. Client address: localhost.
Mode: leader
hadoop@master:~/opt/app/zookeeper$ zkServer.sh start /home/hadoop/opt/app/zookeeper/conf/zoo3.cfg # 通过zoo3.cfg启动zkServer
ZooKeeper JMX enabled by default
Using config: /home/hadoop/opt/app/zookeeper/conf/zoo3.cfg
Starting zookeeper ... STARTED
hadoop@master:~/opt/app/zookeeper$ zkServer.sh status /home/hadoop/opt/app/zookeeper/conf/zoo3.cfg # 查看通过zoo3.cfg启动的zkServer状态
ZooKeeper JMX enabled by default
Using config: /home/hadoop/opt/app/zookeeper/conf/zoo3.cfg
Client port found: 2183. Client address: localhost.
Mode: follower
hadoop@master:~/opt/app/zookeeper$ zkServer.sh status /home/hadoop/opt/app/zookeeper/conf/zoo1.cfg # 查看通过zoo1.cfg启动的zkServer状态
ZooKeeper JMX enabled by default
Using config: /home/hadoop/opt/app/zookeeper/conf/zoo1.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
如下图:
此时使用命令jps查看所有进程:
代码语言:javascript复制hadoop@master:~/opt/app/zookeeper$ jps
4833 QuorumPeerMain
5060 Jps
4726 QuorumPeerMain
4607 QuorumPeerMain
客户端连接Zookeeper服务节点,则使用如下命令:
代码语言:javascript复制hadoop@master:~/opt/app/zookeeper$ zkCli.sh -server localhost:2181
Connecting to localhost:2181
#退出使用quit
配置全分布式的Zookeeper
对于Zookeeper完全分布式集群,配置文件则是在各自的服务器上,然后在各自的服务器上开启Zookeeper服务进程即可。