Linux安装zookeeper(伪集群)

发布时间 2023-12-30 21:05:22作者: 刀霸汉

环境:

系统: Alibaba Cloud Linux 3 (Soaring Falcon)

jdk: jdk8

1.下载安装包

zookeeper官网: https://zookeeper.apache.org/releases.html

找到对应版本,这里以稳定版 3.8.3 为例,在节点上下载

# wget https://dlcdn.apache.org/zookeeper/zookeeper-3.8.3/apache-zookeeper-3.8.3-bin.tar.gz  --no-check-certificate

 2.解压对应压缩包

# tar -xzvf apache-zookeeper-3.8.3-bin.tar.gz

将路径添加到环境变量中

# pwd
/home/apache-zookeeper-3.8.3-bin

编辑 /etc/profile,添加 

export ZOOKEEPER_HOME=/home/apache-zookeeper-3.8.3-bin

使之生效

source /etc/profile

3.数据及日志文件准备

创建数据存放目录

mkdir -p ./data/zk/data ./data/zk1/data ./data/zk2/data

创建日志目录

mkdir -p ./data/zk/dataLog ./data/zk1/dataLog ./data/zk2/dataLog

4.配置文件

修改配置文件

复制./conf/zoo_sample.cfg 并命名为 ./conf /zoo.cfg

cp ./conf/zoo_sample.cfg ./conf/zoo.cfg

修改内部配置参数如下

# 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=${ZOOKEEPER_HOME}/data
# 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.
#
# https://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# 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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
server.1=127.0.0.1:2001:3001
server.2=127.0.0.1:2002:3002
server.3=127.0.0.1:2003:3003

[root@iZbp1eruj60nrojc5y3u39Z conf]# vim zoo.cfg
[root@iZbp1eruj60nrojc5y3u39Z conf]# cat zoo.cfg
# 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=/home/apache-zookeeper-3.8.3-bin/data/zk/data
dataLogData=/home/apache-zookeeper-3.8.3-bin/data/zk/dataLog
# 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.
#
# https://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# 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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
server.1=127.0.0.1:2001:3001
server.2=127.0.0.1:2002:3002
server.3=127.0.0.1:2003:3003

同理复制并修改为 zoo1.cfg 和 zoo2.cfg,并修改数据存放文件夹、日志文件夹和端口号

 

在每个zookeeper下创建 myid文件,内容为 1、2、3, 就是每台zookeeper的机器ID.

echo 1 >${ZOOKEEPER_HOME}/data/zk/data/myid
echo 2 >${ZOOKEEPER_HOME}/data/zk1/data/myid
echo 3 >${ZOOKEEPER_HOME}/data/zk2/data/myid

5.启动

启动命令:

./bin/zkServer.sh start ./conf/zoo.cfg
./bin/zkServer.sh start ./conf/zoo1.cfg
./bin/zkServer.sh start ./conf/zoo2.cfg

成功提示:

# ./bin/zkServer.sh start ./conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: ./conf/zoo.cfg
Starting zookeeper ... STARTED

如安装失败,可查看安装日志目录 ${ZOOKEEPER_HOME}/logs 里面的out日志

查看状态

./bin/zkServer.sh status ./conf/zoo.cfg
./bin/zkServer.sh status ./conf/zoo1.cfg
./bin/zkServer.sh status ./conf/zoo2.cfg

状态提示:

follower:

# ./bin/zkServer.sh status ./conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: ./conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower

leader:

# ./bin/zkServer.sh status ./conf/zoo1.cfg
ZooKeeper JMX enabled by default
Using config: ./conf/zoo1.cfg
Client port found: 2182. Client address: localhost. Client SSL: false.
Mode: leader