欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

zookeeper伪集群搭建及observer模式配置笔记

程序员文章站 2022-06-21 17:50:21
...

环境说明

本次伪集群搭建zookeeper版本是3.4.14、服务器用的是centos7。

环境准备

下载zookeeper按照包

地址: zookeeper-3.4.14.tar.gz

[aaa@qq.com home]# wget https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
--2020-08-23 18:31:28--  https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
正在解析主机 www.apache.org (www.apache.org)... 95.216.24.32, 40.79.78.1, 2a01:4f9:2a:185f::2
正在连接 www.apache.org (www.apache.org)|95.216.24.32|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:未指定 [text/html]
正在保存至: “zookeeper-3.4.14.tar.gz”

    [  <=>                                                                                   ] 30,070      87.0KB/s 用时 0.3s   

2020-08-23 18:31:30 (87.0 KB/s) - “zookeeper-3.4.14.tar.gz” 已保存 [30070]

[aaa@qq.com home]# ll
-rw-r--r--. 1 root root 30070 8月  23 18:31 zookeeper-3.4.14.tar.gz
创建一个放置zookeeper的目录 
[aaa@qq.com home]# mkdir /home/zookeeper-group
将zookeeper解压并改名称复制3分放到改目录下

zookeeper伪集群搭建及observer模式配置笔记

安装配置

创建zookeeper数据存放目录

分别在 zookeeper-2181、zookeeper-2182、zookeeper-2183目录下创建data目录用来存zookeeper的数据。
zookeeper伪集群搭建及observer模式配置笔记

注意:三个zookeeper目录下都要创建

创建myid文件

在我们刚才创建的data目录里边分别创建myid文件 注意是文件不是目录。然后分别输入1、2、3。

zookeeper-2181 对应的myid 输入1
zookeeper-2182 对应的myid 输入2
zookeeper-2183 对应的myid 输入3

检查是否正确zookeeper伪集群搭建及observer模式配置笔记

修改配置文件

zookeeper的配置文件目录在 conf下边。
分别将 zoo_sample.cfg 拷贝一份改名 zoo.cfg

然后分别修改配置文件:

zookeeper-2181 配置文件如下:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
# 这里是我们保存zookeeper数据的路径
dataDir=/home/zookeeper-group/zookeeper-2181/data/
# the port at which the clients will connect
#这个是我们的端口号
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
# 这里是我们配置的对用的三台机器
#参数说明
# server.1 是我们当前节点在集群的序号
# 192.168.56.101 是我们zookeeper节点的ip地址
#2161:2171 后边两个端口号 前边是一个节点直接同步数据用户的,后边那个是用来选举master用的。
server.1=192.168.56.101:2161:2171
server.2=192.168.56.101:2162:2172
server.3=192.168.56.101:2163:2173

zookeeper-2182 配置文件如下:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
# 这里是我们保存zookeeper数据的路径
dataDir=/home/zookeeper-group/zookeeper-2182/data/
# the port at which the clients will connect
#这个是我们的端口号
clientPort=2182
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
# 这里是我们配置的对用的三台机器
#参数说明
# server.1 是我们当前节点在集群的序号
# 192.168.56.101 是我们zookeeper节点的ip地址
#2161:2171 后边两个端口号 前边是一个节点直接同步数据用户的,后边那个是用来选举master用的。
server.1=192.168.56.101:2161:2171
server.2=192.168.56.101:2162:2172
server.3=192.168.56.101:2163:2173

zookeeper-2183配置文件如下:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
# 这里是我们保存zookeeper数据的路径
dataDir=/home/zookeeper-group/zookeeper-2183/data/
# the port at which the clients will connect
#这个是我们的端口号
clientPort=2183
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
# 这里是我们配置的对用的三台机器
#参数说明
# server.1 是我们当前节点在集群的序号
# 192.168.56.101 是我们zookeeper节点的ip地址
#2161:2171 后边两个端口号 前边是一个节点直接同步数据用户的,后边那个是用来选举master用的。
server.1=192.168.56.101:2161:2171
server.2=192.168.56.101:2162:2172
server.3=192.168.56.101:2163:2173

环境测试

分别去启动我们的三个节点

[aaa@qq.com conf]# sh /home/zookeeper-group/zookeeper-2181/bin/zkServer.sh start

[aaa@qq.com conf]# sh /home/zookeeper-group/zookeeper-2182/bin/zkServer.sh start

[aaa@qq.com conf]# sh /home/zookeeper-group/zookeeper-2183/bin/zkServer.sh start

查看三个节点状态:

zookeeper伪集群搭建及observer模式配置笔记
注意:红框如果是 Mode: standalone表示你的集群有问题,每个节点还是单独运行的,需要排查问题。

一、防火墙
二、配置文件哪里写错了。

节点创建测试

在其中一个节点创建一个节点,在其他两个节点查看是否有数据。

zookeeper伪集群搭建及observer模式配置笔记
zookeeper伪集群搭建及observer模式配置笔记
zookeeper伪集群搭建及observer模式配置笔记
发现数据可以同步,创建成功!!

observer模式

该模式运行的zookeeper
1、不参与选举,
2、不参与数据事务提交的ack应答。

注意:观察者机器数据也是跟集群同步的

配置很简单:
这个zookeeper节点的配置文件需要加入

peerType=observer

通知在集群的所有配置文件中对这个节点后边加:observer

zookeeper-2184-observer节点配置文件

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/home/zookeeper-group/zookeeper-2184-observer/data/
# the port at which the clients will connect
clientPort=2184
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
# observer 机器需要配置
peerType=observer
server.1=192.168.56.101:2161:2171
server.2=192.168.56.101:2162:2172
server.3=192.168.56.101:2163:2173
#所有机器都需要配置这个
server.4=192.168.56.101:2164:2174:observer