Zookeeper单机以及集群搭建教程
单机搭建
单机安装以及启动
安装zookeeper的前提是必须有java环境
# 选择目录进行下载安装
cd /app
# 下载zk,可以去官方网站下载,自己上传
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz
# 解压zk
tar -zxvf zookeeper-3.4.11.tar.gz
# 设定软连接
ln -s zookeeper-3.4.11 zookeeper
# 添加两个目录,一个是数据目录,一个是日志目录
cd zookeeper/
mkdir data
mkdir log
# 添加配置文件zoo.cfg在zookeeper的config目录下面见【2】使用:wq! 进行保存
cd conf
cp zoo_sample.cfg zoo.cfg
# 修改 dataDir=/app/zookeeper/data dataLogDir=/app/zookeeper/log 目录
vi zoo.cfg
# 进入bin目录进行启动
cd ../bin
./zkServer.sh start
# 显示如下:Starting zookeeper ... STARTED即为成功
单机配置zoo.cfg的配置的内容,只需要修改dataDir、dataLogDir
# 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=/app/zookeeper/data
dataLogDir=/app/zookeeper/log
# 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
单机常用命令
# 进入zookeeper目录使用命令,连接成功则说明通过
./zkCli.sh
# 启动zk服务
./zkServer.sh start
# 查看zk服务状态
./zkServer.sh status
# 停止zk服务
./zkServer.sh stop
# 重启zk服务
./zkServer.sh restart
集群搭建
虚拟机配置
虚拟机1 | 虚拟机2 | 虚拟机3 |
---|---|---|
172.16.48.129 | 172.16.48.130 | 172.16.48.131 |
myid:1 | myid:2 | myid:3 |
myid配置
# 在每个虚拟机的dataDir=/app/zookeeper/data目录下面创建myid文件
cd /app/zookeeper/data
# 创建myid文件,内容依照表格1,2,3.使用:wq进保存
vim myid
zoo.cfg配置添加
# 其他配置同单机配置
# 在zoo.cfg下面添加如下的集群配置server.myid
# 对应其他实例的内网ip地址
server.1=172.16.48.129:2888:3888
server.2=172.16.48.130:2888:3888
server.3=172.16.48.131:2888:3888
在三台虚拟机的终端同时启动三个zookeeper实例,zk1 -》 zk2 -》 zk3
cd /app/zookeeper/bin
./zkServer.sh start
./zkServer.sh status
# 分别在状态中显示了leader还是follower
虚拟机1 | 虚拟机2 | 虚拟机3 |
---|---|---|
172.16.48.129 | 172.16.48.130 | 172.16.48.131 |
follower | follower | leader |
高可用测试
按照道理zookeeper高可用3台的情况下只要两台挂了,集群就无法提供服务了。
一台挂了的情况: 关闭虚拟机3的zookeeper,调用status命令
虚拟机1 | 虚拟机2 | 虚拟机3 |
---|---|---|
172.16.48.129 | 172.16.48.130 | 172.16.48.131 |
follower | leader | Error contacting service. It is probably not running. |
虚拟机2转为了leader,虚拟机1和2一起提供服务
两台挂了的情况: 关闭虚拟机2的zookeeper,调用status命令
虚拟机1 | 虚拟机2 | 虚拟机3 |
---|---|---|
172.16.48.129 | 172.16.48.130 | 172.16.48.131 |
Error contacting service. It is probably not running. | Error contacting service. It is probably not running. | Error contacting service. It is probably not running. |
重新启动一台后,虚拟机1和虚拟机2能够提供服务