zk单机部署
root@ubuntu20:~# cd /apps
注意:3.5.x版本之后,应下载 apache-zookeeper-x.x.x-bin.tar.gz ,而不是 apache-zookeeper-x.x.x.tar.gz
会报Error: Could not find or load main class org.apache.zookeeper.ZooKeeperMain
tar -xf apache-zookeeper-3.8.2-bin.tar.gz
cd apache-zookeeper-3.8.2-bin/
cd conf/
mv zoo_sample.cfg zoo.cfg
vi zoo.cfg
bash
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10 #cs
# 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=/data/zookeeper
# the port at which the clients will connect
clientPort=2181
.../bin/zkServer.sh start
ss -nlt |grep 2181
./bin/zkCli.sh
[zk: localhost:2181(CONNECTED) 1] create /host1 "192.168.1.100"
Created /host1
[zk: localhost:2181(CONNECTED) 2] get /host1
192.168.1.100
[zk: localhost:2181(CONNECTED) 3] ls /
[host1, zookeeper]
[zk: localhost:2181(CONNECTED) 8] create /luo
Created /luo
[zk: localhost:2181(CONNECTED) 9] create /luo/host1 "8.8.8.8"
Created /luo/host1
[zk: localhost:2181(CONNECTED) 10] create /luo/host2 "8.8.8.8"
Created /luo/host2
[zk: localhost:2181(CONNECTED) 11] ls /luo
[host1, host2]
[zk: localhost:2181(CONNECTED) 27] get /luo/host2 #最后的信息要用get
8.8.8.8
python创建
bash
#!/bin/enc python
#coding:utf8
#需要安装kazoo 使用 pip3 install kazoo
from kazoo.client import KazooClient
zk=KazooClient(hosts='192.168.74.70:2181')
zk.start()
#makepath=True makepath是递归创建
zk.create('/name/t1',b'luohuiwen',makepath=True)
#查查看具体数据 相当于get
#data=zk.get('/name/t1')
## 获取某个节点下所有子节点,相当于ls
data = zk.get_children('/name/')
print(data)
#与zk断开
zk.stop()
连接zookConnection dropped: socket connection error: 句柄无效。
结论:kazoo版本2.9.0问题, 重新安装2.8.0版本就ok了,安装时指定版本号 kazoo==2.8.0
pip show kazoo
pip3 uninstall kazoo
pip3 install kazoo==2.8.0
zk集群部署
安装jdk
apt update
apt install openjdk-11-jdk -y
写入leader ,leader向flower同步
集群中一半写入成功就算成功,
集群中要一半以上的zk工作正常,整个集群可用
2181 客户端访问端口
2888 数据同步端口 lender才有
3888 选举端口
flower会和lender的2888端口镜像同步
bash
root@ubuntu20:/apps/apache-zookeeper-3.8.2-bin# grep -vE "^$|^#" conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/zookeeper
clientPort=2181
maxClientCnxns=120
拷贝配置文件到其他节点
root@ubuntu20:/apps/apache-zookeeper-3.8.2-bin# scp conf/zoo.cfg 192.168.74.71:/apps/apache-zookeeper-3.8.2-bin/
root@ubuntu20:/apps/apache-zookeeper-3.8.2-bin# scp conf/zoo.cfg 192.168.74.72:/apps/apache-zookeeper-3.8.2-bin/
各个节点生成myid文件
mkdir -p /data/zookeeper/ && echo 1 > /data/zookeeper/myid
mkdir -p /data/zookeeper/ && echo 2 > /data/zookeeper/myid
mkdir -p /data/zookeeper/ && echo 3 > /data/zookeeper/myid
/apps/apache-zookeeper-3.8.2-bin/bin/zkServer.sh start
/apps/apache-zookeeper-3.8.2-bin/bin/zkServer.sh status
/apps/apache-zookeeper-3.8.2-bin/bin/zkServer.sh restart
72是leader
在70写入数据72可以查看
ZooInspector查看
在72 leader查看 netstat -antpl
tcp6 0 0 192.168.74.72:2888 192.168.74.70:33952 ESTABLISHED 161320/java
tcp6 0 0 192.168.74.72:2888 192.168.74.71:35050 ESTABLISHED 161320/java
192.168.74.70的33952端口(随机端口) 向192.168.74.72:2888 同步数据
选举过程
投票
对比事务id,事务id最新的成为主,一样的比myid ,myid大的成为leader
监控2181端口
客户端编译
不支持java11版本
cd /opt && mkdir zk && cd zk
apt install git -y
git clone https://github.com/zzhang5/zooinspector.git
cd zooinspector/
mvn clean package
mvn clean package
跳过测试
mvn clean install package -Dmaven.test.skip=true