系统环境(本次环境)
准备三台Linux服务器,系统要求如下: Linux: CentOS 6.5 64bit Server Java: 1.7.0_60 64-Bit Server VM ZooKeeper 使用Java语言编写,运行环境需要Java环境支持,建议使用JDK1.6以上版本。Java环境请自行配置。给三台服务器各自配置IP。下载ZooKeeper
在每一个服务器上下载ZooKeeper,本次使用的版本:zookeeper-3.4.5.tar.gz 将安装文件解压至某一目录:/usr/zookeeper-3.4.5#tar zxvf zookeeper-3.4.5.tar.gz
配置ZooKeeper
1)三台服务器分别创建数据目录Server1: #mkdir /var/zookeeperdataServer2: #mkdir /var/zookeeperdataServer3: #mkdir /var/zookeeperdata
2)分别在在数据目录中创建myid文件,并添加id值
Server1: #echo "1" > /var/zookeeperdata/myidServer2: #echo "2" > /var/zookeeperdata/myidServer3: #echo "3" > /var/zookeeperdata/myid
3)分别修改配置文件
Server1: #cd /usr/zookeeper-3.4.5/confServer1: #cp zoo_sample.cfg zoo.cfg Server1: #vi zoo.cfg tickTime=2000 initLinmit=10 syncLimit=5 dataDir=/var/zookeeperdata clientPort=2181 server.1=server1 IP:2888:3888 server.2=server2 IP:2888:3888 server.3=server3 IP:2888:3888
Server2: #cd /usr/zookeeper-3.4.5/confServer2: #cp zoo_sample.cfg zoo.cfg Server2: #vi zoo.cfg tickTime=2000 initLinmit=10 syncLimit=5 dataDir=/var/zookeeperdata clientPort=2181 server.1=server1 IP:2888:3888 server.2=server2 IP:2888:3888 server.3=server3 IP:2888:3888
Server3: #cd /usr/zookeeper-3.4.5/confServer3: #cp zoo_sample.cfg zoo.cfg Server3: #vi zoo.cfg tickTime=2000 initLinmit=10 syncLimit=5 dataDir=/var/zookeeperdata clientPort=2181 server.1=server1 IP:2888:3888 server.2=server2 IP:2888:3888 server.3=server3 IP:2888:3888
增加集群实例到配置文件中,server.X中”X”表示每个数据目录中的myid的值
4)分别设置ZooKeeper环境变量(可选操作,方便操作,推荐配置)#vi /etc/profile export ZOOKEEPER_HOME=/usr/zookeeper-3.4.5 export PATH=$PATH:$ZOOKEEPER_HOME/bin:$ZOOKEEPER_HOME/conf
分别启动ZooKeeper
Server1: #zkServer.sh startServer2: #zkServer.sh startServer3: #zkServer.sh start
查看运行状态
Server1: #zkServer.sh statusJMX enabled by defaultUsing config: /usr/zookeeper-3.4.5/bin/../conf/zoo.cfgMode: leader
Server2: #zkServer.sh statusJMX enabled by defaultUsing config: /usr/zookeeper-3.4.5/bin/../conf/zoo.cfgMode: follower
Server3: #zkServer.sh statusJMX enabled by defaultUsing config: /usr/zookeeper-3.4.5/bin/../conf/zoo.cfgMode: follower
我们看到有一台Server为leader,其余为follower。
查看ZooKeeper物理文件目录结构
Server1: #tree –L 3 /var/zookeeperdata /var/zookeeperdata├── myid├── version-2│ ├── acceptedEpoch│ ├── currentEpoch│ └── snapshot.0└── zookeeper_server.pid
Server2: #tree –L 3 /var/zookeeperdata /var/zookeeperdata├── myid├── version-2│ ├── acceptedEpoch│ ├── currentEpoch│ └── snapshot.0└── zookeeper_server.pid
Server3: #tree –L 3 /var/zookeeperdata /var/zookeeperdata├── myid├── version-2│ ├── acceptedEpoch│ ├── currentEpoch│ └── snapshot.0└── zookeeper_server.pid
停止ZooKeeper
Server1: #zkServer.sh stopJMX enabled by defaultUsing config: /usr/zookeeper-3.4.5/bin/../conf/zoo.cfgStooping zookeeper … STOPPED
Server2: #zkServer.sh stopJMX enabled by defaultUsing config: /usr/zookeeper-3.4.5/bin/../conf/zoo.cfgStooping zookeeper … STOPPED
Server3: #zkServer.sh stopJMX enabled by defaultUsing config: /usr/zookeeper-3.4.5/bin/../conf/zoo.cfgStooping zookeeper … STOPPED