centos7 安装zookeeper 3.4.14单机集群

写在前面:

  1. zookeeper依赖java环境,请先安装jdk:centos7 安装jdk1.8环境
  2. zookeeper单机集群规划:3个节点,端口号分别是2181、2812、2813
  3. zookeeper单机集群关键在于隔离各节点的端口、数据,与多机集群无太大差别。
zookeeper集群.jpg
1. 下载并解压安装包

下载安装包到任意文件夹,本文下载文件夹是:/home/soft

# 没有wget指令,可以执行yum install -y wget安装
wget http://apache.website-solution.net/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz

解压至安装文件夹,本文文件夹是:/home/app

tar -zxvf zookeeper-3.4.14.tar.gz -C /home/app/
2. 安装目录结构

本案zookeeper安装根文件夹是:/home/app/zookeeper
3个节点的子文件夹是:2181,2182,2183
各个节点的数据文件夹data,数据日志文件夹datalogs

# 创建文件夹结构
mkdir -p zookeeper/218{1,2,3}/{data,datalogs}

# 复制zookeeper程序文件到各个节点
cp -r zookeeper-3.4.14 zookeeper/2181
cp -r zookeeper-3.4.14 zookeeper/2182
cp -r zookeeper-3.4.14 zookeeper/2183

# 原安装文件可以删除
rm -rf zookeeper-3.4.14/

最终目录结构:

# 没有tree指令,可以执行yum install -y tree安装
[root@localhost app]# tree -L 3
.
└── zookeeper
    ├── 2181
    │   ├── data
    │   ├── datalogs
    │   └── zookeeper-3.4.14
    ├── 2182
    │   ├── data
    │   ├── datalogs
    │   └── zookeeper-3.4.14
    └── 2183
        ├── data
        ├── datalogs
        └── zookeeper-3.4.14
3. zoo.cfg配置

zoo.cfg是zookeeper的配置文件,放在各个节点的zookeeper-3.4.14/conf/文件夹下。可以复制zoo_sample.cfg配置模板。

  • 节点1:
# 节点1
[root@localhost app]# cd zookeeper/2181/zookeeper-3.4.14/conf/
[root@localhost conf]# cp zoo_sample.cfg zoo.cfg 
[root@localhost conf]# vim zoo.cfg
clientPort=2181
dataDir=/home/app/zookeeper/2181/data
dataLogDir=/home/app/zookeeper/2181/datalogs
server.1=10.30.2.222:2881:3881
server.2=10.30.2.222:2882:3882
server.3=10.30.2.222:2883:3883

同理,节点2和节点3也这样配置。注意修改clientPort、dataDir、dataLogDir

# 节点2
clientPort=2182
dataDir=/home/app/zookeeper/2182/data
dataLogDir=/home/app/zookeeper/2182/datalogs
server.1=10.30.2.222:2881:3881
server.2=10.30.2.222:2882:3882
server.3=10.30.2.222:2883:3883

# 节点3
clientPort=2183
dataDir=/home/app/zookeeper/2183/data
dataLogDir=/home/app/zookeeper/2183/datalogs
server.1=10.30.2.222:2881:3881
server.2=10.30.2.222:2882:3882
server.3=10.30.2.222:2883:3883

clientPort:客户端连接端口
dataDir:数据存放目录
dataLogDir:数据日志存放目录,非运行日志
server.x:集群中的节点(包括自己),x对应myid的数字,2个端口分别用于集群选举通讯

4. myid配置

每个节点必须有myid配置文件,记录节点的唯一标识,必须放在dataDir文件夹下
id值必须与上面配置的server.x中的x对应

# 创建myid文件,并写入id值
touch 2181/data/myid && echo "1" > 2181/data/myid
touch 2182/data/myid && echo "2" > 2182/data/myid
touch 2183/data/myid && echo "3" > 2183/data/myid

查看myid文件内容,确保内容无误

[root@localhost zookeeper]# more 218*/data/myid
::::::::::::::
2181/data/myid
::::::::::::::
1
::::::::::::::
2182/data/myid
::::::::::::::
2
::::::::::::::
2183/data/myid
::::::::::::::
3
5. 配置防火墙
# 添加端口2181-2183;2881-2883;3881-3883
firewall-cmd --zone=public --add-port=2181/tcp --permanent
firewall-cmd --zone=public --add-port=2182/tcp --permanent
firewall-cmd --zone=public --add-port=2183/tcp --permanent

firewall-cmd --zone=public --add-port=2881/tcp --permanent
firewall-cmd --zone=public --add-port=2882/tcp --permanent
firewall-cmd --zone=public --add-port=2883/tcp --permanent

firewall-cmd --zone=public --add-port=3881/tcp --permanent
firewall-cmd --zone=public --add-port=3882/tcp --permanent
firewall-cmd --zone=public --add-port=3883/tcp --permanent

# 重新载入防火墙配置
firewall-cmd --reload
6. 启动集群

逐个节点启动

cd 2181/ && zookeeper-3.4.14/bin/zkServer.sh start
cd ../2182/ && zookeeper-3.4.14/bin/zkServer.sh start
cd ../2183/ && zookeeper-3.4.14/bin/zkServer.sh start

进入各个节点启动zookeeper,是为了让运行日志zookeeper.out生成在各个节点目录下。

7. 查看集群状态
# 节点1
[root@localhost zookeeper]# 2181/zookeeper-3.4.14/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/app/zookeeper/2181/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: follower

# 节点2
[root@localhost zookeeper]# 2182/zookeeper-3.4.14/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/app/zookeeper/2182/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: leader

# 节点3
[root@localhost zookeeper]# 2183/zookeeper-3.4.14/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/app/zookeeper/2183/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: follower

leader表示主节点,follower表示从节点。目前状态是1主2从,状态正常!

8. 客户端连接
[root@localhost zookeeper]# 2181/zookeeper-3.4.14/bin/zkCli.sh 
Connecting to localhost:2181
2019-05-05 08:42:38,307 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.14-4c25d480e66aadd371de8bd2fd8da255ac140bcf, built on 03/06/2019 16:18 GMT
2019-05-05 08:42:38,309 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=localhost
...
...
...
2019-05-05 08:42:38,460 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@879] - Socket connection established to localhost/127.0.0.1:2181, initiating session
2019-05-05 08:42:38,517 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x10000a217f10000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]

连接正常,zookeeper单机集群搭建大功告成!

你可能感兴趣的:(centos7 安装zookeeper 3.4.14单机集群)