美文网首页
2017-12-26 搭建高可用zookeeper

2017-12-26 搭建高可用zookeeper

作者: lucasdada | 来源:发表于2018-01-29 15:53 被阅读0次

    搭建高可用zookeeper

    标签(空格分隔): 运维


    Step 1:安装jdk和jre

    下载最新版本的jdk
    地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html
    
    cd /usr
    mkdir  java
    cd java
    tar xzvf jdk-8u141-linux-x64.tar.gz
    
    # 设置环境变量
    vim /etc/profile
    # 加入以下内容,注意目录设置,jdk1.8.0_141要换成当前所下载的jdk版本
    # set java environment
    JAVA_HOME=/usr/java/jdk1.8.0_141
    JRE_HOME=/usr/java/jdk1.8.0_141/jre 
    CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
    PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
    export JAVA_HOME JRE_HOME CLASS_PATH PATH
    
    # 在当前shell会话中立马生效,而无需重新登录主机
    source /etc/profile
    
    # 查看java 版本以确认成功安装了jdk和jre
    java -version
    

    Step 2:搭建zookeeper集群

    参考文献-官方文档:
    http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_zkMulitServerSetup

    # 下载zookeeper-3.4.10到/home下
    地址:
    https://archive.apache.org/dist/zookeeper/zookeeper-3.4.10/
    
    cd /home
    tar xzfv zookeeper-3.4.10.tar.gz
    mkdir /data/zookeeperLog #zoo.conf中需要用到。存放zookeeper产生的log。
    vim /etc/hosts
    vim /home/zookeeper-3.4.10/conf/zoo.cfg
    # 每个集群中都加入该文件,该文件中写一个数字,代表该zookeeper节点在集群中的序号,取值正整数。
    # myid这个文件应该放在zoo.cfg中指定dataDir下目录下,这里的zoo.cfg中指定dataDir=/home。
    vim /home/myid
    

    在/etc/hosts中加入如下格式的内容

    10.30.x.x   slave-01
    10.30.x.x   slave-02
    10.30.x.x   slave-03
    10.30.x.x   slave-04
    10.30.x.x   slave-05
    

    注意:IP换成zookeeper的IP即可,主机名按照如上格式排序即可。目前生产环境的zookeeper集群设置5个节点。每一个节点都放在一个单独的主机上。

    zoo.cfg内容如下

    注意:最后面的几行等号右边写的slave-01是主机名。左边写的是zookeeper集群节点的标记,每一个zookeeper节点在集群中都有唯一一个序号,序号取值为正整数。目前序号最大可以写到255。

    # 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.
    dataLogDir=/data/zookeeperLog/
    dataDir=/home
    # 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=2000
    #
    # 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=10
    # Purge task interval in hours
    # Set to "0" to disable auto purge feature
    autopurge.purgeInterval=1
    server.1=slave-01:2888:3888
    server.2=slave-02:2888:3888
    server.3=slave-03:2888:3888
    server.4=slave-04:2888:3888
    server.5=slave-05:2888:3888
    

    至此,可以启动zookeeper,检查节点能否成功启动,节点之间是否有成功互相连接成集群.后续设置

    # 启动zookeeper
    /home/zookeeper-3.4.10/bin/zkServer.sh start
    # 停止zookeeper 
    /home/zookeeper-3.4.10/bin/zkServer.sh start
    # 检查zookeeper是否有成功连接成集群,要在每个zookeeper节点上检测所有zookeeper节点的状态,例如:
    /home/zookeeper-3.4.10/bin/zkCli.sh -server slave-01
    # 查看子节点
    ls
    

    Step 3:使用daemontools守护zookeeper进程。

    安装daemontools

    mkdir /package
    chmod 755 /package
    cd /package
    wget http://cr.yp.to/daemontools/daemontools-0.76.tar.gz
    tar zxf daemontools-0.76.tar.gz
    cd admin/daemontools-0.76
    vim src/error.h 找到:extern int errno; 改成:#include <errno.h> 
    # 或者:在src下的conf-cc文件的第一行最后添加如下代码即可  -include /usr/include/errno.h
    # vi src/conf-cc
    # 在最后加上 -include /usr/include/errno.h
    package/install
    

    监控zookeeper

    在/service下,新建文件夹zookeeper,新建run文件:
    cd /service
    mkdir zookeeper
    cd zookeeper
    vim run
    chmod 755 run
    run内容:

    #!/bin/bash
    exec 2>&1
    exec /home/zookeeper-3.4.4/bin/zkServer.sh start
    date
    

    执行付下命令启动功能启动daemontools

    supervise /service/zookeeper
    //或者可以用nohup以后台方式运行。如下:
    nohup supervise /service/zookeeper &
    

    如果出现
    supervise: fatal: unable to acquire /service/zookeeper/supervise/lock: temporary failure
    快速rm /service/zookeeper/supervise 重新执行 supervise /service/zookeeper 即可
    验证监控zookeeper是否成功:
    kill zookeeper进程,查看zookeeper的进程是否自动重启

    参考文献:
    http://cr.yp.to/daemontools/faq/create.html

    Step 4:添加daemontools开机启动

    # 在/etc/rc.d/rc.local中末尾加入
    /command/svscanboot &
    

    相关文章

      网友评论

          本文标题:2017-12-26 搭建高可用zookeeper

          本文链接:https://www.haomeiwen.com/subject/vuvdgxtx.html