zookeeper

【Zookeeper】三节点集群搭建

版本:3.9.1

1. 准备机器

本文搭建一个拥有三个节点的ZK集群,系统使用CentOS Stream 9,三台机器需要网络互通,如果使用云服务器可以使用VPC,如果自己使用虚拟机搭建集群,可以通过修改hosts文件,配置域名和IP。

【三台机器】

修改/etc/hosts

192.168.233.101 linux1
192.168.233.102 linux2
192.168.233.103 linux3

2. 下载安装包

【三台机器】

可以手动下载,或者使用wget指令下载zk的安装包

wget https://dlcdn.apache.org/zookeeper/zookeeper-3.9.1/apache-zookeeper-3.9.1-bin.tar.gz
tar -zxvf apache-zookeeper-3.9.1-bin.tar.gz
mv apache-zookeeper-3.9.1-bin zookeeper

3. 安装JDK

ZK是Java编写的程序,需要Java运行环境。

【三台机器】

yum install -y java-1.8.0-openjdk*

配置环境变量:

[root@linux3 zookeeper]# vim /etc/profile.d/java-env.sh
[root@linux3 zookeeper]# cat /etc/profile.d/java-env.sh 
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b09-4.el9.x86_64
export PATH=$PATH:$JAVA_HOME/bin
[root@linux3 zookeeper]# source /etc/profile

4. 配置

zk的配置在conf目录下:

[root@linux3 conf]# pwd
/opt/software/zookeeper/conf
[root@linux3 conf]# ls
configuration.xsl  logback.xml  zoo_sample.cfg

需要将 zoo_sample.cfg重命名为 zoo.cfg,并修改内部配置,主要修改两个配置即可。

dataDir:数据存储位置,默认在/tmp/zookeeper下,修改为/var/lib/zookeeper

mkdir -p /var/lib/zookeeper

集群节点信息,需要配置三个节点的地址,以及内部通信端口。

# 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=/var/lib/zookeeper
# 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=linux1:2888:3888
server.2=linux2:2888:3888
server.3=linux3:2888:3888

dataDir下创建文件myid,文件内容分别是123

这里的数字要和上面配置的server.x中的x一致,它代表了服务的唯一标志。

[root@linux1 zookeeper]# echo "1" > /var/lib/zookeeper/myid
[root@linux2 zookeeper]# echo "2" > /var/lib/zookeeper/myid
[root@linux3 zookeeper]# echo "3" > /var/lib/zookeeper/myid

5. 启动

[root@linux1 zookeeper]# bin/zkServer.sh  start
ZooKeeper JMX enabled by default
Using config: /opt/software/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED


[root@linux2 zookeeper]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/software/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

[root@linux3 zookeeper]# bin/zkServer.sh  start
ZooKeeper JMX enabled by default
Using config: /opt/software/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

6. 测试

QuorumPeerMain

可以看到三个进程都在,再查看下集群的状态:

集群的状态

至此一个最基本的集群就搭建完毕了。

标签: Zookeeper

添加新评论