当前位置:   article > 正文

如何在Linux系统中搭建Zookeeper集群

如何在Linux系统中搭建Zookeeper集群

一、概述

ZooKeeper是一个开源的且支持分布式部署的应用程序,是Google的Chubby一个开源的实现;它为分布式应用提供了一致性服务支持,包括:配置维护、域名服务、分布式同步、组服务等。

官网:https://zookeeper.apache.org/index.html

二、集群搭建

1、目标

分别在192.168.1.100、192.168.1.101和192.168.1.102三台虚拟机安装Zookeeper,搭建Zookeeper集群

2、准备

准备三台IP地址为192.168.1.100、192.168.1.101和192.168.1.102的虚拟机,视频教程:https://www.bilibili.com/video/BV15m4y1d7ZP

3、步骤

a、下载:https://zookeeper.apache.org/releases.html,这里下载apache-zookeeper-3.8.4-bin.tar.gz

b、连接:通过MobaXterm 远程连接192.168.1.100虚拟机;

c、上传:将apache-zookeeper-3.8.4-bin.tar.gz压缩文件上传至/opt目录;

d、解压:将Zookeeper压缩文件解压至/usr/local目录

[root@localhost ~]# tar -zxvf /opt/apache-zookeeper-3.8.4-bin.tar.gz -C /usr/local
  • 1

e、删除:删除压缩文件

[root@localhost ~]# rm -f /opt/apache-zookeeper-3.8.4-bin.tar.gz
  • 1

f、创建目录:

/usr/local/apache-zookeeper-3.8.4-bin根目录下创建data和log文件夹

[root@localhost ~]# mkdir /usr/local/apache-zookeeper-3.8.4-bin/{datas,logs}
  • 1

g、修改配置:

①、重命名:

[root@localhost ~]# mv /usr/local/apache-zookeeper-3.8.4-bin/conf/zoo_sample.cfg /usr/local/apache-zookeeper-3.8.4-bin/conf/zoo.cfg
  • 1

②、改配置:

[root@localhost ~]# tee /usr/local/apache-zookeeper-3.8.4-bin/conf/zoo.cfg <<-'EOF'
# 设置服务器之间,或客户端与服务器之间心跳时间间隔,单位:毫秒
tickTime=2000
# Leader节点允许从节点连接和同步的初始化时间段,以tickTime倍数表示
initLimit=10
# Leader节点和从节点之间同步通信的时间段,以tickTime倍数表示
syncLimit=5
# 设置存储内存快照目录
dataDir=/usr/local/apache-zookeeper-3.8.4-bin/datas
# 设置Zookeeper日志目录
dataLogDir=/usr/local/apache-zookeeper-3.8.4-bin/logs
# 设置用于客户端连接Zookeeper端口
clientPort=2181

# server.X:用于配置集群中的每个服务器节点,X 是一个唯一的数字标识,等号右边是该节点的IP地址和两个端口,这些端口分别是用于节点间进行Leader选举的端口(Leader Election)和用于节点间进行数据同步的端口(Leader Sync)。
server.100=192.168.1.100:2888:3888
server.101=192.168.1.101:2888:3888
server.102=192.168.1.102:2888:3888

EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

h、节点拷贝:

[root@localhost ~]# scp -r /usr/local/apache-zookeeper-3.8.4-bin root@192.168.1.101:/usr/local/apache-zookeeper-3.8.4-bin
[root@localhost ~]# scp -r /usr/local/apache-zookeeper-3.8.4-bin root@192.168.1.102:/usr/local/apache-zookeeper-3.8.4-bin
  • 1
  • 2

注意:节点拷贝操作只在192.168.1.100节点执行。

i、创建myid:

每个 Zookeeper 节点 dataDir 指定目录中都需要创建一个名为 myid 的文件,其内容为上述配置文件中 server.X 的X值,例如X是100,那么/usr/local/apache-zookeeper-3.8.4-bin/data/myid文件的值为100,为快速设置内容,建议直接执行如下指令:

192.168.1.100节点:

[root@localhost ~]# tee /usr/local/apache-zookeeper-3.8.4-bin/datas/myid <<-'EOF'
100
EOF
  • 1
  • 2
  • 3

192.168.1.101节点:

[root@localhost ~]# tee /usr/local/apache-zookeeper-3.8.4-bin/datas/myid <<-'EOF'
101
EOF
  • 1
  • 2
  • 3

192.168.1.102节点:

[root@localhost ~]# tee /usr/local/apache-zookeeper-3.8.4-bin/datas/myid <<-'EOF'
102
EOF
  • 1
  • 2
  • 3

j、开放端口:

[root@localhost ~]# firewall-cmd --zone=public --add-port=2181/tcp --add-port=2888/tcp --add-port=3888/tcp --permanent
[root@localhost ~]# firewall-cmd --reload
  • 1
  • 2

注意:每个Zookeeper所在节点都需要通过执行上面命令开放端口。

三、启动节点

1、逐个操作
[root@localhost ~]# /usr/local/apache-zookeeper-3.8.4-bin/bin/zkServer.sh start
[root@localhost ~]# /usr/local/apache-zookeeper-3.8.4-bin/bin/zkServer.sh status
[root@localhost ~]# /usr/local/apache-zookeeper-3.8.4-bin/bin/zkServer.sh stop
  • 1
  • 2
  • 3

注意:每个Zookeeper所在节点都需要通过执行上面命令启动节点。

2、一键操作

通过MobaXterm 远程连接192.168.1.100虚拟机,进行如下操作:

a、zkStart.sh

[root@localhost ~]# tee /usr/local/apache-zookeeper-3.8.4-bin/bin/zkStart.sh <<-'EOF'
#!/bin/bash
echo "zookeeper start 100,101,102..."

ssh 192.168.1.100 "source /usr/local/apache-zookeeper-3.8.4-bin/bin/zkServer.sh start"
ssh 192.168.1.101 "source /usr/local/apache-zookeeper-3.8.4-bin/bin/zkServer.sh start"
ssh 192.168.1.102 "source /usr/local/apache-zookeeper-3.8.4-bin/bin/zkServer.sh start"
EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

b、zkStop.sh

[root@localhost ~]# tee /usr/local/apache-zookeeper-3.8.4-bin/bin/zkStop.sh <<-'EOF'
#!/bin/bash
echo "zookeeper stop 100,101,102..."

ssh 192.168.1.100 "source /usr/local/apache-zookeeper-3.8.4-bin/bin/zkServer.sh stop"
ssh 192.168.1.101 "source /usr/local/apache-zookeeper-3.8.4-bin/bin/zkServer.sh stop"
ssh 192.168.1.102 "source /usr/local/apache-zookeeper-3.8.4-bin/bin/zkServer.sh stop"
EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

c、zkStatus.sh

[root@localhost ~]# tee /usr/local/apache-zookeeper-3.8.4-bin/bin/zkStatus.sh <<-'EOF'
#!/bin/bash
echo "zookeeper status 100,101,102..."

ssh 192.168.1.100 "source /usr/local/apache-zookeeper-3.8.4-bin/bin/zkServer.sh status"
ssh 192.168.1.101 "source /usr/local/apache-zookeeper-3.8.4-bin/bin/zkServer.sh status"
ssh 192.168.1.102 "source /usr/local/apache-zookeeper-3.8.4-bin/bin/zkServer.sh status"
EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

d、设置权限

[root@localhost ~]# chmod +x /usr/local/apache-zookeeper-3.8.4-bin/bin/zkStart.sh
[root@localhost ~]# chmod +x /usr/local/apache-zookeeper-3.8.4-bin/bin/zkStop.sh
[root@localhost ~]# chmod +x /usr/local/apache-zookeeper-3.8.4-bin/bin/zkStatus.sh
  • 1
  • 2
  • 3

e、执行文件

[root@localhost ~]# /usr/local/apache-zookeeper-3.8.4-bin/bin/zkStart.sh
[root@localhost ~]# /usr/local/apache-zookeeper-3.8.4-bin/bin/zkStop.sh
[root@localhost ~]# /usr/local/apache-zookeeper-3.8.4-bin/bin/zkStatus.sh
  • 1
  • 2
  • 3

四、访问节点

[root@localhost ~]# /usr/local/apache-zookeeper-3.8.4-bin/bin/zkCli.sh -server 192.168.1.100:2181
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/976044
推荐阅读
相关标签
  

闽ICP备14008679号