Zookeeper集群搭建
Zookeeper集群搭建
1.搭建Zookeeper集群
1)准备工作
-
安装JDK
-
下载ZooKeeper:ZooKeeper官网下载地址
-
将Zookeeper上传到服务器
-
解压Zookeeper,并创建data目录,将conf下的zoo_sample.cfg(zookeeper提供的示例配置文件)文件改名为zoo.cfg
mkdir /opt/zookeeper tar -zxvf /opt/zookeeper/zookeeper-3.4.6.tar.gz mkdir /usr/local/zookeeper mv /opt/zookeeper/zookeeper-3.4.6 /usr/local/zookeeper #将zookeeper提供的示例配置文件zoo_sample.cfg改名为zoo.cfg mv zoo_sample.cfg zoo.cfg
-
建立
/user/local/zookeeper-cluster
,并将解压后的Zookeeper复制到以下三个目录
mkdir /usr/local/zookeeper-cluster
mkdir /usr/local/zookeeper-cluster/zookeeper-1
mkdir /usr/local/zookeeper-cluster/zookeeper-2
mkdir /usr/local/zookeeper-cluster/zookeeper-3
#-r 表示递归复制整个文件
cp -r /usr/local/zookeeper /usr/local/zookeeper-cluster/zookeeper-1/
cp -r /usr/local/zookeeper /usr/local/zookeeper-cluster/zookeeper-2/
cp -r /usr/local/zookeeper /usr/local/zookeeper-cluster/zookeeper-3/
-
配置每一个 Zookeeper 的 dataDir(zoo.cfg) clientPort 分别为 2181 2182 2183
mkdir /usr/local/zookeeper-cluster/zookeeper-1/data mkdir /usr/local/zookeeper-cluster/zookeeper-2/data mkdir /usr/local/zookeeper-cluster/zookeeper-3/data
修改/usr/local/zookeeper-cluster/zookeeper-1/zookeeper/zookeeper-3.4.6/conf/zoo.cfg
vi /usr/local/zookeeper-cluster/zookeeper-1/zookeeper/zookeeper-3.4.6/conf/zoo.cfg
#修改以下内容
clientPort=2181
dataDir=/usr/local/zookeeper-cluster/zookeeper-1/data
修改/usr/local/zookeeper-cluster/zookeeper-2/zookeeper/zookeeper-3.4.6/conf/zoo.cfg
vi /usr/local/zookeeper-cluster/zookeeper-2/zookeeper/zookeeper-3.4.6/conf/zoo.cfg
#修改以下内容
clientPort=2182
dataDir=/usr/local/zookeeper-cluster/zookeeper-2/data
修改/usr/local/zookeeper-cluster/zookeeper-3/zookeeper/zookeeper-3.4.6/conf/zoo.cfg
vi /usr/local/zookeeper-cluster/zookeeper-3/zookeeper/zookeeper-3.4.6/conf/zoo.cfg
#修改以下内容
clientPort=2183
dataDir=/usr/local/zookeeper-cluster/zookeeper-3/data
2)配置集群
-
在每个 zookeeper 的 data 目录下创建一个 myid 文件,内容分别是 1、2、3 。这个文件就是记录每个服务器的 ID
#创建myid文件 touch /usr/local/zookeeper-cluster/zookeeper-1/data/myid #编辑文件 vi /usr/local/zookeeper-cluster/zookeeper-1/data/myid
-
在每一个 zookeeper 的 zoo.cfg 配置客户端访问端口(clientPort)和集群服务器 IP 列表。
集群服务器 IP 列表如下
#编辑zoo.cfg文件,在文件末尾添加如下内容; 2、3同理
#注意这里的服务器id就是我们前面所配置的id需要一一对应
vi /usr/local/zookeeper-cluster/zookeeper-1/zookeeper/zookeeper-3.4.6/conf/zoo.cfg
server.1=192.168.25.140:2881:3881
server.2=192.168.25.140:2882:3882
server.3=192.168.25.140:2883:3883
解释:server.服务器 ID=服务器 IP 地址:服务器之间通信端口:服务器之间投票选举端口
3)启动集群
启动集群就是分别启动每个实例。
#启动zk
/usr/local/zookeeper-cluster/zookeeper-1/zookeeper/zookeeper-3.4.6/bin/zkServer.sh start