CentOS安装ZooKeeper集群

关于ZooKeeper(单机版)的下载、安装、配置、启动、测试等,请参考另一篇文章:CentOS安装ZooKeeper

搭建集群只需在此基础上增加一些配置,本文也主要介绍这些和集群相关的配置,其他步骤不再赘述

假设节点数为3,ip分别为:10.20.1.52,10.20.1.53,10.20.1.54

一、配置

1、先安装并配置其中一台机器(10.20.1.52):
修改zoo.cfg
shell> cd /usr/local/zookeeper-3.5.3-beta/conf
shell> cp zoo_sample.cfg zoo.cfg
shell> vim zoo.cfg

末尾增加以下集群相关配置

server.1=10.20.1.52:2888:3888
server.2=10.20.1.53:2888:3888
server.3=10.20.1.54:2888:3888
增加myid文件
shell> mkdir /usr/local/zookeeper-3.5.3-beta/data
shell> echo 1 > /usr/local/zookeeper-3.5.3-beta/data/myid   #10.20.1.52

这里echo 1的数字”1“与zoo.cfgserver.1是一致的

2、复制到其他节点(10.20.1.53,10.20.1.54)

第一台机器配置好以后,直接复制到其他节点然后稍加修改即可(当然,如果你愿意,也可以按照相同的步骤再重复安装配置一次,注意修改myid文件的内容即可)

shell> scp -r /usr/local/zookeeper-3.5.3-beta [email protected]:/usr/local/
shell> scp -r /usr/local/zookeeper-3.5.3-beta [email protected]:/usr/local/

然后分别修改其他两台机器上的myid文件

shell> echo 2 > /usr/local/zookeeper-3.5.3-beta/data/myid   #10.20.1.53
shell> echo 3 > /usr/local/zookeeper-3.5.3-beta/data/myid   #10.20.1.55
3、关闭防火墙

CentOS 6:

shell> service iptables stop
shell> chkconfig iptables off

CentOS 7:

shell> systemctl stop firewalld
shell> systemctl disable firewalld

更多防火墙设置(不想简单粗暴的关闭防火墙,只开启指定端口的访问),请参考:CentOS防火墙设置

二、启动

分别启动3个ZooKeeper节点

shell> cd /usr/local/zookeeper-3.5.3-beta/
shell> bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

三、测试

1、查看启动状态

单机版的Mode: standalone不同,集群的Mode会显示为leader(主节点)或follower(从节点),默认集群中的第一个节点为leader

# 10.20.1.52
shell> bin/zkServer.sh status                                      
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
# 10.20.1.53
shell> bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
# 10.20.1.54
shell> bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
2、测试节点(或数据)同步

在其中一台机器(10.20.1.52)上创建节点,查看其他机器(10.20.1.53)上节点是否同步

shell> cd /usr/local/zookeeper-3.5.3-beta/
# 使用zkCli工具连接本机ZooKeeper,默认2181端口
shell> bin/zkCli.sh
# 查看节点,默认只有一个[zookeeper]
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
# 创建测试节点
[zk: localhost:2181(CONNECTED) 1] create /test
Created /test
# 再次查看,多了一个test节点
[zk: localhost:2181(CONNECTED) 2] ls /
[test, zookeeper]
# 使用connect连接其他机器(也可以直接通过zkCli.sh -server 10.20.1.53:2181连接)
[zk: localhost:2181(CONNECTED) 3] connect 10.20.1.53
# 看到test节点已经同步
[zk: 10.20.1.53(CONNECTED) 4] ls /
[test, zookeeper]

你可能感兴趣的:(CentOS安装ZooKeeper集群)