• 如何在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
    

    e、删除:删除压缩文件

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

    f、创建目录:

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

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

    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
    

    ②、改配置:

    [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
    

    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
    

    注意:节点拷贝操作只在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
    

    192.168.1.101节点:

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

    192.168.1.102节点:

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

    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
    

    注意:每个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
    

    注意:每个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
    

    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
    

    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
    

    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
    

    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
    

    四、访问节点

    [root@localhost ~]# /usr/local/apache-zookeeper-3.8.4-bin/bin/zkCli.sh -server 192.168.1.100:2181
    
  • 相关阅读:
    MySQL审计插件介绍
    Java后端开发面试题——JVM虚拟机篇
    VC6 MFC Dialog as apllication 编程
    Google vs IBM vs Microsoft: 哪个在线数据分析师证书最好
    流式数据湖平台实战 | HudiSQL DML
    springBoot整合讯飞星火认知大模型
    Java格式化字符串嵌入变量
    Eureka介绍与使用
    系统架构师第一部分——架构设计基础
    厌烦了iPhone默认的热点名称?如何更改iPhone上的热点名称
  • 原文地址:https://blog.csdn.net/wangshuxuncom/article/details/139319865