ZooKeeper集群部署
1. 基础环境
三台虚拟机(centos7,个人建议可以先做一台然后克隆)
1.1 主机规划
IP | 主机名 | 节点 |
---|---|---|
192.168.237.141 | zookeeper1 | 集群节点 |
192.168.237.142 | zookeeper2 | 集群节点 |
192.168.237.143 | zookeeper3 | 集群节点 |
2.基础安装配置
2.1 修改主机名
1. zookeeper1
[root@localhost ~]# hostnamectl set-hostname zookeeper1
[root@localhost ~]# bash
[root@zookeeper1 ~]#
2. zookeeper2
[root@localhost ~]# hostnamectl set-hostname zookeeper2
[root@localhost ~]# bash
[root@zookeeper2 ~]#
3.zookeeper3
[root@localhost ~]# hostnamectl set-hostname zookeeper3
[root@localhost ~]# bash
[root@zookeeper3 ~]#
2.2 修改hosts 配置文件
1. zookeeper1:
[root@zookeeper1 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.237.141 zookeeper1
192.168.20.142 zookeeper2
192.168.20.143 zookeeper3
2. zookeeper2:
[root@zookeeper2 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.237.141 zookeeper1
192.168.20.142 zookeeper2
192.168.20.143 zookeeper3
3.zookeeper3:
[root@zookeeper3 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.237.141 zookeeper1
192.168.20.142 zookeeper2
192.168.20.143 zookeeper3
2.3 安装JDK环境与配置
1.安装JDK
[root@zookeeper1 ~]# yum -y list java* | grep java-1.8.0-openjdk-devel
java-1.8.0-openjdk-devel.i686 1:1.8.0.342.b07-1.el7_9 updates
java-1.8.0-openjdk-devel.x86_64 1:1.8.0.342.b07-1.el7_9 updates
[root@zookeeper1 ~]# yum -y install java-1.8.0-openjdk-devel
[root@zookeeper1 ~]# java -version
openjdk version "1.8.0_342"
OpenJDK Runtime Environment (build 1.8.0_342-b07)
OpenJDK 64-Bit Server VM (build 25.342-b07, mixed mode)
以上操作后三台机全要装
2.4 下载并解压zookeeper软件包
1. 下载
[root@zookeeper1]# wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
--2022-09-14 09:36:34-- https://archive.apache.org/dist/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
正在解析主机 archive.apache.org (archive.apache.org)... 138.201.131.134, 2a01:4f8:172:2ec5::2
正在连接 archive.apache.org (archive.apache.org)|138.201.131.134|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:22724574 (22M) [application/x-gzip]
正在保存至: “zookeeper-3.4.9.tar.gz”
100%[=============================================================================================================>] 22,724,574 3.81MB/s 用时 6.9s
2022-09-14 09:36:42 (3.15 MB/s) - 已保存 “zookeeper-3.4.9.tar.gz” [22724574/22724574])
[root@zookeeper1 src]# tar -zxvf zookeeper-3.4.9.tar.gz
[root@zookeeper1]# cd zookeeper-3.4.9/
[root@zookeeper1 zookeeper-3.4.9]#
以上操作三台机全要
3. Zookeeper配置
3.1 配置zookeeper节点
1. 配置与修改 zoo_sample.cfg 文件
1.1
[root@zookeeper1 zookeeper-3.4.9]# cd conf/
[root@zookeeper1 conf]# ls
configuration.xsl log4j.properties zoo_sample.cfg
[root@zookeeper1 conf]# cp zoo_sample.cfg zoo.cfg
[root@zookeeper1 conf]# vim zoo.cfg
tickTime=2000 # Zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔
initLimit=10 # 允许follower连接并同步到Leader的初始化连接时间,以tickTime为单位。
syncLimit=5 # 配置follower和leader之间发送消息,请求和应答的最大时间长度。
dataDir=/tmp/zookeeper # 数据目录
dataLogDir=/tmp/zookeeper/log # 日志目录
clientPort=2181 # 客户端端口
server.0=192.168.237.141:2888:3888 # server.id=ip:服务器之间的通信端口:Leader选举的端口
server.1=192.168.237.142:2888:3888
server.2=192.168.237.143:2888:3888
[root@zookeeper1 ~]# mkdir /tmp/zookeeper
[root@zookeeper1 ~]# vi /tmp/zookeeper/myid
[root@zookeeper1 ~]# cat /tmp/zookeeper/myid
1
1.1 zookeeper2
[root@zookeeper2 zookeeper-3.4.9]# cd conf/
[root@zookeeper2 conf]# ls
configuration.xsl log4j.properties zoo_sample.cfg
[root@zookeeper2 conf]# cp zoo_sample.cfg zoo.cfg
[root@zookeeper2 conf]# vim zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
dataLogDir=/tmp/zookeeper/log
clientPort=2181
server.0=192.168.237.141:2888:3888
server.1=192.168.237.142:2888:3888
server.2=192.168.237.143:2888:3888
[root@zookeeper2 ~]# mkdir /tmp/zookeeper
[root@zookeeper2 ~]# vi /tmp/zookeeper/myid
[root@zookeeper2 ~]# cat /tmp/zookeeper/myid
2
1.3 zookeeper3
[root@zookeeper3 zookeeper-3.4.9]# cd conf/
[root@zookeeper3 conf]# ls
configuration.xsl log4j.properties zoo_sample.cfg
[root@zookeeper3 conf]# cp zoo_sample.cfg zoo.cfg
[root@zookeeper2 conf]# vim zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
dataLogDir=/tmp/zookeeper/log
clientPort=2181
server.0=192.168.237.141:2888:3888
server.1=192.168.237.142:2888:3888
server.2=192.168.237.143:2888:3888
[root@zookeeper3 ~]# mkdir /tmp/zookeeper
[root@zookeeper3 ~]# vi /tmp/zookeeper/myid
[root@zookeeper3 ~]# cat /tmp/zookeeper/myid
3
3.2 启动并验证Zookeeper
1.zookeeper1:
[root@zookeeper1 conf]# cd /usr/local/zookeeper-3.4.9/bin/
[root@zookeeper1 bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@zookeeper1 bin]#
2.zookeeper2:
[root@zookeeper2 conf]# cd /usr/local/zookeeper-3.4.9/bin/
[root@zookeeper2 bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@zookeeper2 bin]#
3.zookeeper3:
[root@zookeeper3 conf]# cd /usr/local/zookeeper-3.4.9/bin/
[root@zookeeper3 bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@zookeeper3 bin]#
全部开启后查看节点端口是否开启3888端口
查看命令为 netstat -tunpl(如果没有此命令就安装 net-tools。 yum -y install net-tools )
3.4 验证
1.zookeeper1:
[root@zookeeper1 ]# /root/zookeeper-3.4.9/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: follower
[root@zookeeper1 ]#
2.zookeeper2:
[root@zookeeper2 ]# /root/zookeeper-3.4.9/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: follower
[root@zookeeper2 ]#
## 3.zookeeper3:
```c
[root@zookeeper3 ]# /root/zookeeper-3.4.9/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: leader
[root@zookeeper3 ]#