操作系统:openEuler
JDK:21
主机名 | IP地址 |
---|---|
spark01 | 192.168.171.101 |
spark02 | 192.168.171.102 |
spark03 | 192.168.171.103 |
yum -y update
yum -y install gcc gcc-c++ autoconf automake cmake make \
zlib zlib-devel openssl openssl-devel pcre-devel \
rsync openssh-server vim man zip unzip net-tools tcpdump lrzsz tar wget
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
hostnamectl set-hostname spark01
hostnamectl set-hostname spark02
hostnamectl set-hostname spark03
vim /etc/sysconfig/network-scripts/ifcfg-ens160
参考如下:
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=ens160
UUID=22d234d4-df21-45ca-a078-bb280978fbce
DEVICE=ens160
ONBOOT=yes
IPADDR=192.168.171.101
PREFIX=24
GATEWAY=192.168.171.2
DNS1=192.168.171.2
vim /etc/hosts
修改内容如下:
192.168.171.101 spark01
192.168.171.102 spark02
192.168.171.103 spark03
reboot
在所有主机节点创建软件目录
mkdir -p /opt/soft
以下操作在 spark01 主机上完成
进入软件目录
cd /opt/soft
下载JDK
wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
下载 Zookeeper
wget https://dlcdn.apache.org/zookeeper/zookeeper-3.9.1/apache-zookeeper-3.9.1-bin.tar.gz
解压 JDK 修改名称
解压 zookeeper 修改名称
tar -zxvf jdk-21_linux-x64_bin.tar.gz
mv jdk-21.0.1 jdk-21
tar -zxvf apache-zookeeper-3.9.1-bin.tar.gz
mv apache-zookeeper-3.9.1-bin zookeeper-3
配置环境变量
vim /etc/profile.d/my_env.sh
编写以下内容:
export JAVA_HOME=/opt/soft/jdk-21
export set JAVA_OPTS="--add-opens java.base/java.lang=ALL-UNNAMED"
export ZOOKEEPER_HOME=/opt/soft/zookeeper-3
export PATH=$PATH:$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin
在各服务器上使环境变量生效
source /etc/profile
cd $ZOOKEEPER_HOME/conf
vim zoo.cfg
# 心跳单位,2s
tickTime=2000
# zookeeper-3初始化的同步超时时间,10个心跳单位,也即20s
initLimit=10
# 普通同步:发送一个请求并得到响应的超时时间,5个心跳单位也即10s
syncLimit=5
# 内存快照数据的存储位置
dataDir=/home/zookeeper-3/data
# 事务日志的存储位置
dataLogDir=/home/zookeeper-3/datalog
# 当前zookeeper-3节点的端口
clientPort=2181
# 单个客户端到集群中单个节点的并发连接数,通过ip判断是否同一个客户端,默认60
maxClientCnxns=1000
# 保留7个内存快照文件在dataDir中,默认保留3个
autopurge.snapRetainCount=7
# 清除快照的定时任务,默认1小时,如果设置为0,标识关闭清除任务
autopurge.purgeInterval=1
#允许客户端连接设置的最小超时时间,默认2个心跳单位
minSessionTimeout=4000
#允许客户端连接设置的最大超时时间,默认是20个心跳单位,也即40s,
maxSessionTimeout=300000
#zookeeper-3 3.5.5启动默认会把AdminService服务启动,这个服务默认是8080端口
admin.serverPort=9001
#集群地址配置
server.1=spark01:2888:3888
server.2=spark02:2888:3888
server.3=spark03:2888:3888
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/zookeeper-3/data
dataLogDir=/home/zookeeper-3/datalog
clientPort=2181
maxClientCnxns=1000
autopurge.snapRetainCount=7
autopurge.purgeInterval=1
minSessionTimeout=4000
maxSessionTimeout=300000
admin.serverPort=9001
server.1=spark01:2888:3888
server.2=spark02:2888:3888
server.3=spark03:2888:3888
在每台服务器上执行
mkdir -p /home/zookeeper-3/data
mkdir -p /home/zookeeper-3/datalog
在/etc/systemd/system/文件夹下创建一个启动脚本zookeeper-3.service
注意:在每台服务器上编写
cd /etc/systemd/system
vim zookeeper.service
内容如下:
[Unit]
Description=zookeeper
After=syslog.target network.target
[Service]
Type=forking
# 指定zookeeper-3 日志文件路径,也可以在zkServer.sh 中定义
Environment=ZOO_LOG_DIR=/home/zookeeper-3/datalog
# 指定JDK路径,也可以在zkServer.sh 中定义
Environment=JAVA_HOME=/opt/soft/jdk-21
ExecStart=/opt/soft/zookeeper-3/bin/zkServer.sh start
ExecStop=/opt/soft/zookeeper-3/bin/zkServer.sh stop
Restart=always
User=root
Group=root
[Install]
WantedBy=multi-user.target
[Unit]
Description=zookeeper
After=syslog.target network.target
[Service]
Type=forking
Environment=ZOO_LOG_DIR=/home/zookeeper-3/datalog
Environment=JAVA_HOME=/opt/soft/jdk-21
ExecStart=/opt/soft/zookeeper-3/bin/zkServer.sh start
ExecStop=/opt/soft/zookeeper-3/bin/zkServer.sh stop
Restart=always
User=root
Group=root
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
# 等所有主机配置好后再执行以下命令
systemctl start zookeeper
systemctl enable zookeeper
systemctl status zookeeper
创建本地秘钥并将公共秘钥写入认证文件
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
ssh-copy-id root@spark01
ssh-copy-id root@spark02
ssh-copy-id root@spark03
分发 ssh 免密钥
scp -r ~/.ssh root@spark02:~/
scp -r ~/.ssh root@spark03:~/
分发 hosts 文件
scp -r /etc/hosts root@spark02:/etc/
scp -r /etc/hosts root@spark03:/etc/
分发软件
scp -r /opt/soft/jdk-21 root@spark02:/opt/soft
scp -r /opt/soft/zookeeper-3 root@spark02:/opt/soft
scp -r /opt/soft/jdk-21 root@spark03:/opt/soft
scp -r /opt/soft/zookeeper-3 root@spark03:/opt/soft
分发环境变量
scp /etc/profile.d/my_env.sh root@spark02:/etc/profile.d/
scp /etc/profile.d/my_env.sh root@spark03:/etc/profile.d/
在所有主机节点 使新的环境变量生效
source /etc/profile
spark01
echo 1 > /home/zookeeper-3/data/myid
more /home/zookeeper-3/data/myid
spark02
echo 2 > /home/zookeeper-3/data/myid
more /home/zookeeper-3/data/myid
spark03
echo 3 > /home/zookeeper-3/data/myid
more /home/zookeeper-3/data/myid
在各节点执行以下命令
systemctl daemon-reload
systemctl start zookeeper
systemctl enable zookeeper
systemctl status zookeeper
jps
zkServer.sh status
浏览器访问:http://spark03:9001/commands
zookeeper 存储结构类似于Linux文件系统 使用根结构
node 不是文件也不是目录
# 连接本地服务
zkCli.sh
# 连接其他节点
zkCli.sh -server spark02:2181
# 这里并不是连接了三个节点,而是按照顺序连接一个,当第一个连接无法获取时,就连接第二个
zkCli.sh -server spark01:2181,spark02:2181,spark03:2181
help
ZooKeeper -server host:port -client-configuration properties-file cmd args
addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
addauth scheme auth
close
config [-c] [-w] [-s]
connect host:port
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
delete [-v version] path
deleteall path [-b batch size]
delquota [-n|-b|-N|-B] path
get [-s] [-w] path
getAcl [-s] path
getAllChildrenNumber path
getEphemerals path
history
listquota path
ls [-s] [-w] [-R] path
printwatches on|off
quit
reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
redo cmdno
removewatches path [-c|-d|-a] [-l]
set [-s] [-v version] path data
setAcl [-s] [-v version] [-R] path acl
setquota -n|-b|-N|-B val path
stat [-w] path
sync path
version
whoami
# 在某节点连接其他节点
connect spark01:2181
quit
ls /
ls -s /
cZxid = 0x0 # 节点创建的时候的zxid
ctime = Thu Jan 01 08:00:00 CST 1970 # 节点创建的时候的zxid
mZxid = 0x0 #节点修改的时候的zxid,与子节点的修改无关
mtime = Thu Jan 01 08:00:00 CST 1970 # 节点修改的时间
pZxid = 0x0 # 子节点的创建/删除对应的 zxid,和修改无关,和孙子节点无关
cversion = -1 # 子节点的更新次数
dataVersion = 0 # 节点数据的更新次数
aclVersion = 0 # 节点(ACL)的更新次数
ephemeralOwner = 0x0 # 值表示与该节点绑定的 session id. 如果该节点不是 ephemeral 节点, ephemeralOwner 值为0
dataLength = 0 # 节点数据的字节数
numChildren = 1 # 节点数据的字节数
持久
临时
有序号
无序号
# 持久无序号
# 节点lihaozhe存储的数据是 李昊哲
create /lihaozhe "李昊哲"
# 节点lihaozhe存储的数据是 空
create /lihaozhe ""
# 查看该节点中的文件或目录,[]代表其中没有文件或子目录
[zk: localhost:2181(CONNECTED) 7] ls /lihaozhe
[]
# 创建持久无序号子节点
create /lihaozhe/zk01
# 创建持久有序号子节点
create -s /lihaozhe/zk01
# 创建临时无序号节点
create -e /lihaozhe/zk10
# 创建临时有序号节点
create -e -s /lihaozhe/zk10
delete /lihaozhe/zk04
delete /lihaozhe/zk01 # Node not empty: /lihaozhe/zk01
deleteall /lihaozhe
get /lihaozhe
set /lihaozhe "李大宝"
close
# NodeChildrenChanged
ls -w /lihaozhe
# NodeDataChanged
stat -w /lihaozhe
#
haozhe
[]
create /lihaozhe/zk01
create -s /lihaozhe/zk01
create -e /lihaozhe/zk10
create -e -s /lihaozhe/zk10
### 删除节点
```bash
delete /lihaozhe/zk04
delete /lihaozhe/zk01 # Node not empty: /lihaozhe/zk01
deleteall /lihaozhe
get /lihaozhe
set /lihaozhe "李大宝"
close
# NodeChildrenChanged
ls -w /lihaozhe
# NodeDataChanged
stat -w /lihaozhe
#