1、单机部署ZooKeeper
1-1、配置 Java 环境
[root@ubuntu2004 ~]#apt install -y openjdk-8-jdk
1-2、下载zookeeper
#官方下载:
[root@ubuntu2004 ~]#wget https://dlcdn.apache.org/zookeeper/zookeeper-3.7.1/apache-zookeeper-3.7.1-bin.tar.gz
#国内清华源下载(建议):
[root@ubuntu2004 ~]#wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.7.1/apache-zookeeper-3.7.1-bin.tar.gz
1-3、解压并修改配置
[root@ubuntu2004 ~]#tar xf apache-zookeeper-3.7.1-bin.tar.gz -C /usr/local/
[root@ubuntu2004 ~]#cd /usr/local/
[root@ubuntu2004 local]#ln -s apache-zookeeper-3.7.1-bin /usr/local/zookeeper
[root@ubuntu2004 local]#echo 'PATH=/usr/local/zookeeper/bin:$PATH' > /etc/profile.d/zookeeper.sh
[root@ubuntu2004 local]#. /etc/profile.d/zookeeper.sh
#修改配置:
[root@ubuntu2004 local]#cp /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg
[root@ubuntu2004 local]#vim /usr/local/zookeeper/conf/zoo.cfg
tickTime=2000 #"滴答时间",用于配置Zookeeper中最小的时间单元长度,单位毫秒,是其它时间配置的基础
initLimit=10 #初始化时间,包含启动和数据同步,其值是tickTime的倍数
syncLimit=5 #正常工作,心跳监测的时间间隔,其值是tickTime的倍数
dataDir=/usr/local/zookeeper/data #配置Zookeeper服务存储数据的目录,基于安全,可以修改为服务所在目录下
dataLogdir=/usr/local/zookeeper/logs #可以指定日志路径
clientPort=2181 #配置当前Zookeeper服务对外暴露的端口,用户客户端和服务端建立连接会话
autopurge.snapRetainCount=3 #启用后,ZooKeeper 自动清除功能,会将只保留此最新3个快照和相应的事务日志,并分别保留在dataDir 和dataLogDir中,删除其余部分,默认值为3,最小值为3
autopurge.purgeInterval=24 #3.4.0及之后版本,ZK提供了自动清理日志和快照文件的功能,这个参数指定了清理频率,单位是小时,需要配置一个1或更大的整数,默认是 0,表示不开启自动清理功能
1-4、启动zookeeper
1-4-1、启动方式一:
#前台启动
[root@ubuntu2004 local]#zkServer.sh start-foreground
##后台启动
[root@ubuntu2004 local]#zkServer.sh start
[root@ubuntu2004 local]#zkServer.sh status
/usr/bin/java
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
#注意:如果配置service,zkServer.sh和systemctl不要混用,否则无法启动
1-4-2、启动方式二:
#配置service文件:
[root@ubuntu2004 local]#vim /usr/lib/systemd/system/zookeeper.service
[Unit]
Description=zookeeper.service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/zookeeper/bin/zkServer.sh start
ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop
ExecReload=/usr/local/zookeeper/bin/zkServer.sh restart
[Install]
WantedBy=multi-user.target
[root@ubuntu2004 local]#systemctl daemon-reload
[root@ubuntu2004 local]#systemctl start zookeeper.service
2、部署ZooKeeper 集群
2-1、环境准备
主机名 | IP | 系统版本 |
zookeeper.node1 | 10.0.0.101 | ubuntu2004 |
zookeeper.node2 | 10.0.0.102 | ubuntu2004 |
zookeeper.node3 | 10.0.0.103 | ubuntu2004 |
2-2、安装openjdk
#所有节点执行:
[root@zookeeper-node1 ~]#apt install -y openjdk-8-jdk
2-3、下载zookeeper并解压
#所有节点执行:
[root@zookeeper-node1 ~]#wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.7.1/apache-zookeeper-3.7.1-bin.tar.gz
[root@zookeeper-node1 ~]#tar xf apache-zookeeper-3.7.1-bin.tar.gz -C /usr/local/
[root@zookeeper-node1 ~]#cd /usr/local
[root@zookeeper-node1 local]#ln -s apache-zookeeper-3.7.1-bin zookeeper
[root@zookeeper-node1 local]#echo 'PATH=/usr/local/zookeeper/bin:$PATH' > /etc/profile.d/zookeeper.sh
[root@zookeeper-node1 local]#. /etc/profile.d/zookeeper.sh
#所有节点执行:
[root@zookeeper-node1 local]#cp /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg
[root@zookeeper-node1 local]#vim /usr/local/zookeeper/conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper/data
dataLogdir=/usr/local/zookeeper/logs
clientPort=2181
maxClientCnxns=128 #单个客户端IP 可以和zookeeper保持的连接数
autopurge.snapRetainCount=3
autopurge.purgeInterval=24
server.101=10.0.0.101:2888:3888
server.102=10.0.0.102:2888:3888
server.103=10.0.0.103:2888:3888
[root@zookeeper-node1 local]#mkdir /usr/local/zookeeper/data
================
#格式说明:
# server.服务器唯一编号=服务器IP:Leader和Follower的数据同步端口(只有leader才会打开):Leader和Follower选举端口(L和F都有)
server.101=10.0.0.101:2888:3888
server.102=10.0.0.102:2888:3888
server.103=10.0.0.103:2888:3888
#如果添加节点,只需要在所有节点上添加新节点的上面形式的配置行,在新节点创建myid文件,并重启所有节点服务即可
#偷个懒,把配置拷贝到其他节点:
[root@zookeeper-node1 local]#scp /usr/local/zookeeper/conf/zoo.cfg 10.0.0.102:/usr/local/zookeeper/conf/zoo.cfg
[root@zookeeper-node1 local]#scp /usr/local/zookeeper/conf/zoo.cfg 10.0.0.103:/usr/local/zookeeper/conf/zoo.cfg
2-4、在各个节点生成ID文件
#各节点分别执行:
[root@zookeeper-node1 local]#echo 101 > /usr/local/zookeeper/data/myid
[root@zookeeper-node2 local]#echo 102 > /usr/local/zookeeper/data/myid
[root@zookeeper-node3 local]#echo 103 > /usr/local/zookeeper/data/myid
注意: 各个myid文件的内容要和zoo.cfg文件相匹配
2-5、生成service文件并启动
#所有节点执行:
[root@zookeeper-node1 ~]#vim /usr/lib/systemd/system/zookeeper.service
[Unit]
Description=zookeeper.service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/zookeeper/bin/zkServer.sh start
ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop
ExecReload=/usr/local/zookeeper/bin/zkServer.sh restart
[Install]
WantedBy=multi-user.target
[root@zookeeper-node1 ~]#systemctl daemon-reload
[root@zookeeper-node1 ~]#systemctl restart zookeeper.service
[root@zookeeper-node1 ~]#systemctl status zookeeper.service