Zookeeper 集群搭建

一、官方网站下载zookeeper-3.4.6.tar.gz

    http://www.apache.org/dyn/closer.cgi/zookeeper/

二、将下载得到的zookeeper-3.4.6.tar.gz放到指定的目录中,自己统一放在了/usr/appdata/zookeeper-3.4.6/下,tar解压缩即可

   #tar -zxvf zookeeper-3.4.6.tar.gz

三、设置环境变量:
    
    为了方便运行zkServer.sh脚本,我们将zookeeper的bin路径加入到/etc/profile中,作为一个全局变量进行输出到PATH中

     #vi /etc/profile

     ZOOKEEPER_HOME=/usr/appdata/zookeeper-3.4.6
     export ZOOKEEPER_HOME
     export PATH=$PATH:$ZOOKEEPER_HOME/bin:$ZOOKEEPER_HOME/conf

     #source /etc/profile

     修改完成之后运行source /etc/profile使修改生效

四、设置host

    #vi /etc/hosts

    172.16.15.13 server1
    172.16.15.14 server2
    172.16.15.15 server3

    增加集群服务器的IP和host的对应关系到/etc/hosts(所有节点都同样增加)
       

五、建立zookeeper配置文件(可以先在一个节点上配置,后期直接复制到其他节点即可)
   
    进入到zookeeper的conf/中,将zoo_sample.cfg拷贝成zoo.cfg:

    #cp zoo_sample.cfg zoo.cfg

    #vi zoo.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=/home/zookeeper/local/var/zookeeper/data
dataLogDir=/home/zookeeper/local/var/zookeeper/log
# 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=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=server1:2888:3888
server.2=server2:2888:3888
server.3=server3:2888:3888

    端口2181
    设置dataDir为一个指定目录存放zookeeper的协调数据。最后添加集群中的节点,根据server.X的号码在相应的节点上的dataDir下建立myid文件,输入自身的“X”作为内容即可,比如server1节点上的myid只输入1即可。

  
   #cd /home/zookeeper/local/var/zookeeper/data
   #echo "1" >> myid
  
   server1节点上的myid输入1,server2节点上的myid输入2,server3节点上的myid输入3

六、复制zookeeper-3.4.6到其他各节点
    
    这里使用scp -r zookeeper-3.4.6/ hadoop@server2:实现,注意每个节点的dataDir下的myid文件要修改成自身的Server-X号码


七、运行zkServer.sh
   
    #zkServer.sh start

    在所有节点上分别运行zkServer.sh start命令

八、查看运行状态

    #zkServer.sh status

    JMX enabled by default
    Using config: /usr/appdata/zookeeper-3.4.6/bin/../conf/zoo.cfg
    Mode: follower


九、配置开机启动zookeeper文件

#cd /etc/rc.d/init.d
#touch zookeeper
#chmod +x zookeeper
#vi zookeeper

添加如下内容

#! /bin/sh
# chkconfig: 2345 10 90
# description: zookeeper

ZOOKEEPER_PATH=/usr/appdata/zookeeper-3.4.6/bin/

case "$1" in
     start)
echo "start zookeeper service..."
sh ${ZOOKEEPER_PATH}/zkServer start
;;
stop)
echo "stop zookeeper service..."
sh ${ZOOKEEPER_PATH}/zkServer stop
;;
     restart)
echo "restart zookeeper service..."
sh ${ZOOKEEPER_PATH}/zkServer restart
;;   
     status)
echo "zookeeper status"
sh ${ZOOKEEPER_PATH}/zkServer status
;; 
     *)
echo "require start|stop|status|restart"
exit1
;;
esac

添加到开机启动服务

chkconfig --add zookeeper
chkconfig zookeeper on
chkconfig --list              #查看开机启动的服务


然后就可以使用
#service zookeeper start
启动啦


  


你可能感兴趣的:(zookeeper)