Zookeeper 之 安装 (linux & docker-compose)

linux系统安装(先啰嗦一下安装步骤,在提供一键脚本):

1.安装jdk并配置环境变量 & 下载zk安装包

wget  https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz

2.下载安装包上传到linux服务中,并解压缩

tar -zxvf zookeeper-3.4.12.tar.gz

3.重命名安装目录

mv zookeeper-3.4.12 zk

4.配置zoo.cfg配置文件

1.修改zk的conf目录下的zoo_simple.cfg,修改完后,重命名为zoo.cfg

  tickTime=2000

  initLimit=10

  syncLimit=5

  dataDir=/usr/zookeeper/zkdata

  clientPort=2181

5.启动zk

在zk的bin目录下,运行zkServer.sh

./bin/zkServer.sh start /usr/zookeeper/conf/zoo.cfg

6.使用jps查看启动是否成功

7.启动客户端连接到zk

./bin/zkCli.sh -server 192.168.0.220:2181

注意:可以通过  ./bin/zkCli.sh help 查看客户端所有可以执行的指令


Docker安装zookeeper

1.获取zk的镜像

docker pull zookeeper:3.4.14

2.启动zk服务

docker run --name zk -p 2181:2181 -d zookeeper:3.4.14

docker-compose 方式启动

version: '3.0'
 
services:
  behelpful-zookeeper:
    image: zookeeper:latest
    container_name: behelpful-zookeeper
    ports:
      - "2181:2181"
    volumes:
      # Zookeeper 配置文件的路径为 /conf
      - ./conf/zoo.cfg:/conf/zoo.cfg
      # Zookeeper 数据存储位置
      - ./data:/data

 


配置参数解释

# The number of milliseconds of each tick  心跳停止的最长时间2s
tickTime=2000
 
# The number of ticks that the initial
# synchronization phase can take
# 初始通信时限,集群中的Fo1lower跟随者服务器与Leader领导者服务器之间初始连接时能容忍的最多心跳数(tickTime的数量),
# 用它来限定集群中的Zookeeper服务器连接到Leader的时限。
 
initLimit=10
 
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# 同步通信时限,集群中Leader与Fo1lower之间的最大响应时间单位,假如响应超过syncLimit*tickTime,Leader认为Fo11wer死掉,从服务器列表中删除Fo1lwer。
syncLimit=5
 
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 数据文件目录+数据持久化路径,主要用于保存Zookeeper中的数据。
dataDir=/home/bigdata/app/zookeeper/data
 
# the port at which the clients will connect
# 客户端连接端口
clientPort=2181
 
# 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
# autopurge.snapRetainCount=500
 
#2888端口:各个zookeeper之间进行数据的同步,3888端口:各个zookeeper之间进行选主。选一个为主,其他为从(单机模式只需要配一台)
#server后面的数字和myid保持一致就行
server.0=hadoop2:2888:3888

Zookeeper中的配置文件zoo.cfg中参数含义解读如下:

1、tickTime =2000:通信心跳数,Zookeeper 服务器与客户端心跳时间,单位毫秒 Zookeeper使用的基本时间,服务器之间或客户端与服务器之间维持心跳的时间间隔, 也就是每个tickTime时间就会发送一个心跳,时间单位为毫秒。 它用于心跳机制,并且设置最小的session超时时间为两倍心跳时间。(session的最小超 时时间是2*tickTime)

2、initLimit =10:LF 初始通信时限 集群中的Follower跟随者服务器与Leader领导者服务器之间初始连接时能容忍的最多心跳数(tickTime的数量),用它来限定集群中的Zookeeper服务器连接到Leader的时限。

3、syncLimit =5:LF 同步通信时限集群中Leader与Follower之间的最大响应时间单位,假如响应超过syncLimit * tickTime,Leader认为Follwer死掉,从服务器列表中删除Follwer。

4、dataDir:数据文件目录+数据持久化路径主要用于保存 Zookeeper 中的数据。

5、clientPort =2181:客户端连接端口监听客户端连接的端口。


集群搭建

1、创建三个dataDir

mkdir zkdata1 zkdata2 zkdata3

2、分别在三个dataDir目录下面myid文件,myid的内容是服务器的id表示  1|2|3

touch ./zkdata1/myid

3、在/conf目录下创建三个zk配置文件,分别为 zoo1.cfg,zoo2.cfg,zoo3.cfg

zoo1.cfg:内容

tickTime=2000

initLimit=10

syncLimit=5

dataDir=/root/zkdata1

clientPort=3001

server.1=10.15.0.5:3002:3003

server.2=10.15.0.5:4002:4003

server.3=10.15.0.5:5002:5003

zoo2.cfg: 内容

tickTime=2000

initLimit=10

syncLimit=5

dataDir=/root/zkdata2

clientPort=4001

server.1=10.15.0.5:3002:3003

server.2=10.15.0.5:4002:4003

server.3=10.15.0.5:5002:5003

zoo3.cfg:内容

tickTime=2000

initLimit=10

syncLimit=5

dataDir=/root/zkdata3

clientPort=5001


server.1=10.15.0.5:3002:3003

server.2=10.15.0.5:4002:4003

server.3=10.15.0.5:5002:5003

server.A=B:C:D

A 是一个数字,表示这个是第几号服务器; 集群模式下配置一个文件 myid,这个文件在 dataDir 目录下,这个文件里面有一个数据就是 A 的值,Zookeeper 启动时读取此文件,拿到里面的数据与 zoo.cfg 里面的配置信息比较从而判断到底是哪个 server。

B 是这个服务器的地址;

C 是这个服务器 Follower 与集群中的 Leader 服务器交换信息的端口;

D 是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的Leader,而这个端口就是用来执行选举时服务器相互通信的端口。

说明:

    1、server.X :x为服务器的唯一标识。对应myid

    2、192.168.0.220:服务器所在的ip地址

    3、3002:数据同步使用的端口号

    4、3003:选举使用的端口号

 

4、分别启动各个zk服务器

第一个服务

./bin/zkServer.sh start /usr/zookeeper/conf/zoo1.cfg

第二个服务

./bin/zkServer.sh start /usr/zookeeper/conf/zoo2.cfg

第三个服务

./bin/zkServer.sh start /usr/zookeeper/conf/zoo3.cfg

5、查看各个zk服务器的角色信息

./bin/zkServer.sh status /usr/zookeeper/conf/zoo1.cfg

6、客户端连接任意zk服务器进行节点操作

./bin/zkCli.sh -server 192.168.0.220:3001

7、停止特定zk服务器

./bin/zkServer.sh stop /usr/zookeeper/conf/zoo1.cfg

 ...

>>>>>>>>>>>>   接下一篇 : https://www.cnblogs.com/Alay/p/15488005.html   <<<<<<<<<<<<<<<<

posted @ 2021-10-29 10:47  Vermeer  阅读(254)  评论(0)    收藏  举报