Zookeeper相关笔记(1)介绍,简单安装与简单集群部署

Zookeeper相关笔记(1)介绍,简单安装与简单集群部署

1.介绍

image

Zookeeper是一个分布式服务的协调服务,内部文件结构采用树形结构。

建立Zookeeper集群时,这些服务会公共选出一个leader,然后其他的服务成为这个leader服务的镜像服务,称为follower,所有的操作都由leader完成。当leader服务故障消失时,剩下的follower服务会投票选出下一个leader,然后所有其他服务会成为新leader的follower,当故障服务重新连接入集群,他将会成为新leader的follower。

2.安装

准备三个以上的linux节点,确保每一个都安装并配置了jdk1.8或以上的版本(推荐使用Oracle的jdk,不要使用linux自带的open JDK),并且能够从其中任意一台ping通另两台,关闭防火墙或者打开端口2888和3888。

官网下载:

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

进入下载页面,选择二进制的版本下载(不带Source Release的版本)(源码版本设计很复杂的编译问题)这里我选择3.5.9

Apache ZooKeeper 3.5.9(asc, sha512)

点击进入下载页面,在linux上直接下载:

# wget https://dlcdn.apache.org/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz

解压:

# tar -zxvf apache-zookeeper-3.5.9-bin.tar.gz

完成后会有一个文件目录,将他移动到/opt/(随便取个名)下:

# mv apache-zookeeper-3.5.9-bin /etc/balabala/

打开目录:

# cd /etc/balabala/apache-zookeeper-3.5.9-bin/conf

复制zoo_sample.cfg => zoo.cfg

# cp zoo_sample.cfg zoo.cfg

打开并修改:

# vi 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=/tmp/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.
#
# http://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

修改dataDir=[你想放zookeeper文件的目录]

在最后添加:

server.1=192.168.xxx.xxx:2888:3888
server.2=192.168.xxx.xxx:2888:3888
server.3=192.168.xxx.xxx:2888:3888
...									#你有几个节点就添加几条,每一条你准备的节点的网络ip:2888:3888

然后在刚才修改的dataDir中加入myid文件

直接vi新建或者echo都可以,在第一行顶头写个x(参照上面配置中写的server.x,这台机器是哪个ip就写server.x中的x)

完成后修改profile文件,在最末尾添加上环境变量:

export ZOOKEEPER_HOME=/opt/balabala/apache-zookeeper-3.5.9-bin

然后添加PATH变量:

PATH=$PATH:$ZOOKEEPER_HOME/bin

保存退出

重新加载profile

# source /etc/profile

如果在之前已经安装过zookeeper且已经配置过环境变量,切记把以前安装的目录移走或删除,否则无法加载新安装的zookeeper。

完成后检查,在命令行输入:

# zk

然后Tab键:如果出现可执行命令,则说明成功。

可以试着启动(阻塞模式):

# zkServer.sh start-foreground

完成

3.集群部署

将其他的节点机也如上述方式安装及配置,启动服务,zookeeper就会自动建立集群。

查看节点状态:此时可以新开一个窗口,链接任意一台:

# zkServer.sh status

将会显示是leader还是follower。

完成

posted @ 2021-09-08 16:37  飞杨煎生物  阅读(65)  评论(0)    收藏  举报