我们今天要搭建的zookeeper服务想需要用三台虚拟机来进行搭建;
Zookeeper 的工作原理 zookeeper 的核心是原子广播,使用这个机制保证各个 Server 间的同步,实现原子广播的协议叫做 Zab 协议 Zab 协议有两种模式:恢复模式(选主)和广播模式(同步),当服务启动或 Leader 宕机,Zab 就进入恢复模式,一旦 Leader 被选举出来,且大多数 Server 完成 Leader 状态同步后,即进入广播模式 每个工作中的 Server 都有三种工作状态: LOOKING -- 搜寻 Leader LEADING -- 当前 Server 是集群 Leader FOLLOWING -- Leader 已经选举出,当前 Server 节点与 Leader 同步
1.软件的准备;jdk搭建java环境;
[root@01 zookeeper]# ls jdk-8u66-linux-x64.rpm zookeeper-3.4.10.tar
2.查看java环境是否处在 java-version,没有的话需要安装jdk软件包;
3.解压zookeeper包,并移动到usr下生成相关文件;
[root@01 zookeeper]# tar xf zookeeper-3.4.10.tar ^C [root@01 zookeeper]# mv zookeeper-3.4.10 /usr/local/zookeeper
4.为了标准化管理,我们在opt目录下创建zookeeper相关的配置文件、日志文件、数据文件;
进入到usr/local/zookeeper目录
mkdir -p /opt/zookeeper/{conf,logs,data}
cp conf/zoo_sample.cfg /opt/zookeeper/conf/zoo.cfg 拷贝usr文件到我们的opt文件里生成配置文件
5.修改日志文件
[root@vlnx251104 zookeeper]# vim conf/log4j.properties
6.修改其他配置文件
[root@vlnx251104 zookeeper]# vim bin/zkEnv.sh
#if [ "x$ZOOCFG" = "x" ]
#then
# ZOOCFG="zoo.cfg"
#fi
47 ZOOCFG="/opt/zookeeper/conf/zoo.cfg" 48 49 if [ -f "$ZOOCFGDIR/java.env" ] 50 then 51 . "$ZOOCFGDIR/java.env" 52 fi 53 54 if [ "x${ZOO_LOG_DIR}" = "x" ] 55 then 56 ZOO_LOG_DIR="/opt/zookeeper/logs" 57 fi 58 59 if [ "x${ZOO_LOG4J_PROP}" = "x" ] 60 then 61 ZOO_LOG4J_PROP="INFO,ROLLINGFILE" 62 fi
7.注释掉bin/zkServer.sh里面的部分内容
82 #fi 83 # 84 #if [ "x$2" != "x" ] 85 #then 86 # ZOOCFG="$ZOOCFGDIR/$2" 87 #fi 88 # 89 ## if we give a more complicated path to the config, don't screw around in $ZOOC FGDIR 90 #if [ "x$(dirname "$ZOOCFG")" != "x$ZOOCFGDIR" ] 91 #then 92 # ZOOCFG="$2" 93 #fi
8.创建一个编号文件
9. ./bin/zkServer.sh start 启动zookeeper
[root@01 zookeeper]# echo 1 > /opt/zookeeper/data/myid
到这里我们单台的zookeeper就完成了,接下来我们要做的就是把这台虚拟机的配置文件拷给另外两台虚拟机
[root@vlnx251104 zookeeper]# echo 1 > /opt/zookeeper/data/myid [root@vlnx251104 zookeeper]# scp -r /usr/local/zookeeper/ 192.168.251.105:/usr/local/ [root@vlnx251104 zookeeper]# scp -r /usr/local/zookeeper/ 192.168.251.106:/usr/local/ [root@vlnx251104 zookeeper]# scp -r /opt/zookeeper/ 192.168.251.105:/opt/ [root@vlnx251104 zookeeper]# scp -r /opt/zookeeper/ 192.168.251.106:/opt/
[root@vlnx251104 zookeeper]# echo 1 > /opt/zookeeper/data/myid [root@vlnx251105 ~]# echo 2 > /opt/zookeeper/data/myid [root@vlnx251106 ~]# echo 3 > /opt/zookeeper/data/myid
测试:
[root@01 zookeeper]# ./bin/zkCli.sh Connecting to localhost:2181 Welcome to ZooKeeper! JLine support is enabled WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0]