Ubuntu 16.04 LTS搭建ZooKeeper集群

北极之光

准备

系统:Ubuntu 16.04 LTS

ZooKeeper安装

安装JDK

zookeeper运行需要java的支持,所以我们要首先安装jdk。此处不再赘述,之前有写过相关博客。

安装ZooKeeper

下载并解压安装包

从官方提供的下载页面下载Zookeeper-3.4.11,并解压文件到/usr目录下。

1
2
wget http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.11/
tar -zxvf zookeeper-3.4.11.tar.gz -C /usr

修改配置文件

在/usr/zookeeper-3.4.11/conf下复制zoo_sample.cfg文件并重名为zoo.cfg

1
cp conf/zoo_sample.cfg conf/zoo.cfg

把zookeeper加入到环境变量,此时完整的环境应该是这样的

1
2
3
4
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin:$PATH"
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export JAVA_HOME=/usr/jdk1.8.0
export ZOOKEEPER_HOME=/usr/zookeeper-3.4.11

保存修改之后,执行source命令使刚刚修改的环境生效

1
source /etc/environment

启动

如果上面的一系列操作都顺利完成之后,就可以启动ZooKeeper了

1
sudo bin/zkServer.sh start

ZooKeeper集群搭建

如果要运行zookeeper集群的话,最好部署3,5,7个zookeeper节点。本次实验我们是以3个节点进行的。三台服务器的ip分别是172.31.3.131、172.31.4.22、172.31.4.29

准备

三台服务器按上述方法安装好ZooKeeper

在搭建zookeeper集群时,一定要停止已经启动的zookeeper。

创建文件夹

我们需要创建两个文件夹放置内存数据库快照和日志文件

1
2
3
cd /usr/zookeeper-3.4.11
sudo mkdir data
sudo mkdir logs

修改zoo.cfg文件

主要修改dataDir和dataLogDir,并且添加Server地址,修改后的zoo.cfg文件如下

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
29
30
31
32
33
# 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=/usr/zookeeper-3.4.11/data
dataLogDir=/usr/zookeeper-3.4.11/logs
# the port at which the clients will connect
clientPort=2181
server.10=172.31.3.131:2888:3888
server.11=172.31.4.22:2888:3888
server.12=172.31.4.29:2888:3888
# 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

配置文件参数说明

参数 说明
tickTime ZK中的一个时间单元。ZK中所有时间都是以这个时间单元为基础,进行整数倍配置的。例如,session的最小超时时间是2*tickTime
initLimit 用来配置zookeeper接受客户端(这里所说的客户端不是用户连接zookeeper服务器的客户端,而是zookeeper服务器集群中连接到leader的follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数
syncLimit 在运行过程中,Leader负责与ZK集群中所有机器进行通信,例如通过一些心跳检测机制,来检测机器的存活状态。如果L发出心跳包在syncLimit之后,还没有从F那里收到响应,那么就认为这个F已经不在线了。注意:不要把这个参数设置得过大,否则可能会掩盖一些问题。(No Java system property)
dataDir 存储快照文件snapshot的目录。默认情况下,事务日志也会存储在这里。建议同时配置参数dataLogDir, 事务日志的写性能直接影响zk性能。
dataLogDir 事务日志输出目录。尽量给事务日志的输出配置单独的磁盘或是挂载点,这将极大的提升ZK性能。
clientPort 客户端连接server的端口,即对外服务端口,一般设置为2181
server.x=[hostname]:nnnnn[:nnnnn] 这里的x是一个数字,与myid文件中的id是一致的。右边可以配置两个端口,第一个端口用于F和L之间的数据同步和其它通信,第二个端口用于Leader选举过程中投票通信。

创建ServerID标识

在/usr/zookeeper-3.4.11/data下存放myid,通过上面的配置文件参数说明,我们知道zoo.cfg中server.x=[hostname]:nnnnn[:nnnnn]中的x应该与myid文件中的id一致。因此,ip为172.31.3.131的服务器上的myid应该为10。

1
echo 10 > /usr/zookeeper-3.4.11/data/myid

以此类推,ip为172.31.4.22的服务器上的myid是11,ip为172.31.4.29的服务器上的myid是12。

启动

1
2
cd /usr/zookeeper-3.4.11/
sudo bin/zkServer.sh start

zkServer.sh start-foreground也可以用这个

查看状态

1
2
cd /usr/zookeeper-3.4.11/
sudo bin/zkServer.sh status

ZooKeeper Status

通过上图,我们可以看到其中两台服务器是follow模式,一台是leader模式,这说明ZooKeeper集群已经搭建成功。

参考&引用

ZooKeeper官方文档

更新时间

发布时间 : 2017-11-15

看你的了!