环境准备
- java1.6以上的软件包
- zookeeper安装包
- 三台及以上的服务器(带Linux系统)
我的环境
名称 | 版本 |
---|---|
JDK | java-1.8.0-openjdk-src.x86_64 |
ZooKeeper | 3.7.0 |
CentOS | 7.9 |
服务器信息
节点名称 | IP地址 |
---|---|
ClusterNode1 | 192.168.1.1 |
ClusterNode2 | 192.168.1.2 |
ClusterNode3 | 192.168.1.3 |
安装
安装下载工具并安装jdk
yum install wget java-1.8.0-openjdk-src.x86_64 -y
- jdk版本可以使用yum search jdk搜索
下载ZooKeeper安装包
wget https://downloads.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
创建zookeeper目录并解压
mkdir -p zookeeper/{data,logs}| tar -zxvf apache-zookeeper-3.7.0-bin.tar.gz --strip-components 1 -C zookeeper
- 去除目录结构加上 --strip-components N(N是目录深度)
配置zoo.cfg
首次使用 ZooKeeper,需要将 $ZK_HOME 下的 zoo_sample.cfg 文件重命名为 zoo.cfg,并进行以下配置
dataDir=/$ZK_HOME/data # Zookeeper服务器存储快照文件的目录,必须配置
dataLogDir=/$ZK_HOME/logs # Zookeeper服务器存储事务日志的目录
clientPort=2181 # 服务器对外服务端口,一般设置为2181
server.x=IP1:2888:3888
server.x=IP2:2888:3888
server.x=IP3:2888:3888
- server.x中的x是一个数字,与myid文件中的id是一致的,一般设置成IP地址最后一位。右边配置了两个端口,第一个端口用于数据同步和其它通信,第二个端口用于Leader选举过程中投票通信
创建myid文件
echo "x" > /$ZK_HOME/data/myid
我的zk配置文件
节点:ClusterNode1
tickTime=2000
initLimit=10
syncLimit=5
clientPort=2181
dataDir=/root/zookeeper/data
dataLogDir=/root/zookeeper/logs
server.1=192.168.3.1:2888:3888
server.2=192.168.3.2:2888:3888
server.3=192.168.3.3:2888:3888
节点:ClusterNode2
tickTime=2000
initLimit=10
syncLimit=5
clientPort=2181
dataDir=/root/zookeeper/data
dataLogDir=/root/zookeeper/logs
server.1=192.168.3.1:2888:3888
server.2=192.168.3.2:2888:3888
server.3=192.168.3.3:2888:3888
节点:ClusterNode3
tickTime=2000
initLimit=10
syncLimit=5
clientPort=2181
dataDir=/root/zookeeper/data
dataLogDir=/root/zookeeper/logs
server.1=192.168.3.1:2888:3888
server.2=192.168.3.2:2888:3888
server.3=192.168.3.3:2888:3888
验证
节点:ClusterNode1
./zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /root/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
节点:ClusterNode2
./zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /root/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
节点:ClusterNode3
./zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /root/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower