15年的老文章,花点时间用MarkDown重刷一遍,看起来舒服些,强迫症没办法。。。

内容摘要

本文介绍了zookeeper伪集群搭建的详细步骤

集群搭建

最近公司用到了zookeeper做集群管理,这里按步骤记录一下其中的一些经验,也给后来的同学一些参考。由于这里只有一台服务器,因此搭建的其实是伪集群,多台服务器的搭建流程类似。

集群搭建环境

1
2
3
4
CentOS-6.6 64bit   
2.6.32-504.el6.x86_64  
CPUintel-i7 3.6G  
2G  

集群搭建步骤

确保机器安装了jdk

1
2
3
4
[root@rocket ~]# java -version
openjdk version "1.8.0_51"
OpenJDK Runtime Environment (build 1.8.0_51-b16)
OpenJDK 64-Bit Server VM (build 25.51-b03, mixed mode)

如果找不到 jdk,可以通过 yum 进行安装,如果发行版不是 CentOS,请自行搜索对应发行版的安装方法。

1
[root@rocket local]# yum -y install java

下载zookeeper,选一个合适的版本

官网找一个合适的镜像进行下载。这里使用北理的镜像进行下载。

1
[root@rocket local]# wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz

下载完成之后分别解压到

1
2
3
[root@rocket local]# tar -zxvf zookeeper-3.4.6.tar.gz zookeeper-server1
[root@rocket local]# tar -zxvf zookeeper-3.4.6.tar.gz zookeeper-server2
[root@rocket local]# tar -zxvf zookeeper-3.4.6.tar.gz zookeeper-server3

拷贝配置文件

1
2
3
[root@rocket local]# cd /usr/local/zookeeper-server1/;cp conf/zoo_sample.cfg conf/zoo_test.cfg
[root@rocket local]# cd /usr/local/zookeeper-server2/;cp conf/zoo_sample.cfg conf/zoo_test.cfg
[root@rocket local]# cd /usr/local/zookeeper-server3/;cp conf/zoo_sample.cfg conf/zoo_test.cfg

修改配置文件

3个节点配置的主要区别是data目录及端口不同。

zookeeper-server1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

dataDir=/tmp/zookeeper-server1/data
dataLogDir=/tmp/zookeeper-server1/logs

# the port at which the clients will connect
clientPort=2181

server.1=127.0.0.1:8880:7770
server.2=127.0.0.1:8881:7770
server.3=127.0.0.1:8882:7770

zookeeper-server2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

dataDir=/tmp/zookeeper-server2/data
dataLogDir=/tmp/zookeeper-server2/logs

# the port at which the clients will connect
clientPort=3181

server.1=127.0.0.1:8880:7770
server.2=127.0.0.1:8881:7770
server.3=127.0.0.1:8882:7770

zookeeper-server3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

dataDir=/tmp/zookeeper-server3/data
dataLogDir=/tmp/zookeeper-server3/logs

# the port at which the clients will connect
clientPort=4181

server.1=127.0.0.1:8880:7770
server.2=127.0.0.1:8881:7770
server.3=127.0.0.1:8882:7770

建立zookeeper运行目录,data目录和log目录

1
2
3
[root@rocket local]# mkdir -p zookeeper-server1/logs zookeeper-server1/data
[root@rocket local]# mkdir -p zookeeper-server2/logs zookeeper-server2/data
[root@rocket local]# mkdir -p zookeeper-server3/logs zookeeper-server3/data

查看当前运行目录情况:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[root@rocket tmp]# tree
 zookeeper-server1
    data
    logs
 zookeeper-server2
    data
    logs
 zookeeper-server3
     data
 logs

在data目录中创建文件 myid 文件,每个文件中分别写入当前机器的server id

1
2
3
[root@rocket tmp]# echo "1" > zookeeper-server1/data/myid
[root@rocket tmp]# echo "2" > zookeeper-server2/data/myid
[root@rocket tmp]# echo "3" > zookeeper-server3/data/myid

在每个目录中启动zookeeper

1
2
3
[root@rocket local]# cd /usr/local/zookeeper-server1/;bin/zkServer.sh start
[root@rocket local]# cd /usr/local/zookeeper-server2/;bin/zkServer.sh start
[root@rocket local]# cd /usr/local/zookeeper-server3/;bin/zkServer.sh start

查看启动状态

1
2
3
4
[root@rocket zookeeper-server1]# cd /usr/local/zookeeper-server1/bin/zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper-server1/bin/../conf/zoo.cfg
Mode: follower

注意,这里如果提示

1
Error contacting service. It is probably not running.

说明进程启动失败或集群未建立成功,这时需要查看对应目录中的zookeeper.out日志去找到相应的原因并去解决它。

测试连接zookeeper集群

1
[root@rocket local]# cd zookeeper-server1;bin/zkCli.sh -server localhost:2181

可以看到这里已经连接成功了,而且能查看节点情况,说明zookeeper集群已经开始工作了。