zookeeper
一、部署zookeeper单点
1.下载zookeeper软件
https://zookeeper.apache.org/releases.html
[[email protected] ~]# ll
-rw-r--r-- 1 root root 13185104 Apr 10 16:09 apache-zookeeper-3.8.0-bin.tar.gz
2. 解压软件包
[[email protected] ~]# tar xf apache-zookeeper-3.8.0-bin.tar.gz -C /es/softwares/
3. 创建符号连接
[[email protected] ~]# cd /es/softwares/ && ln -sv apache-zookeeper-3.8.0-bin zk
‘zk’ -> ‘apache-zookeeper-3.8.0-bin’
4. 声明zk的环境变量
cat > /etc/profile.d/kafka.sh <<'EOF'
#!/bin/bash
export ZK_HOME=/es/softwares/zk
export PATH=$PATH:$ZK_HOME/bin
EOF
[[email protected] /es/softwares]# cat /etc/profile.d/kafka.sh
#!/bin/bash
export ZK_HOME=/es/softwares/zk
export PATH=$PATH:$ZK_HOME/bin
[[email protected] /es/softwares]# source /etc/profile.d/kafka.sh
5. 创建zk的配置文件
[[email protected] /es/softwares]# cp /es/softwares/zk/conf/{zoo_sample.cfg,zoo.cfg}
6. 启动zk节点
#启动zk
[[email protected] /es/softwares]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /es/softwares/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
#查看zk状态
[[email protected] /es/softwares]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /es/softwares/zk/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone
[[email protected] /es/softwares]# zkServer.sh stop
[[email protected] /es/softwares]# zkServer.sh restart
7. 连接ZK节点
[[email protected] ~]# zkCli.sh
Connecting to localhost:2181
....
....
....
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
二、zookeeper的命令行基本管理
1.查看
- 查看
ls /
查看根(/)下有多少子zookeeper node,简称"znode"
get /linux-zk/test
查看"/linux-zk/test"的数据。
[zk: localhost:2181(CONNECTED) 3] get /linux-zk/test
123
2.创建
- 创建
create /linux-zk
在根路径下创建一个名为"linux-zk"的"znode"。
create /linux-zk/test 123
在"/linux-zk/"znode下创建一个名为"test"的子znode,并指定该znode数据为"123"。
create -s /linux-zk/test/lianxi 88888
创建一个前缀为"/linux-zk/test/lianxi"有序编号的znode,数据为88888
[zk: localhost:2181(CONNECTED) 4] create -s /linux-zk/test/lianxi 88888
Created /linux-zk/test/lianxi0000000000
create -s -e /linux-zk/test/lianxi01
创建一个名为"/linux-zk/test/lianxi01"的临时znode。当前会话结束,临时znode会自动删除。
[zk: localhost:2181(CONNECTED) 5] create -s -e /linux-zk/test/lianxi01
Created /linux-zk/test/lianxi010000000001
[zk: localhost:2181(CONNECTED) 7] stat /linux-zk/test/lianxi010000000001
cZxid = 0x5
ctime = Mon Apr 10 20:55:47 CST 2023
mZxid = 0x5
mtime = Mon Apr 10 20:55:47 CST 2023
pZxid = 0x5
cversion = 0
dataVersion = 0
aclVersion = 0
#临时znode的这个字段后边会接上当前的sessionid,不是临时的是0x0
ephemeralOwner = 0x100003d87730000
dataLength = 0
numChildren = 0
3.修改
- 修改
set /linux-zk/test 456
将"/linux-zk/test "的znode数据修改为456.
[zk: localhost:2181(CONNECTED) 8] set /linux-zk/test 456
[zk: localhost:2181(CONNECTED) 9] get /linux-zk/test
456
4.删除
- 删除
delete /linux-zk/test02
删除名为"/linux-zk/test02 "的znode,但该znode不能有子znode。即必须为空。
[zk: localhost:2181(CONNECTED) 11] create /linux-zk/test02
[zk: localhost:2181(CONNECTED) 13] ls /linux-zk
[test, test02]
[zk: localhost:2181(CONNECTED) 14] delete /linux-zk/test02
[zk: localhost:2181(CONNECTED) 15] ls /linux-zk
[test]
deleteall /linux-zk/test
递归删除"/linux-zk/test"下的所有znode。
[zk: localhost:2181(CONNECTED) 16] delete /linux-zk/test
Node not empty: /linux-zk/test
[zk: localhost:2181(CONNECTED) 17] deleteall /linux-zk/test
[zk: localhost:2181(CONNECTED) 18] ls /linux-zk
[]
三、zookeeper集群部署
1. 创建zk的数据目录
[[email protected] ~]# install -d /es/data/zk
2. 修改单点zk的配置文件
[[email protected] ~]# cat /es/softwares/zk/conf/zoo.cfg
# 定义最小单元的时间范围tick。
tickTime=2000
# 启动时最长等待tick数量。
initLimit=5
# 数据同步时最长等待的tick时间进行响应ACK
syncLimit=2
# 指定数据目录
dataDir=/es/data/zk
# 监听端口
clientPort=2181
# 开启四字命令允许所有的节点访问。
4lw.commands.whitelist=*
# server.ID=A:B:C[:D]
# ID:
# zk的唯一编号。
# A:
# zk的主机地址。
# B:
# leader的选举端口,是谁leader角色,就会监听该端口。
# C:
# 数据通信端口。
# D:
# 可选配置,指定角色。
server.101=10.0.0.101:2888:3888
server.102=10.0.0.102:2888:3888
server.103=10.0.0.103:2888:3888
3.同步数据即可
[[email protected] ~]# data_rsync.sh /es/softwares/zk/
===== rsyncing elk102.com: zk =====
命令执行成功!
===== rsyncing elk103.com: zk =====
命令执行成功!
[[email protected] ~]# data_rsync.sh /es/softwares/apache-zookeeper-3.8.0-bin/
===== rsyncing elk102.com: apache-zookeeper-3.8.0-bin =====
命令执行成功!
===== rsyncing elk103.com: apache-zookeeper-3.8.0-bin =====
命令执行成功!
[[email protected] ~]# data_rsync.sh /es/data/zk/
===== rsyncing elk102.com: zk =====
命令执行成功!
===== rsyncing elk103.com: zk =====
命令执行成功!
[[email protected] ~]# data_rsync.sh /etc/profile.d/kafka.sh
===== rsyncing elk102.com: kafka.sh =====
命令执行成功!
===== rsyncing elk103.com: kafka.sh =====
命令执行成功!
4. 创建myid文件
[[email protected] ~]# for ((host_id=101;host_id<=103;host_id++)) do ssh 10.0.0.${host_id} "echo ${host_id} > /es/data/zk/myid";done
[[email protected] ~]# cat /es/data/zk/myid
101
5. 所有节点启动zk服务
[[email protected] ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /es/softwares/zk/bin/../conf/zoo.cfg
Starting zookeeper ... FAILED TO START
[[email protected] ~]# source /etc/profile.d/kafka.sh
[[email protected] ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /es/softwares/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[[email protected] ~]# source /etc/profile.d/kafka.sh
[[email protected] ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /es/softwares/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
6. 编写zk的集群管理脚本
[[email protected] ~]# cat /usr/local/sbin/zkManager.sh
#!/bin/bash
#判断用户是否传参
if [ $# -ne 1 ];then
echo "无效参数,用法为: $0 {start|stop|restart|status}"
exit
fi
#获取用户输入的命令
cmd=$1
#定义函数功能
function zookeeperManger(){
case $cmd in
start)
echo "启动服务"
remoteExecution start
;;
stop)
echo "停止服务"
remoteExecution stop
;;
restart)
echo "重启服务"
remoteExecution restart
;;
status)
echo "查看状态"
remoteExecution status
;;
*)
echo "无效参数,用法为: $0 {start|stop|restart|status}"
;;
esac
}
#定义执行的命令
function remoteExecution(){
for (( i=101 ; i<=103 ; i++ )) ; do
tput setaf 2
echo ========== 10.0.0.${i} zkServer.sh $1 ================
tput setaf 9
ssh 10.0.0.${i} "source /etc/profile.d/kafka.sh; zkServer.sh $1 2>/dev/null"
done
}
#调用函数
zookeeperManger
[[email protected] ~]# chmod +x /usr/local/sbin/zkManager.sh
#执行之前要先停掉原来的进程
[[email protected] ~]# jps
1458 Elasticsearch
2267 Elasticsearch
3644 Jps
3135 QuorumPeerMain
[[email protected] ~]# kill 3135
[[email protected] ~]# jps
1458 Elasticsearch
3658 Jps
2267 Elasticsearch
[[email protected] ~]# zkManager.sh start
启动服务
========== 10.0.0.101 zkServer.sh start ================
Starting zookeeper ... STARTED
========== 10.0.0.102 zkServer.sh start ================
Starting zookeeper ... STARTED
========== 10.0.0.103 zkServer.sh start ================
Starting zookeeper ... STARTED
[[email protected] ~]# zkManager.sh status
查看状态
========== 10.0.0.101 zkServer.sh status ================
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
========== 10.0.0.102 zkServer.sh status ================
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
========== 10.0.0.103 zkServer.sh status ================
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
7.验证集群
[[email protected] ~]# zkCli.sh -server 10.0.0.101:2181,10.0.0.102:2181,10.0.0.103:2181
Connecting to 10.0.0.101:2181,10.0.0.102:2181,10.0.0.103:2181
...
...
...
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 10.0.0.101:2181,10.0.0.102:2181,10.0.0.103:2181(CONNECTED) 0]
8.使用zkWeb管理zookeeper集群
[[email protected] ~]# ll
-rw-r--r-- 1 root root 28575779 Apr 10 16:09 zkWeb-v1.2.1.jar
[[email protected] ~]# java -jar zkWeb-v1.2.1.jar
四、修改zookeeper的堆内存大小
温馨提示:
修改zookeeper的堆内存大小,一般情况下,生产环境给到2G足以,如果规模较大可以适当调大到4G。
1.查看zk默认的堆内存大小为1GB
[[email protected] ~]# jmap -heap `jps | awk '/QuorumPeerMain/{print $1}'`
Attaching to process ID 2121, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.291-b10
using thread-local object allocation.
Parallel GC with 2 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 1048576000 (1000.0MB)
2.配置ZK的堆内存
[[email protected] ~]# cat > /es/softwares/zk/conf/java.env << 'EOF'
> export JAVA_HOME=/es/softwares/jdk1.8.0_291
> export JVMFLAGS="-Xms256m -Xmx256m $JVMFLAGS"
> EOF
[[email protected] ~]# cat /es/softwares/zk/conf/java.env
export JAVA_HOME=/es/softwares/jdk1.8.0_291
export JVMFLAGS="-Xms256m -Xmx256m $JVMFLAGS"
3.将配置文件同步到集群的其他zk节点上
[[email protected] ~]# data_rsync.sh /es/softwares/zk/conf/java.env
===== rsyncing elk102.com: java.env =====
命令执行成功!
===== rsyncing elk103.com: java.env =====
命令执行成功!
4.重启ZK集群
[[email protected] ~]# zkManager.sh restart
5.验证堆内存
[[email protected] ~]# jmap -heap `jps | awk '/QuorumPeerMain/{print $1}'`
Attaching to process ID 2294, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.291-b10
using thread-local object allocation.
Parallel GC with 2 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 268435456 (256.0MB)
标签:zk,elk101,root,zookeeper,sh,linux,com
From: https://www.cnblogs.com/world-of-yuan/p/17504009.html