zookeeper安装与集群搭建
此处以centos系统下zookeeper安装为例,详细步骤可参考官网文档:zookeeper教程
一、单节点部署
1、下载zookeeper
wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
2、解压下载下来的压缩文件
tar -zxvf zookeeper-3.4.10.tar.gz
3、进入解压缩文件夹的conf目录,新建zoo.cfg配置文件(zookeeper启动时默认加载conf目录下的zoo.cfg文件)
cd zookeeper-3.4.10 cd conf/ touch zoo.cfg vi zoo.cfg
该目录下有一个样例配置文件,可供参考:zoo_sample.cfg
4、修改配置文件(默认监听服务器上所有网卡收到的请求):
vi zoo.cfg
修改完成后,配置文件内容如下:
tickTime=2000 dataDir=/opt/zookeeper-3.4.10/data clientPort=2181
官网介绍如下:
This file can be called anything, but for the sake of this discussion call it conf/zoo.cfg. Change the value of dataDir to specify an existing (empty to start with) directory. Here are the meanings for each of the fields: tickTime the basic time unit in milliseconds used by ZooKeeper. It is used to do heartbeats and the minimum session timeout will be twice the tickTime. dataDir the location to store the in-memory database snapshots and, unless specified otherwise, the transaction log of updates to the database. clientPort the port to listen for client connections
5、创建配置文件中提到的datadir目录:
mkdir -p /opt/zookeeper-3.4.10/data
6、启动zookeeper,安装目录bin目录下执行如下指令:
./zkServer.sh start
其他相关命令:
关闭:./zkServer.sh stop
重启: ./zkServer.sh restart
查看状态: ./zkServer.sh status
到此zookeeper安装完成
二、集群搭建
此处主要介绍在同一机器上搭建一伪集群,生成环境建议多机进行集群部署
1、将zk安装文件夹复制成三份
cp -dr zookeeper-3.4.10 zookeeper-3.4.10_1 cp -dr zookeeper-3.4.10 zookeeper-3.4.10_2
2、修改各集群节点配置文件
vi zookeeper-3.4.10/conf/zoo.cfg vi zookeeper-3.4.10_1/conf/zoo.cfg vi zookeeper-3.4.10_2/conf/zoo.cfg
修改后给配置文件分别为:
zookeeper-3.4.10/conf/zoo.cfg
tickTime=2000 dataDir=/opt/zookeeper-3.4.10/data clientPort=2181 initLimit=5 syncLimit=2 server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:4888:5888 server.3=127.0.0.1:6888:7888
zookeeper-3.4.10_1/conf/zoo.cfg
tickTime=2000 dataDir=/opt/zookeeper-3.4.10_1/data clientPort=3181 initLimit=5 syncLimit=2 server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:4888:5888 server.3=127.0.0.1:6888:7888
zookeeper-3.4.10_2/conf/zoo.cfg
tickTime=2000 dataDir=/opt/zookeeper-3.4.10_2/data clientPort=4181 initLimit=5 syncLimit=2 server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:4888:5888 server.3=127.0.0.1:6888:7888
server后面的端口,前者用于zk间节点通信,后者用于leader选举
附上一段官网原文:
tickTime=2000 dataDir=/var/lib/zookeeper clientPort=2181 initLimit=5 syncLimit=2 server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888 The new entry, initLimit is timeouts ZooKeeper uses to limit the length of time the ZooKeeper servers in quorum have to connect to a leader. The entry syncLimit limits how far out of date a server can be from a leader. With both of these timeouts, you specify the unit of time using tickTime. In this example, the timeout for initLimit is 5 ticks at 2000 milleseconds a tick, or 10 seconds. The entries of the form server.X list the servers that make up the ZooKeeper service. When the server starts up, it knows which server it is by looking for the file myid in the data directory. That file has the contains the server number, in ASCII. Finally, note the two port numbers after each server name: " 2888" and "3888". Peers use the former port to connect to other peers. Such a connection is necessary so that peers can communicate, for example, to agree upon the order of updates. More specifically, a ZooKeeper server uses this port to connect followers to the leader. When a new leader arises, a follower opens a TCP connection to the leader using this port. Because the default leader election also uses TCP, we currently require another port for leader election. This is the second port in the server entry.
3、为每个节点分别新建myid文件,文件内容对应上述配置文件中server的编号
echo "1" > zookeeper-3.4.10/data/myid echo "2" > zookeeper-3.4.10_1/data/myid echo "3" > zookeeper-3.4.10_2/data/myid
4、启动zookeeper
zookeeper-3.4.10/bin/zkServer.sh start zookeeper-3.4.10_1/bin/zkServer.sh start zookeeper-3.4.10_2/bin/zkServer.sh start
5、查看集群状态
到此zk集群配置完成