linux zookeeper安装部署

安装包:apache-zookeeper-3.5.5-bin.tar.gz,下载路径 https://archive.apache.org/dist/zookeeper/

环境要求:zookeeper 3.5.5要求JDK版本不低于1.8,centos7和rhel7的jdk默认就是1.8版本

 

安装过程(在单机上安装3个实例,实例的目录和端口不同):

①上传及解压安装包

[root@centos7-2 ~]# mkdir -p /cslc/zookeeper
[root@centos7-2 ~]# cd /cslc/zookeeper/  #通过SFTP将安装包上传到这个目录
[root@centos7-2 zookeeper]# tar zxf apache-zookeeper-3.5.5-bin.tar.gz 
[root@centos7-2 zookeeper]# cp -r apache-zookeeper-3.5.5-bin zookeeper1  
[root@centos7-2 zookeeper]# cp -r apache-zookeeper-3.5.5-bin zookeeper2 
[root@centos7-2 zookeeper]# cp -r apache-zookeeper-3.5.5-bin zookeeper3  #三个实例
[root@centos7-2 zookeeper]# ll zookeeper1/
总用量 32
drwxr-xr-x. 2 root root 232 2月 23 21:36 bin
drwxr-xr-x. 2 root root 77 2月 23 21:36 conf
drwxr-xr-x. 5 root root 4096 2月 23 21:36 docs
drwxr-xr-x. 2 root root 4096 2月 23 21:36 lib
-rw-r--r--. 1 root root 11358 2月 23 21:36 LICENSE.txt
-rw-r--r--. 1 root root 432 2月 23 21:36 NOTICE.txt
-rw-r--r--. 1 root root 1560 2月 23 21:36 README.md
-rw-r--r--. 1 root root 1347 2月 23 21:36 README_packaging.txt

 

②创建配置文件

[root@centos7-2 zookeeper]# mkdir {zookeeper1,zookeeper2,zookeeper3}/data   #创建数据文件目录
[root@centos7-2 zookeeper]# cd zookeeper1/conf/
[root@centos7-2 conf]# cp zoo_sample.cfg zoo.cfg   #复制生成配置文件

修改zoo.cfg,修改部分如下,然后分别修改zookeeper2和zookeeper3的配置文件,三个配置文件只有dataDir和clientPort不同。

# 存放数据文件
dataDir=/cslc/zookeeper/zookeeper1/data  #其他两个为zookeeper2/data和zookeeper3/data
clientPort=2181   #服务端口,其他两个为2182和2183
# zookeeper cluster配置,2888为心跳端口,即leader和follower的通信端口;3888为重新选举端口。如果是部署在三台机器上,则server的ip不同,心跳和选举端口相同。
server.1=192.168.85.20:2887:3887
server.2=192.168.85.20:2888:3888
server.3=192.168.85.20:2889:3889

 

③创建myid文件

myid文件的内容为一个数字,用来标识当前主机,conf/zoo.cfg文件中配置的server.X中X是什么数字,myid文件中就输入这个数字。

[root@centos7-2 conf]# echo "1" > /cslc/zookeeper/zookeeper1/data/myid
[root@centos7-2 conf]# echo "2" > /cslc/zookeeper/zookeeper2/data/myid
[root@centos7-2 conf]# echo "3" > /cslc/zookeeper/zookeeper3/data/myid

 

④启动和关闭和查看状态

启动命令在bin目录下,./zkServer.sh start、stop、status可分别启动、停止和查看状态。

要同时启动三个实例的应用,启动后会开始选取leader和follower,耗时约30s~120s,leader选取期间集群服务不可用。

启动完成后通过status查看(由于leader选取耗时刚启动状态可能为not running),只有一个实例的Mode为leader即主,其他实例Mode为follower,即从。

[root@centos7-2 conf]# cd ../bin
[root@centos7-2 bin]# ls
README.txt    zkCli.cmd  zkEnv.cmd  zkServer.cmd            zkServer.sh          zkTxnLogToolkit.sh
zkCleanup.sh  zkCli.sh   zkEnv.sh   zkServer-initialize.sh  zkTxnLogToolkit.cmd
[root@centos7-2 bin]# ./zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /cslc/zookeeper/zookeeper1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@centos7-2 bin]# ./zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /cslc/zookeeper/zookeeper1/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader

 

通过客户端访问

[root@centos7-2 bin]# ./zkCli.sh -server localhost:2181
/usr/bin/java
Connecting to localhost:2181
2020-02-23 22:49:53,274 [myid:] - INFO [main:Environment@109] - Client environment:zookeeper.version=3.5.5-390fe37ea45dee……
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]

使用zkCli.sh通过客户端访问时,不加参数默认连接到本地localhost提供的服务;如果想要连接到指定的服务器,可通过-server server:port指定。

退出客户端使用quit命令 

 

需要注意的是myid和zoo.cfg配置文件,行尾不要有多余的空格,myid中也不要有多余的空行,否则启动的时候都会报错误。

例如我的zoo.cfg,server.1和server.2后面有一个空格,启动的时候报无效的配置异常退出。

[root@localhost logs]# more zookeeper-root-server-localhost.out 
2021-12-24 14:56:35,941 [myid:] - INFO  [main:QuorumPeerConfig@135] - Reading configuration from: /usr/local/zookeep
er1-3.5.7/bin/../conf/zoo.cfg
2021-12-24 14:56:35,979 [myid:] - INFO  [main:QuorumPeerConfig@387] - clientPortAddress is 0.0.0.0:2181
2021-12-24 14:56:35,979 [myid:] - INFO  [main:QuorumPeerConfig@391] - secureClientPort is not set
2021-12-24 14:56:35,992 [myid:] - ERROR [main:QuorumPeerMain@89] - Invalid config, exiting abnormally
org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Address unresolved: 192.168.229.5:3887 
        at org.apache.zookeeper.server.quorum.QuorumPeer$QuorumServer.<init>(QuorumPeer.java:261)
        at org.apache.zookeeper.server.quorum.flexible.QuorumMaj.<init>(QuorumMaj.java:89)

 

posted @ 2019-09-08 09:54  Forever77  阅读(234)  评论(0编辑  收藏  举报