ZooKeeper 服务安装

一、集群主机

启动虚拟机,至少准备两个ip,也可准备三个。

序号 IP 角色
1 192.168.3.73 Master、Slave

二、下载安装

1、下载并安装 ZooKeeper

官方地址:https://downloads.apache.org/zookeeper

所有的主机,下载安装最新稳定版 ZooKeeper (当前最新稳定版本为:3.9.2).

$ cd /usr/local/src && wget https://downloads.apache.org/zookeeper/zookeeper-3.9.2/apache-zookeeper-3.9.2-bin.tar.gz --no-check-certificate
$ tar zxf apache-zookeeper-3.9.2-bin.tar.gz -C /usr/local/
$ cd .. && mv apache-zookeeper-3.9.2-bin/ zookeeper
$ echo 'export ZOOKEEPER_HOME="/usr/local/zookeeper"' >>/etc/profile
$ echo 'export PATH="$ZOOKEEPER_HOME/bin:$PATH"' >>/etc/profile
$ source /etc/profile

2、修改配置文件

首先查看下 /usr/local/zookeeper/conf/ 目录下的 zoo_sample.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=/home/zookeeper/data
dataLogDir=/home/zookeeper/logs
# 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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

# Disable the adminserver by default to avoid port conflicts.
# Set the port to something non-conflicting if choosing to enable this
admin.enableServer=false
#dmin.serverPort=8080
  • tickTime:CS通信心跳时间,客户端与服务器或者服务器与服务器之间维持心跳。单位是毫秒,系统默认是2000毫秒,也就是间隔两秒心跳一次。

  • initLimit:集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数。

  • syncLimit: Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 2*2000=4 秒。

  • server.A=B:C:D :A 是一个数字,表示这个是第几号服务器; B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader。

另外,可以通过其他 4 个参数对 ZooKeeper 的数据文件生成和清理进行配置。

  • autopurge.snapRetainCount : (No Java system property) New in 3.4.0
  • autopurge.purgeInterval : (No Java system property) New in 3.4.0

  • snapCount : (Java system property: zookeeper.snapCount)

    每进行snapCount次事务日志输出后,触发一次快照(snapshot), 此时,ZK会生成一个snapshot.文件,同时创建一个新的事务日志文件log.。默认是100000.(真正的代码实现中,会进行一定的随机数处理,以避免所有服务器在同一时间进行快照而影响性能)(Java system property:zookeeper.snapCount )

  • snapSizeLimitInKb : (Java system property: zookeeper.snapSizeLimitInKb)

下面对 tickTime、dataDir、dataLogDir、clientPort、initLimit=5、syncLimit 等进行基本配置。

注意:在每台主机上都执行以下命令 !

$ cd zookeeper/conf && cp -p zoo_sample.cfg zoo.cfg
$ mkdir -p /home/zookeeper/data
$ mkdir -p /home/zookeeper/logs
$ cat > zoo.cfg<<'EOF'
dataDir=/home/zookeeper/data
dataLogDir=/home/zookeeper/logs
tickTime=2000
initLimit=5
syncLimit=2
autopurge.snapRetainCount=3
autopurge.purgeInterval=0
maxClientCnxns=1024
standaloneEnabled=true
admin.enableServer=true
dmin.serverPort=8080
server.1=192.168.3.99:2888:3888
clientPort=2181
EOF

在 server.1 上执行:

$ echo 1 > /home/zookeeper/data/myid

配置完成后,切记一定要放开云服务器安全策略的 2181,2888,3888 几个端口,主机还需要关闭防火墙(一开始没关闭没成功);执行以下命令,实时看到启动的情况:

/usr/local/zookeeper/bin/zkServer.sh start-foreground

可用命令:

zkServer.sh start 启动zk服务
zkServer.sh stop 停止zk服务
zkServer.sh restart 重启zk服务
zkServer.sh status 查看zk服务状态

启动后浏览器也可以访问查看状态:http://ip:8080/commands; 我这里是 :http://192.168.1.170:8080/commands

3、开机启动

[root@nacos nacos]# vi /lib/systemd/system/zookeeper.service

脚本内容:

[Unit]
Description=Zookeeper 3.x
After=network.target

[Service]
User=zookeeper
Type=forking
Environment='ZOO_LOG_DIR=/home/zookeeper/logs'
Environment='JAVA_HOME=/usr/local/java'
ExecStart=/usr/local/zookeeper/bin/zkServer.sh start
ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop
ExecReload=/usr/local/zookeeper/bin/zkServer.sh restart
Restart=on-failure
StartLimitInterval=60
StartLimitBurst=3000

[Install]
WantedBy=multi-user.target

创建 zookeeper:zookeeper 用户组和用户,并进行授权:

groupadd zookeeper;
useradd -g zookeeper -M -s /sbin/nologin zookeeper;
chown -R zookeeper. /home/zookeeper/data;
chown -R zookeeper. /home/zookeeper/logs;
chown -R zookeeper. /usr/local/zookeeper;

设置随机启动:

[root@nacos nacos]# systemctl daemon-reload
[root@nacos nacos]# systemctl enable zookeeper.service
[root@nacos nacos]# systemctl start zookeeper.service
[root@nacos nacos]# systemctl status zookeeper.service

查询主节点状态:

$ /usr/local/zookeeper/bin/zkServer.sh status
作者:Jeebiz  创建时间:2023-01-26 17:35
最后编辑:Jeebiz  更新时间:2024-11-01 10:06