- 安装前设置
[root@ZKDASH ~]# hostname
ZKDASH.COM
#修改主机名:
[root@ZKDASH ~]#vim /etc/hostname
ZKDASH.COM
[root@ZKDASH local]#vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=ZKDASH
[root@ZKDASH local]# # vim /etc/hosts
127.0.0.1 ZKDASH
192.168.40.151 ZKDASH
# selinux 要 disable
[root@ZKDASH local]# vim /etc/selinux/config
SELINUX=disabled
[root@ZKDASH ~]# yum install -y openssh openssh-clients
检查并卸载OpenJDK & 安裝JDK 1.8
[root@xag200 ~]# rpm -qa|grep -i mysql
[root@xag200 ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@xag200 ~]# yum remove mariadb*
[root@xag200 ~]# rm /etc/my.cnf
[root@xag200 ~]# rpm -qa|grep mariadb
#安装jdk
[root@XAG193 ~]# mkdir /u01/java -p
[root@XAG193 ~]# cd /u01/java/
[root@XAG193 java]# ls
jdk1.8.0_181 jdk-8u181-linux-x64.tar.gz
[root@XAG193 java]# tar -zxvf jdk-8u181-linux-x64.tar.gz
#设置环境变量
[root@XAG193 java]# vim /etc/profile
在profile中添加如下内容:
#set java environment
JAVA_HOME=/u01/java/jdk1.8.0_181
JRE_HOME=/u01/java/jdk1.8.0_181/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH
#让修改生效:
[root@XAG193 java]# source /etc/profile
#输入java -version查看一下jdk版本信息:
[root@xag182 java]# java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
2.设置环境变量
[root@ZKDASH java]# vim /etc/profile
#在profile中添加如下内容:
### Zookeeper ###
ZK_HOME=/usr/local/zookeeper
PATH=$ZK_HOME/bin:$PATH
export ZK_HOME
export PATH
#让修改生效:
[root@localhost java]# source /etc/profile
- 安装Zookeeper
wget http://archive.apache.org/dist/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz
wget http://archive.apache.org/dist/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz
[root@ZKDASH java]# tar -xvf zookeeper-3.4.12.tar.gz
#安装Zookeeper
[root@ZKDASH java]# mv zookeeper-3.4.12 /usr/local/zookeeper
[root@ZKDASH ~]# cd /usr/local
#规划文件及日志的存储位置
[root@ZKDASH ~]#mkdir -p /usr/local/zookeeper/data
[root@ZKDASH ~]#mkdir -p /usr/local/zookeeper/log
- 配置参数文件
#备份Zookeeper参数文件:
[root@ZKDASH ~]#mv /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg
[root@ZKDASH ~]#cp /usr/local/zookeeper/conf/zoo.cfg /usr/local/zookeeper/conf/zoo.cfg_bak
[root@ZKDASH ~]# vim /usr/local/zookeeper/conf/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.
dataDir=/usr/local/zookeeper/data
dataLogDir=/usr/local/zookeeper/log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections. (set to 0 is unlimit the maximum)
maxClientCnxns=0
--------------------------------------------------------------------------
- 编辑防火墙配置文件
[root@ZKDASH ~]#vi /etc/sysconfig/iptables
##添加下面三句话到默认的22端口这条规则的下面
------------------------------------------------------------------------------------------
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2181 -j ACCEPT
-------------------------------------------------------------------------------------------
##最后重启防火墙使配置生效
[root@ZKDASH ~]#systemctl restart iptables.service
##查看防火墙状态
[root@ZKDASH ~]systemctl status iptables.service
- 启动Zookeeper
[root@ZKDASH ~]# /usr/local/zookeeper/bin/zkServer.sh start
##启动后检测Zookeeper状态
[root@ZKDASH ~]#/usr/local/zookeeper/bin/zkServer.sh status
检测Zookeeper的端口:
[root@ZKDASH ~]# netstat -an | grep -E "2181|2888|3888"
- 测试Zookeeper
[root@ZKDASH ~]#/usr/local/zookeeper/bin/zkCli.sh -server 127.0.0.1:2181
[root@ZKDASH ~]# /usr/local/zookeeper/bin/zkCli.sh -server 192.168.40.151:2181
[root@ZKDASH ~]# /usr/local/zookeeper/bin/zkCli.sh -server 192.168.40.152:2181
[root@ZKDASH ~]# /usr/local/zookeeper/bin/zkCli.sh -server 192.168.40.153:2181
#查看版本
[root@mpb53 conf]# echo stat|nc mpb53 2181
stat is not executed because it is not in the whitelist.
解決方法: 修改启动指令 zkServer.sh ,往里面添加 :
ZOOMAIN="-Dzookeeper.4lw.commands.whitelist=* ${ZOOMAIN}"
在如下段落前
-------------------------------------------
if [ "x$SERVER_JVMFLAGS" != "x" ]
then
JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS"
fi
-------------------------------------------
[root@xag182 ~]# echo stat|nc xag182 2181
Zookeeper version: 3.4.14-4c25d480e66aadd371de8bd2fd8da255ac140bcf, built on 03/06/2019 16:18 GMT
Clients:
/192.168.40.182:49382[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/10
Received: 80
Sent: 79
Connections: 1
Outstanding: 0
Zxid: 0x636
Mode: standalone
Node count: 17
8.1 配置Zookeeper服务开机自动启动 --與8.2 二選一
[root@ZKDASH ~]# touch /etc/init.d/zookeeper
[root@ZKDASH ~]# chmod +x /etc/init.d/zookeeper
[root@ZKDASH ~]# vim /etc/init.d/zookeeper
#!/bin/bash
#chkconfig: 2345 10 90
#description: service zookeeper
export JAVA_HOME=/u01/java/jdk1.8.0_181
export ZOO_LOG_DIR=/usr/local/zookeeper/log
ZOOKEEPER_HOME=/usr/local/zookeeper
case "$1" in
start)
${ZOOKEEPER_HOME}/bin/zkServer.sh start
;;
start-foreground)
${ZOOKEEPER_HOME}/bin/zkServer.sh start-foreground
;;
stop)
${ZOOKEEPER_HOME}/bin/zkServer.sh stop
;;
status)
${ZOOKEEPER_HOME}/bin/zkServer.sh status
;;
restart)
${ZOOKEEPER_HOME}/bin/zkServer.sh restart
;;
upgrade)
${ZOOKEEPER_HOME}/bin/zkServer.sh upgrade
;;
print-cmd)
${ZOOKEEPER_HOME}/bin/zkServer.sh print-cmd
;;
*)
echo "requirestart|start-foreground|stop|status|restart|print-cmd"
;;
esac
[root@ZKDASH ~]# chkconfig --del zookeeper
[root@ZKDASH ~]# chkconfig --add zookeeper
8.2 systemctl开机启动zookeeper (推建)--與8.1 二選一
1. 创建zookeeper.service 在/usr/lib/systemd/system目录下创建文件
vim /usr/lib/systemd/system/zookeeper.service
--------------------------------------------------------
[Unit]
Description=zookeeper
After=syslog.target network.target
[Service]
Type=forking
Environment="JAVA_HOME=/u01/java/jdk1.8.0_181"
Environment="ZOO_LOG_DIR=/usr/local/zookeeper/log"
ExecStart=/usr/local/zookeeper/bin/zkServer.sh start
ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop
ExecReload=/usr/local/zookeeper/bin/zkServer.sh restart
LimitNOFILE=65536
LimitNPROC=16384
LimitCORE=infinity
LimitMEMLOCK=infinity
[Install]
WantedBy=multi-user.target
--------------------------------------------------------
设置开机启动
systemctl enable zookeeper.service
通過服務啟動或停止
[root@mpb53 log]# service zookeeper status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone
[root@mpb53 log]# service zookeeper stop
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@mpb53 log]# echo stat|nc mpb53 2181
Ncat: Connection refused.
[root@mpb53 log]# service zookeeper start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@mpb53 log]# echo stat|nc mpb53 2181
Zookeeper version: 3.6.2--803c7f1a12f85978cb049af5e4ef23bd8b688715, built on 09/04/2020 12:44 GMT
Clients:
/10.0.102.53:50854[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0.0/0
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x4
Mode: standalone
Node count: 5
配置文件说明
配置项说明如下:
tickTime: ZooKeeper 中使用的基本时间单元, 以毫秒为单位, 默认值是 2000。它用来调节心跳和超时。
例如, 默认的会话超时时间是两倍的 tickTime。
initLimit: 默认值是 10, 即 tickTime 属性值的 10 倍。它用于配置允许 followers 连接并同步到 leader 的最大时间。
如果 ZooKeeper 管理的数据量很大的话可以增加这个值。
syncLimit: 默认值是 5, 即 tickTime 属性值的 5 倍。它用于配置leader 和 followers 间进行心跳检测的最大延迟时间。
如果在设置的时间内 followers 无法与 leader 进行通信, 那么 followers 将会被丢弃。
dataDir: ZooKeeper 用来存储内存数据库快照的目录, 并且除非指定其它目录,
否则数据库更新的事务日志也将会存储在该目录下。建议配置 dataLogDir 参数来指定 ZooKeeper 事务日志的存储目录。
clientPort: 服务器监听客户端连接的端口, 也即客户端尝试连接的端口, 默认值是 2181。
maxClientCnxns: 在 socket 级别限制单个客户端与单台服务器之前的并发连接数量,
可以通过 IP 地址来区分不同的客户端。它用来防止某种类型的 DoS 攻击,
包括文件描述符耗尽。默认值是 60。将其设置为 0 将完全移除并发连接数的限制。
autopurge.snapRetainCount: 配置 ZooKeeper 在自动清理的时候需要保留的数据文件快照的数量和对应的事务日志文件, 默认值是 3。
autopurge.purgeInterval: 和参数 autopurge.snapRetainCount 配套使用, 用于配置 ZooKeeper 自动清理文件的频率,
默认值是 1, 即默认开启自动清理功能, 设置为 0 则表示禁用自动清理功能。