zookeeper搭建集群(单机伪集群)

zookeeper搭建单机伪集群

搭建步骤

  • 所需安装包

本次演示的zookeeper版本是:zookeeper-3.4.14。

  • 步骤1、在放置zookeeper的目录下,如“/usr/app/zookeeper”目录,创建zookeeper-colony,然后先创建子目录zk1,然后在zk1中创建数据存放目录data和日志存放目录logs。
## 创建zk集群目录后进入在创建节点zk1目录,之后在创建data和logs目录
mkdir zookeeper-colony
cd zookeeper-colony/
mkdir zk1
cd zk1
mkdir data
mkdir logs
  • 步骤2、复制单机节点中zookeeper的conf目录中的zoo.cfg文件到zk1下并重命名为zoo1.cfg,然后添加zk1节点的配置信息。
## 拷贝单机机安装目录下zoo.cfg配置文件,并重命令(当然也可不必这么做),然后编辑修改。
cp /usr/app/zookeeper/zookeeper-3.4.14/conf/zoo.cfg /usr/app/zookeeper/zookeeper-colony/zk1 –a
mv zoo.cfg zoo1.cfg
vim zoo1.cfg

配置内容如示。

zoo1.cfg

# 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=/usr/app/zookeeper/zookeeper-colony/zk1/data
dataLogDir=/usr/app/zookeeper/zookeeper-colony/zk1/logs

# 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=localhost:2889:3889
server.2=localhost:2890:3890
server.3=localhost:2891:3891

zookeeper 的三个端口作用
1)2181:对 client 端提供服务
2)2888:集群内机器通信使用
3)3888:选举 leader 使用

  • 步骤3、在zk1中的data目录下新建文件myid,添加内容1。
## 创建myid文件,可以直接使用 vim data/myid此命令会检查是否存在这个文件,没有时新建
touch data/myid
vim data/myid
## zk1中的内容为1,zk2中的内容为2,zk3中的内容为3,和zoo1.cfg中的server.*中的 “*”对应
  • 步骤4、复制两份zk1,命名为zk2、zk3、然后修改zoo.cfg文件中的端口、数据位置、日志位置,以及myid中序号,分别为2、3。

zoo2.cfg

# 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=/usr/app/zookeeper/zookeeper-colony/zk2/data
dataLogDir=/usr/app/zookeeper/zookeeper-colony/zk2/logs

# 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=localhost:2889:3889
server.2=localhost:2890:3890
server.3=localhost:2891:3891

zoo3.cfg

# 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=/usr/app/zookeeper/zookeeper-colony/zk3/data
dataLogDir=/usr/app/zookeeper/zookeeper-colony/zk3/logs

# 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

## 配置集群通信节点
server.1=localhost:2889:3889
server.2=localhost:2890:3890
server.3=localhost:2891:3891
  • 步骤5、在系统环境中配置zookeeper的路径,方便命令启动输入(当然也可不做此步)。在/etc/profile文件中添加
# 编辑文件
vim /etc/profile

# 添加zk安装地址
# zookeeper安装目录
export ZOOKEEPER_HOME=/usr/app/zookeeper/zookeeper-3.4.14
PATH=${ZOOKEEPER_HOME}/bin:$PATH
export PATH

# 重启环境变量
source /etc/profile
  • 步骤6、分别启动三个zookeeper
# 启动3个zookeeper服务
zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg

# 查看每个zookeeper对应的角色
zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg

# 停止zookeeper服务
zkServer.sh stop /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh stop /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh stop /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg

启动后查看zk状态,控制台输入如下。

[root@localhost ~]# zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg 
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
Starting zookeeper ... STARTED
[root@localhost ~]# zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg 
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
Starting zookeeper ... STARTED
[root@localhost ~]# zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg 
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg
Starting zookeeper ... STARTED
[root@localhost ~]# zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg 
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
Mode: follower
[root@localhost ~]# zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg 
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
Mode: leader
[root@localhost ~]# zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg 
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg
Mode: follower
  • 步骤7、ZooKeeper命令行验证使用。(可选)
# 连接服务器 zkCli -server IP:PORT
zkCli.sh -server 127.0.0.1:2182
# 帮助命令
help
# 获取根节点列表,默认会有一个zookeeper节点,节点列表就像linux目录一样,根节点为/
ls /
# 创建znode并关联值, create /节点名称 “值”
create /myZnode "my znode"
# 获取znode
get /myZnode
# 修改znode
set /myZnode "my znode value string"
# 删除单个znode
delete /myZnode
# 递归删除(删除myZnode的子节点和myZnode节点)
rmr /myZnode
# 退出登录
quit
  • 步骤8、外部访问端口开放。
# 开放配置zk端口2182、2183、2184
firewall-cmd --add-port=2182/tcp --permanent
firewall-cmd --add-port=2183/tcp --permanent
firewall-cmd --add-port=2184/tcp --permanent

# 重启服务
firewall-cmd --reload

# 查看开放端口
firewall-cmd --list-port
  • 步骤10、在zookeeper-colony目录下编写快速启动、重启、停止运行脚本,并且为这些文件赋予可执行权限。(文件名后缀为.sh)
# 编写启动脚本
vim zk-all-start.sh
## 内容如下。
zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg

# 编写重启脚本
vim zk-all-restart.sh
## 内容如下。
zkServer.sh restart /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh restart /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh restart /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg

# 编写停止脚本
vim zk-all-stop.sh
## 内容如下。
zkServer.sh stop /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh stop /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh stop /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg

# 编写查看脚本
vim zk-all-status.sh
## 内容如下。
zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg

为编写文件赋权可行命令:chmod 755 zk-all-*,而后测试验证即可。

以上就安装单机伪集群结束。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值