文章目录
一、Zookeeper 是什么二、准备工作:主机准备三、准备工作:JDK 安装四、下载 Zookeeper五、安装 Zookeeper六、配置 Zookeeper 参数1、配置zoo.cfg2、创建 Zookeeper 的ID
七、配置 Zookeeper 环境变量八、测试安装结果1、启动 Zookeeper2、停止Zookeeper3、查看 Zookeeper状态3、查看 Zookeeper数据
九、配置文件十、注意问题
一、Zookeeper 是什么
官方地址:https://www.apache.org/dyn/closer.lua/zookeeper
ZooKeeper 是一个开源的分布式协调服务,它提供了一个高性能的、可靠的分布式环境,用于协调和管理分布式应用程序的配置、状态和元数据信息。ZooKeeper 旨在解决分布式系统中的一致性和协调问题。它提供了一个简单的文件系统层次结构,类似于标准文件系统,但是用于存储和管理分布式应用程序的数据(注意他的主要作用不是用来存数据)。ZooKeeper 主要特性
分布式协调:ZooKeeper 提供了一套原语,如锁、队列、同步和通知机制,用于分布式应用程序之间的协调和同步。高性能:ZooKeeper 的设计目标之一是提供低延迟和高吞吐量的访问性能,以满足高负载的分布式应用程序的需求。可靠性:ZooKeeper 使用了一致性协议(ZAB)来确保数据的一致性和可靠性。它采用主从架构,支持自动故障恢复和数据冗余,以提供高可用性和可靠性。容错性:ZooKeeper 允许在集群中部署多个服务器实例,以提供容错性。即使有部分服务器故障,ZooKeeper 仍然可以继续正常运行,保持服务的可用性。 ZooKeeper 应用方向:分布式锁、配置管理、集群管理、分布式队列等。
二、准备工作:主机准备
要安装Zookeeper至少需准备2N+1台主机,我这里使用3台虚拟主机测试,如下:
主机id名称IP第一台Zookeeper主机1zookeeper1192.168.8.51第二台Zookeeper主机2zookeeper2192.168.8.52第三台Zookeeper主机3zookeeper3192.168.8.53
三、准备工作:JDK 安装
自行下载JDK8的安装包,官方地址:https://www.oracle.com/java/technologies/downloads/#java8
rpm -i jdk-8u381-linux-x64.rpm
vi /etc/profile
export JAVA_HOME=/usr/java/default
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile
注意:3台主机都要安装配置
四、下载 Zookeeper
使用wget下载
yum install wget -y
wget https://dlcdn.apache.org/zookeeper/zookeeper-3.8.2/apache-zookeeper-3.8.2-bin.tar.gz
注意地址可能会有变动,请自行到官方网站下载:apache-zookeeper-3.8.2-bin.tar.gz
下载地址:https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.8.2/apache-zookeeper-3.8.2-bin.tar.gz 注意:3台主机都要下载,也可以在一台上配置好后使用scp命令复制过去。
五、安装 Zookeeper
解压下载包
tar zxvf apache-zookeeper-3.8.2-bin.tar.gz
把解压文件夹移动到指定目录
mv apache-zookeeper-3.8.2-bin /opt/zookeeper-3.8.2
注意:3台主机都要安装,也可以在一台上配置好后使用scp命令复制过去。
六、配置 Zookeeper 参数
1、配置zoo.cfg
复制一份新的配置文件 zoo.cfg
cd zookeeper-3.8.2/conf
cp zoo_sample.cfg zoo.cfg
修改zoo.cfg配置项
vi zoo.cfg
# zookeeper 数据目录
dataDir=/var/zookeeper
# 3台服务器的配置
# 这里可以使用IP,也可以使用主机名,但使用主机名时要配置好hosts文件。
server.1=192.168.8.51:2888:3888
server.2=192.168.8.52:2888:3888
server.3=192.168.8.53:2888:3888
可以先配置一台,完成后使用scp命令复制
格式为:scp -r zookeeper-3.8.2/ root@192.168.8.52:pwd 也可以分别在三台主机上配置。 创建 Zookeeper 数据目录
mkdir -p /var/zookeeper
注意
3台主机都要配置,也可以在一台上配置好后使用scp命令复制过去。 这个配置里面每个server后面有一个数字,这个数字是有用的,第二会用到,要注意对应关系。
2、创建 Zookeeper 的ID
这里的值要与zoo.cfg中配置的server一致。 第一台 192.168.8.51 echo 1 > /var/zookeeper/myid
第二台 192.168.8.52 echo 2 > /var/zookeeper/myid
第三台 192.168.8.53 echo 3 > /var/zookeeper/myid
七、配置 Zookeeper 环境变量
将zookeeper添加到环境变量是为了使用方便,但不是必须的。
vi /etc/profile
export ZOOKEEPER_HOME=/opt/zookeeper-3.8.2
export PATH=$PATH:$ZOOKEEPER_HOME/bin
source /etc/profile
八、测试安装结果
1、启动 Zookeeper
zkServer.sh start
相关命令 # 前台启动,前台启动可以直接看到日志信息
zkServer.sh start-foreground
# 重新启动
zkServer.sh restart
注意:3台主机都要启动
2、停止Zookeeper
zkServer.sh stop
3、查看 Zookeeper状态
zkServer.sh status
查看版本使用 zkServer.sh version通过查询状态可以看到其中一台是leader,另外两台是follower,如下图
3、查看 Zookeeper数据
zkCli.sh
输入zkCli.sh回车后会进入Zookeeper系统,在这可以查看Zookeeper数据等信息,如命令“ ls / ”可以查看根目录下的内容,如下图。
九、配置文件
下面是 zoo.cfg 配置文件的全部说明
# The number of milliseconds of each tick
# 主从服务间心跳时间间隔
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# 从节点追随主机点时,主节点对从节点初始延迟等待时间=tickTime * initLimit
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# 主节眯对从节点同步数据超时时间=tickTime * syncLimit
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 数据持久化目录
# dataDir=/tmp/zookeeper
# the port at which the clients will connect
# Zookeeper 服务端口号
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
# Zookeeper 各节点配置信息
#server.1=192.168.8.51:2888:3888
#server.2=192.168.8.52:2888:3888
#server.3=192.168.8.53:2888:3888
十、注意问题
注意配置防火墙
如果不会配置,测试时可以先关闭防火墙:systemctl stop firewalld
推荐链接
发表评论