zookeeper集群模式

集群模式

1.准备工作

准备三个虚拟机,注意关闭防火墙

192.168.0.11
192.168.0.12
192.168.0.13

每个虚拟机都安装好zookeeper之后,接下来要先创建几个文件夹.

创建zookeeper工作目录:

mkdir -p /usr/zookeeperdata/
mkdir -p /usr/zookeeperdata/data

创建zookeeper日志目录:

mkdir -p /usr/zookeeperdata/log

2.修改配置文件

修改zoo.cfx

# 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/zookeeperdata/data
dataLogDir=/usr/zookeeperdata/log
# the port at which the clients will connect
clientPort=2181
# 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.
#
# 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

server.1=192.168.0.11:2888:3888
server.2=192.168.0.12:2888:3888
server.3=192.168.0.13:2888:3888

请注意信息的变换:分别是dataDir/dataLogDir
以及最后的集群IP和端口.

上面这个配置文件是每一个虚拟机都需要配置的.配置完一个后可以使用scp命令传递给其他的虚拟机.

3.添加myid

注意上面的

server.1=192.168.0.11:2888:3888      
server.2=192.168.0.12:2888:3888      
server.3=192.168.0.13:2888:3888 

每个虚拟机都有自己的编号,分别是1/2/3,这实际上就是该虚拟机zookeeper的myid.
接下来创建myid文件
在192.168.0.11执行

echo 1 > /usr/zookeeperdata/data/myid

在192.168.0.12执行

echo 2 > /usr/zookeeperdata/data/myid

在192.168.0.13执行

echo 3 > /usr/zookeeperdata/data/myid

4.启动

已经可以启动了.分别在机器上执行

zkServer.sh start

然后分别查看状态
在192.168.0.11执行,可以看到是follower

[root@localhost ~]# zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zk/apache-zookeeper-3.5.9-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower

在192.168.0.12执行,可以看到是follower

[root@localhost ~]# zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zk/apache-zookeeper-3.5.9-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower

在192.168.0.13执行,可以看到是被选举为了leader.

[root@localhost ~]# zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zk/apache-zookeeper-3.5.9-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader

5.注意的地方

  • 配置文件里的
    server.{myid}={ip}:{leader服务器交换信息的端口}:{当leader服务器挂了后, 选举leader的端口}
  • 三台机器是通过选举算法选出leader
  • 集群后,任意机器修改节点其他机器的节点也会同步改变.

猜你喜欢

转载自blog.csdn.net/dmw412724/article/details/114021589