目录
1.环境配置
Required Software
ZooKeeper runs in Java, release 1.7 or greater (JDK 7 or greater, FreeBSD support requires openjdk7). It runs as an ensemble of ZooKeeper servers. Three ZooKeeper servers is the minimum recommended size for an ensemble, and we also recommend that they run on separate machines. At Yahoo!, ZooKeeper is usually deployed on dedicated RHEL boxes, with dual-core processors, 2GB of RAM, and 80GB IDE hard drives.
这里安装 ZooKeeper3.5.9 ,需要 JDK1.7以上,这里配置JDK1.8,下载地址https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
(1)下载解压
#创建目录
mkdir -p /usr/java
#下载并解压
tar -zxvf jdk-8u212-linux-x64.tar.gz
(2)配置环境变量
打开 /etc/profile 文件,在末尾添加下面内容
export JAVA_HOME=/usr/java/jdk1.8.0_212
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
保存,运行命令
source /etc/profile
(3)测试java环境
java -version
2.安装 ZooKeeper
https://zookeeper.apache.org/releases.html#download
(1)下载解压
wget https://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz
tar -zxvf apache-zookeeper-3.5.9-bin.tar.gz
(2)修改配置文件
cd apache-zookeeper-3.5.9-bin/
cd conf/
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
按照 server.id = ip:port:port ,在zoo.cfg 文件结尾添加配置:
server.1=192.168.11.29:2888:3888
server.2=192.168.11.30:2888:3888
server.3=192.168.11.31:2888:3888
配置完成的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=/var/lib/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
server.1=192.168.111.129:2888:3888
server.2=192.168.111.130:2888:3888
server.3=192.168.111.131:2888:3888
将该配置文件复制到其他两台机器对应目录下,内容保持不变。
注意:dataDir= 参数 配置的目录应该为空目录,不能复用有数据的目录,否则会导致zookeeper启动失败。
(3) 配置 myid
根据 server.id = ip:port:port 中, id 和 ip 的对应关系,分别在 三台节点机器的 dataDir=/var/lib/zookeeper/ 指定的目录下, 新建 myid 文件并且填入对应的数字。
vim /var/lib/zookeeper/myid
节点1: ip 为192.168.111.129 机器为
节点2: ip 为192.168.111.130 机器为
节点3: ip 为192.168.111.131 机器为
注意:myid 文件只能填数字(具体要求如下引用),除此之外不能添加其他内容,空格也不能有,否则会报错(如下截图)。
The myid file consists of a single line containing only the text of that machine's id. So myid of server 1 would contain the text "1" and nothing else. The id must be unique within the ensemble and should have a value between 1 and 255. IMPORTANT: if you enable extended features such as TTL Nodes (see below) the id must be between 1 and 254 due to internal limitations.
3.运行测试
(1)关闭防火墙
首先必须关闭每台机器的防火墙,否则会导致zookeeper启动失败
systemctl stop firewalld
(2)启动服务
在第一台机器启动zookeeper 服务
cd apache-zookeeper-3.5.9-bin/
./bin/zkServer.sh start
查看启动状态
./bin/zkServer.sh status
发现并没有启动成功,查看系统日志。
cd apache-zookeeper-3.5.9-bin/
tail -f ./logs/zookeeper-root-server-node1.out
发现是连接不到另外两台机器,分别启动另外两台机器上面的zookeeper服务。
(3)查看程序状态
节点1:
节点2:
节点3:
说明节点2是主节点,节点1和节点3是从节点。
(4)测试
bin/zkCli.sh -server 192.168.111.129:2181,192.168.111.130:2181,192.168.111.131:2181
安装成功。
参考:
1.jdk 下载
https://www.oracle.com/java/technologies/javase-downloads.html
2.zookeeper官网
https://zookeeper.apache.org/index.html
3.zookeeper下载
https://zookeeper.apache.org/releases.html#download
4.系统需求
https://zookeeper.apache.org/doc/r3.5.9/zookeeperAdmin.html#sc_systemReq
5.单机安装教程
https://zookeeper.apache.org/doc/r3.5.9/zookeeperStarted.html#sc_InstallingSingleMode
6.集群安装教程
https://zookeeper.apache.org/doc/r3.5.9/zookeeperStarted.html
https://zookeeper.apache.org/doc/r3.5.9/zookeeperStarted.html#sc_RunningReplicatedZooKeeper
https://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_zkMulitServerSetup
7.配置文件参数说明
https://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_configuration
8.其他