zookeeper集群搭建与配置

服务器信息

hosts ip
zookeeper01 192.168.122.217
zookeeper02 192.168.122.132
zookeeper03 192.168.122.112

软件版本

1
2
3
jdk-7u80-linux-x64.rpm
zookeeper-3.4.6.tar.gz
官方下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/

安装jdk

三台服务器均需要安装

1
rpm -vih jdk-7u80-linux-x64.rpm

zookeeper的安装配置

在zookeeper01上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
tar zxvf zookeeper-3.4.6.tar.gz 
mv zookeeper-3.4.6 /usr/local/zookeeper-3.4.6
vim /etc/profile
......
#设置zookeeper环境变量
#ZOOKEEPER
ZOOKEEPER=/usr/local/zookeeper-3.4.6
PATH=$PATH:$ZOOKEEPER/bin

source /etc/profile
cp /usr/local/zookeeper-3.4.6/conf/zoo_sample.cfg /usr/local/zookeeper-3.4.6/conf/zoo.cfg #拷贝zookeeper的配置文件
cd /usr/local/zookeeper-3.4.6/conf
sed -i 's#dataDir=/tmp/zookeeper#dataDir=/data/zookeeper#g' zoo.cfg #修改data目录
vim zoo.cfg
......
#server.A=B:C:D:A表示第几号服务器;B 是这个服务器的 ip 地址;C 与集群中的 Leader 服务器交换信息的端口;D 表示Leader 服务器挂了,执行选举时服务器相互通信的端口
server.1=192.168.122.217:2888:3888
server.2=192.168.122.132:2888:3888
server.3=192.168.122.112:2888:3888

mkdir -p /data/zookeeper
cd /data/zookeeper/
vim myid #常见myid文件
1 #此处的1对应server.1
#启动zookeeper
zkServer.sh start

在zookeeper02上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
tar zxvf zookeeper-3.4.6.tar.gz 
mv zookeeper-3.4.6 /usr/local/zookeeper-3.4.6
vim /etc/profile
......
#设置zookeeper环境变量
#ZOOKEEPER
ZOOKEEPER=/usr/local/zookeeper-3.4.6
PATH=$PATH:$ZOOKEEPER/bin

source /etc/profile

cp /usr/local/zookeeper-3.4.6/conf/zoo_sample.cfg /usr/local/zookeeper-3.4.6/conf/zoo.cfg #拷贝zookeeper的配置文件
cd /usr/local/zookeeper-3.4.6/conf
sed -i 's#dataDir=/tmp/zookeeper#dataDir=/data/zookeeper#g' zoo.cfg #修改data目录

vim zoo.cfg
......
#server.A=B:C:D:A表示第几号服务器;B 是这个服务器的 ip 地址;C 与集群中的 Leader 服务器交换信息的端口;D 表示Leader 服务器挂了,执行选举时服务器相互通信的端口
server.1=192.168.122.217:2888:3888
server.2=192.168.122.132:2888:3888
server.3=192.168.122.112:2888:3888

mkdir -p /data/zookeeper
cd /data/zookeeper/
vim myid #常见myid文件
2 #此处的2对应server.2
#启动zookeeper
zkServer.sh start
在zookeeper03上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
tar zxvf zookeeper-3.4.6.tar.gz 
mv zookeeper-3.4.6 /usr/local/zookeeper-3.4.6
vim /etc/profile
......
#设置zookeeper环境变量
#ZOOKEEPER
ZOOKEEPER=/usr/local/zookeeper-3.4.6
PATH=$PATH:$ZOOKEEPER/bin

source /etc/profile
cp /usr/local/zookeeper-3.4.6/conf/zoo_sample.cfg /usr/local/zookeeper-3.4.6/conf/zoo.cfg #拷贝zookeeper的配置文件
cd /usr/local/zookeeper-3.4.6/conf
sed -i 's#dataDir=/tmp/zookeeper#dataDir=/data/zookeeper#g' zoo.cfg #修改data目录
vim zoo.cfg
......
#server.A=B:C:D:A表示第几号服务器;B 是这个服务器的 ip 地址;C 与集群中的 Leader 服务器交换信息的端口;D 表示Leader 服务器挂了,执行选举时服务器相互通信的端口
server.1=192.168.122.217:2888:3888
server.2=192.168.122.132:2888:3888
server.3=192.168.122.112:2888:3888
mkdir -p /data/zookeeper
cd /data/zookeeper/
vim myid #常见myid文件
3 #此处的3对应server.3
#启动zookeeper
zkServer.sh start

查看zookeeper状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#zookeeper01
zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: leader #区别另外两台

#zookeeper02
zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower

#zookeeper03
zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower