• Zookeeper的安装及集群搭建


    一、单机版安装

      1、安装Zookeeper

        官网地址:https://zookeeper.apache.org/

      

    #下载安装包
    wget https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz
    #解压
    tar -zxvf apache-zookeeper-3.6.2-bin.tar.gz
    修改文件夹名称
    mv apache-zookeeper-3.6.2-bin zookeeper
    #创建数据保存文件夹
    mkdir zookeeper/data
    #修改配置文件
    vi zookeeper/conf/zoo_sample.cfg

        主要修改数据保存的目录

    dataDir=/root/rj/zk/zookeeper/data

         复制zoo_sample.cfg为zoo.cfg,因为zk默认加载zoo.cfg配置文件,这里复制一份或者重命名都可以

    cp zoo_sample.cfg zoo.cfg

        修改profile文件

    vi /etc/profile

        插入内容

    export ZK_HOME=/root/rj/zk/zookeeper
    export PATH=$ZK_HOME/bin:$PATH

        重新加载profile文件

    source /etc/profile

        启动zk

    [root@lcl-aliyun conf]# zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED

      2、操作Zookeeper  

    #开启
    [root@lcl-aliyun conf]# zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    
    #查看状态
    [root@lcl-aliyun ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/bin/../conf/zoo.cfg
    Client port found: 2181. Client address: localhost. Client SSL: false.
    Mode: standalone
    
    #重启
    [root@lcl-aliyun ~]# zkServer.sh restart
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/bin/../conf/zoo.cfg
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/bin/../conf/zoo.cfg
    Stopping zookeeper ... STOPPED
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    
    #停止
    [root@lcl-aliyun ~]# zkServer.sh stop
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/bin/../conf/zoo.cfg
    Stopping zookeeper ... STOPPED

    二、集群搭建

      1、先搭建一台服务器

        按照上面单机单机配置完毕后,还需要修改zoo.cfg配置文件,这里要说明一下配置server中的两个ip,2888是zk集群通讯的端口号,3888是集群leader选举的端口号,这两个端口号不局限于2888和3888,也不要求每一台都设置一样,可以随意设置,这个在搭建伪集群的时候就可以模拟。

    clientPort=2181
    dataDir=/root/rj/zk/zookeeper/data
    
    server.1=192.168.206.128:2888:3888
    server.2=192.168.206.129:2888:3888
    server.3=192.168.206.130:2888:3888
    server.4=192.168.206.134:2888:3888:observer

      创建myid并为myid赋值,这里主要是为了leader选举时,判断那台服务器被选举为leader的。

    echo 1 > /root/rj/zk/zookeeper/data/myid

      其实到这里一台服务器就配置完毕了,但是,有序多台服务器需要通讯,因此需要关闭防火墙

    #临时关闭
    systemctl stop firewalld.service
    #禁止防火墙(开机不启动)
    systemctl disable firewalld.service

      启动zk节点

    [root@localhost zk]# zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED

      查看状态,可以看到并没有启动成功,这是因为我们配置了4个节点,其中包含三个可选举节点,因此需要至少两台服务器选举才可以成功。

    [root@localhost zk]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/bin/../conf/zoo.cfg
    Client port found: 2181. Client address: localhost. Client SSL: false.
    Error contacting service. It is probably not running.

      然后配置剩余的三台服务器,其配置方式跟上面配置一样,只有一点区别,就是observer节点需要添加peerType=observer标识,将其标记为observer

    clientPort=2181
    dataDir=/root/rj/zk/zookeeper/data
    
    #observer服务器特有配置
    peerType=observer
    
    server.1=192.168.206.128:2888:3888
    server.2=192.168.206.129:2888:3888
    server.3=192.168.206.130:2888:3888
    server.4=192.168.206.134:2888:3888:observer

      启动server2服务器,同时查看其状态

    [root@localhost logs]# zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@localhost logs]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/bin/../conf/zoo.cfg
    Client port found: 2181. Client address: localhost. Client SSL: false.
    Mode: leader

      此时再来查看server1的状态,发现其也已经启动

    [root@localhost zk]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/bin/../conf/zoo.cfg
    Client port found: 2181. Client address: localhost. Client SSL: false.
    Mode: follower

      然后再启动server3和server4,查看状态,都已启动成功,server4为observer

    [root@localhost zk]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/bin/../conf/zoo.cfg
    Client port found: 2181. Client address: localhost. Client SSL: false.
    Mode: observer

    三、伪集群搭建

      1、创建4个不同的配置文件

    [root@lcl-aliyun conf]# cp zoo.cfg zoo1.cfg
    [root@lcl-aliyun conf]# cp zoo.cfg zoo2.cfg
    [root@lcl-aliyun conf]# cp zoo.cfg zoo3.cfg
    [root@lcl-aliyun conf]# cp zoo.cfg zoo4.cfg
    [root@lcl-aliyun conf]# ls
    configuration.xsl  zoo1.cfg  zoo3.cfg  zoo.cfg  log4j.properties zoo2.cfg zoo4.cfg zoo_sample.cfg

      2、修改配置文件

      修改zoo1.cfg--zoo4.cfg文件,修改端口号及目录,同时添加通讯端口和选举端口,并设置server.4为observer,在observer服务器中添加peerType=observer标识。

    clientPort=2181
    dataDir=/root/rj/zk/zookeeper/data1

    #observer服务器特有配置 peerType
    =observer
    server.
    1=8.131.245.53:2666:3666 server.2=8.131.245.53:2777:3777 server.3=8.131.245.53:2888:3888 server.4=8.131.245.53:2999:3999:observer

      创建数据存放目录

    mkdir /root/rj/zk/zookeeper/data1 /root/rj/zk/zookeeper/data2 /root/rj/zk/zookeeper/data3 /root/rj/zk/zookeeper/data4

      创建myid文件并为myid文件赋值

    [root@lcl-aliyun conf]# echo 1 > /root/rj/zk/zookeeper/data1/myid
    [root@lcl-aliyun conf]# echo 2 > /root/rj/zk/zookeeper/data2/myid
    [root@lcl-aliyun conf]# echo 3 > /root/rj/zk/zookeeper/data3/myid
    [root@lcl-aliyun conf]# echo 4 > /root/rj/zk/zookeeper/data4/myid

      启动服务器

    zkServer.sh start /root/rj/zk/zookeeper/conf/zoo1.cfg
    zkServer.sh start /root/rj/zk/zookeeper/conf/zoo2.cfg

      查看状态

    [root@lcl-aliyun logs]# zkServer.sh status /root/rj/zk/zookeeper/conf/zoo2.cfg
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/conf/zoo2.cfg
    Client port found: 2182. Client address: localhost. Client SSL: false.
    Mode: leader
    [root@lcl-aliyun logs]# zkServer.sh status /root/rj/zk/zookeeper/conf/zoo1.cfg
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/conf/zoo1.cfg
    Client port found: 2181. Client address: localhost. Client SSL: false.
    Mode: follower

      集群启动成功,然后再启动剩余的两个虚拟主机,启动后查看状态

    [root@lcl-aliyun logs]# zkServer.sh status /root/rj/zk/zookeeper/conf/zoo3.cfg
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/conf/zoo3.cfg
    Client port found: 2183. Client address: localhost. Client SSL: false.
    Mode: follower
    [root@lcl-aliyun logs]# zkServer.sh status /root/rj/zk/zookeeper/conf/zoo4.cfg
    ZooKeeper JMX enabled by default
    Using config: /root/rj/zk/zookeeper/conf/zoo4.cfg
    Client port found: 2184. Client address: localhost. Client SSL: false.
    Mode: observer
    ------------------------------------------------------------------
    -----------------------------------------------------------
    ---------------------------------------------
    朦胧的夜 留笔~~
  • 相关阅读:
    Redis源码剖析之字典(dict)
    Redis源码剖析之跳表(skiplist)
    面试题精选:神奇的斐波那契数列
    awk实现类sql的join操作
    [翻译]CAP理论及其证明
    今年是冷冬?我爬了北京10年的气温,哟 还真是!
    python 等间隔抽取一定数量的数据
    操作系统-第十章-文件系统
    操作系统-第九章-虚拟内存管理
    操作系统-第八章-内存管理
  • 原文地址:https://www.cnblogs.com/liconglong/p/14559374.html
Copyright © 2020-2023  润新知