基于centos8搭建zookeeper集群
【README】 本文基于centos8 搭建
1,其他linux版本,命令可能不同;
2,集群包括3個(gè)節(jié)點(diǎn),如下(因?yàn)椴捎肗AT模型進(jìn)行網(wǎng)絡(luò)連接,需要讓windows和linux機(jī)器在同一個(gè)網(wǎng)段):
centos01: 192.168.163.201 centos02: 192.168.163.202 centos02: 192.168.163.203【1】 軟件準(zhǔn)備
1、遠(yuǎn)程文件傳輸軟件
window 與 centos 文件傳輸, sz, rz?
yum -y install lrzsz
yum -y install vim
yum -y install scp??
yum -y install rsync ? 遠(yuǎn)程同步文件?
2、安裝java
vim /etc/profile?
# JVM ENV ?
JAVA_HOME=/usr/local/java/jdk1.8
JRE_HOME=$JAVA_HOME/jre
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME CLASSPATH PATH JRE_HOME
source /etc/profile 更新
java -version?
【2】搭建單機(jī)模式的zk?
步驟1:??下載 zookeeper 框架包, 本文使用 zookeeper-3.4.10 ,?https://archive.apache.org/dist/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz?
[root@localhost module]# ls zookeeper-3.4.10.tar.gz步驟2:解壓,位置在 /opt/module
tar -zxvf zookeeper-3.4.10.tar.gz [root@localhost module]# ls zookeeper-3.4.10 zookeeper-3.4.10.tar.gz [root@localhost module]# pwd /opt/module [root@localhost module]# cd zookeeper-3.4.10 [root@localhost zookeeper-3.4.10]# pwd /opt/module/zookeeper-3.4.10步驟3: 修改 zookeeper 配置文件?
[root@localhost zookeeper-3.4.10]# cd conf [root@localhost conf]# ls configuration.xsl log4j.properties zoo_sample.cfg [root@localhost conf]# cp zoo_sample.cfg zoo.cfg [root@localhost conf]# vim zoo.cfg設(shè)置zoo.cfg 如下,修改 dataDir=/opt/module/zookeeper-3.4.10/zkdata? zkdata為自定義文件夾?
-- zoo.cfg[root@localhost conf]# cat 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=/opt/module/zookeeper-3.4.10/zkdata # 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步驟4: 新建?/opt/module/zookeeper-3.4.10/zkdata 文件夾
[root@localhost zookeeper-3.4.10]# pwd /opt/module/zookeeper-3.4.10 [root@localhost zookeeper-3.4.10]# mkdir zkdata步驟5: 啟動(dòng)zk,并查看zk狀態(tài)
[root@localhost zookeeper-3.4.10]# bin/zkServer.sh start ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@localhost zookeeper-3.4.10]# [root@localhost zookeeper-3.4.10]# bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Mode: standalonestandalone 為單機(jī)模式 ;?
提醒:這里可能報(bào)錯(cuò)? Error contacting service. It is probably not running. ?
解決方法1: 關(guān)閉防火墻 查看防火墻狀態(tài):firewall-cmd --state 關(guān)閉防火墻:systemctl stop firewalld.service解決方法2:?https://blog.csdn.net/PacosonSWJTU/article/details/111260961 懂的查看 zk 目錄下的 zookeeper.out 日志文件;步驟6: 啟動(dòng)zk客戶端, 并退出(quit)
[root@localhost zookeeper-3.4.10]# bin/zkCli.sh Connecting to localhost:2181 Welcome to ZooKeeper! ...... WATCHER::WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0] [zk: localhost:2181(CONNECTED) 0] ls / [zookeeper][zk: localhost:2181(CONNECTED) 1] quit Quitting... 2020-12-20 03:42:33,745 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x1767c83c15e0000 closed 2020-12-20 03:42:33,747 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x1767c83c15e0000 [root@localhost zookeeper-3.4.10]#步驟7:停止zk線程(或有)
[root@localhost zookeeper-3.4.10]# bin/zkServer.sh stop ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED補(bǔ)充:zoo.cfg 配置解析
| 序號(hào) | 屬性 | 描述 |
| 1 | tickTime=2000 | 心跳,2000毫秒一個(gè)心跳;(2秒) |
| 2 | initLimit=10 | 10個(gè)2秒, 20秒是Leader和Follower 剛開(kāi)始(初始化)通信的最大延時(shí)時(shí)間; 如果超過(guò)這個(gè)時(shí)間, 則zk服務(wù)器認(rèn)為 兩者連接失敗;? |
| 3 | syncLimit=5 | 5個(gè)2秒, 10秒是 Leader 和 Follower的在集群正常啟動(dòng)后的通信的最大延時(shí)時(shí)間; ?如果超過(guò)這個(gè)時(shí)間, 則zk服務(wù)器認(rèn)為 兩者連接失敗;? |
| 4 | dataDir=/opt/module/zookeeper-3.4.10/zkdata | 設(shè)置zk數(shù)據(jù)文件存儲(chǔ)路徑;? |
| 5 | clientPort=2181 | 客戶端訪問(wèn) zk 服務(wù)的所在主機(jī)的端口號(hào); |
?
【3】搭建集群模式的zk?
步驟1:201機(jī)器,在zoo.cfg下增加集群配置;
server.A=B.C.D , 如 server.1=192.168.163.201:2888:3888
其中A 等于myid文件的值1;?
B表示本服務(wù)器地址 192.168.163.201;?
C表示本服務(wù)器與集群中 Leader服務(wù)器交換信息的端口 ; 兩者同步的數(shù)據(jù)是 數(shù)據(jù)副本;? 2888;
D表示 在選舉Leader服務(wù)器時(shí),服務(wù)器間相互通信的端口; 3888;
小結(jié):添加如下配置:
server.1=192.168.163.201:2888:3888 server.2=192.168.163.202:2888:3888 server.3=192.168.163.203:2888:3888 [root@localhost module]# cat zookeeper-3.4.10/conf/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=/opt/module/zookeeper-3.4.10/zkdata # 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.163.201:2888:3888 server.2=192.168.163.202:2888:3888 server.3=192.168.163.203:2888:3888步驟2:201機(jī)器,設(shè)置服務(wù)器編號(hào),在zkdata文件夾下新建myid,內(nèi)容為1;
[root@localhost zkdata]# pwd /opt/module/zookeeper-3.4.10/zkdata [root@localhost zkdata]# echo "1" > myid步驟3:把201機(jī)器下的 zookeeper 文件夾同步到202,203機(jī)器 ;
[root@localhost module]# rsync -azv zookeeper-3.4.10/ root@192.168.163.202:/opt/module/zookeeper-3.4.10/ [root@localhost module]# rsync -azv zookeeper-3.4.10/ root@192.168.163.203:/opt/module/zookeeper-3.4.10/步驟4:修改 202機(jī)器的myid為2, 203機(jī)器的myid為3 ;
-- 202機(jī)器 [root@localhost zookeeper-3.4.10]# cat zkdata/myid 2 -- 203 機(jī)器 [root@localhost zookeeper-3.4.10]# cat zkdata/myid 3步驟5: 啟動(dòng)zk集群
-- 201機(jī)器-啟動(dòng)失敗 [root@localhost zookeeper-3.4.10]# bin/zkServer.sh start ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@localhost zookeeper-3.4.10]# bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Error contacting service. It is probably not running. [root@localhost zookeeper-3.4.10]# -- 202機(jī)器-啟動(dòng)成功-并且成為leader [root@localhost zookeeper-3.4.10]# bin/zkServer.sh start ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@localhost zookeeper-3.4.10]# bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Mode: leader [root@localhost zookeeper-3.4.10]#注: zk的選舉機(jī)制,自行g(shù)oogle, 這里不再贅述;?
-- 203機(jī)器-啟動(dòng)成功-follower [root@localhost zookeeper-3.4.10]# bin/zkServer.sh start ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@localhost zookeeper-3.4.10]# [root@localhost zookeeper-3.4.10]# bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Mode: follower我們?cè)倩仡^查看201機(jī)器的zk狀態(tài) ; 當(dāng)202,203啟動(dòng)zk線程后, 我們?cè)俅尾榭?01的zk狀態(tài)為 成功且是 follower;?
-- 201 [root@localhost zookeeper-3.4.10]# bin/zkServer.sh start ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@localhost zookeeper-3.4.10]# bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Error contacting service. It is probably not running. [root@localhost zookeeper-3.4.10]# -- ### 我是分割線, 202,203開(kāi)啟zk線程后,[root@localhost zookeeper-3.4.10]# bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Mode: follower [root@localhost zookeeper-3.4.10]#至此,zk集群?jiǎn)?dòng)成功;?
?
【4】其他?
【4.1】zookeeper 選舉機(jī)制?
1)半數(shù)機(jī)制: 當(dāng)有半數(shù)以上機(jī)器存活,則集群可用;否則不可用; (不包括半數(shù));建議zookeeper 集群的機(jī)器數(shù)量為奇數(shù) ;?
2)雖然配置文件沒(méi)有指定 master 和 slave,但在工作時(shí), zookeeper臨時(shí)選擇一條機(jī)器作為 master或 leader, 其他機(jī)器為 slave或follower;
【4.2】zk客戶端命令行
-- zk客戶端常用命令 ls create get delete rmr set登錄zk客戶端
[root@localhost zookeeper-3.4.10]# bin/zkCli.sh1) ls 查看節(jié)點(diǎn)
ls2 查看節(jié)點(diǎn)詳情?
[zk: localhost:2181(CONNECTED) 0] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 1] ls2 / [zookeeper] cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x0 cversion = -1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 1 [zk: localhost:2181(CONNECTED) 2]stat結(jié)構(gòu)體
| 序號(hào) | key | 描述 |
| 1 | cZxid | 創(chuàng)建事務(wù)編號(hào) |
| 2 | ctime | 創(chuàng)建時(shí)間 |
| 3 | mZxid | 修改事務(wù)編號(hào) |
| 4 | mtime | 修改時(shí)間 |
| 5 | pZxid | 最后更新的子節(jié)點(diǎn) |
| 6 | cversion | 子節(jié)點(diǎn)變化編號(hào) |
| 7 | dataVersion | 數(shù)據(jù)變化編號(hào) |
| 8 | aclVersion | 訪問(wèn)控制列表的變化號(hào) |
| 9 | ephemeralOwner | 若是臨時(shí)節(jié)點(diǎn),表示的是znode擁有者的session id,如果不是,則是0 |
| 10 | dataLength | znode的數(shù)據(jù)長(zhǎng)度 |
| 11 | numChildren | znode的子節(jié)點(diǎn)個(gè)數(shù) |
2)create 創(chuàng)建節(jié)點(diǎn)
[zk: localhost:2181(CONNECTED) 2] create /sichuan "sichuan" Created /sichuan [zk: localhost:2181(CONNECTED) 3] ls / [sichuan, zookeeper]3)get 獲取節(jié)點(diǎn)
[zk: localhost:2181(CONNECTED) 4] get /sichuan sichuan cZxid = 0x100000002 ctime = Sun Dec 20 04:22:16 CST 2020 mZxid = 0x100000002 mtime = Sun Dec 20 04:22:16 CST 2020 pZxid = 0x100000002 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0 [zk: localhost:2181(CONNECTED) 5]4)delete 刪除節(jié)點(diǎn)
[zk: localhost:2181(CONNECTED) 5] create /sichuan/cd "chengdu" Created /sichuan/cd [zk: localhost:2181(CONNECTED) 7] create /sichuan/leshan "leshan4" Created /sichuan/leshan [zk: localhost:2181(CONNECTED) 8] ls /sichuan [cd, leshan] [zk: localhost:2181(CONNECTED) 9] delete /sichuan/leshan [zk: localhost:2181(CONNECTED) 10] [zk: localhost:2181(CONNECTED) 10] ls /sichuan [cd] [zk: localhost:2181(CONNECTED) 11]5)rmr?遞歸刪除當(dāng)前目錄及其下的所有子目錄?
[zk: localhost:2181(CONNECTED) 10] ls /sichuan [cd] [zk: localhost:2181(CONNECTED) 11] create /sichuan/my "mianyang" Created /sichuan/my [zk: localhost:2181(CONNECTED) 12] ls /sichuan [cd, my] [zk: localhost:2181(CONNECTED) 13] rmr /sichuan [zk: localhost:2181(CONNECTED) 14] [zk: localhost:2181(CONNECTED) 14] ls /sichuan Node does not exist: /sichuan6)set 設(shè)置節(jié)點(diǎn)存儲(chǔ)的值?
[zk: localhost:2181(CONNECTED) 18] create /china "china" Created /china [zk: localhost:2181(CONNECTED) 19] [zk: localhost:2181(CONNECTED) 19] get /china china cZxid = 0x10000000b ctime = Sun Dec 20 04:28:10 CST 2020 mZxid = 0x10000000b mtime = Sun Dec 20 04:28:10 CST 2020 pZxid = 0x10000000b cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 5 numChildren = 0[zk: localhost:2181(CONNECTED) 20] set /china "china xiongqi" cZxid = 0x10000000b ctime = Sun Dec 20 04:28:10 CST 2020 mZxid = 0x10000000c mtime = Sun Dec 20 04:28:35 CST 2020 pZxid = 0x10000000b cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 13 numChildren = 0[zk: localhost:2181(CONNECTED) 21] get /china china xiongqi cZxid = 0x10000000b ctime = Sun Dec 20 04:28:10 CST 2020 mZxid = 0x10000000c mtime = Sun Dec 20 04:28:35 CST 2020 pZxid = 0x10000000b cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 13 numChildren = 0 [zk: localhost:2181(CONNECTED) 22]7)zk有4種節(jié)點(diǎn)類型:
1、類型1-持久目錄節(jié)點(diǎn):?客戶端與服務(wù)器斷開(kāi)后,創(chuàng)建的節(jié)點(diǎn)不刪除;
2、類型2-持久化順序編號(hào)目錄節(jié)點(diǎn):客戶端與服務(wù)器斷開(kāi)連接后, 該節(jié)點(diǎn)依舊存在,只是zookeeper 給該節(jié)點(diǎn)名稱進(jìn)行順序編號(hào);?
應(yīng)用場(chǎng)景:在分布式系統(tǒng)中,順序號(hào)可以被用于為所有的事件進(jìn)行全局排序,這樣客戶端可以通過(guò)順序號(hào)推斷事件的順序; (哪臺(tái)服務(wù)器先上,哪臺(tái)服務(wù)器后上)
3、類型3-臨時(shí)目錄節(jié)點(diǎn):兩者斷開(kāi)后, 創(chuàng)建的節(jié)點(diǎn)被刪除; ?
應(yīng)用場(chǎng)景: zk 服務(wù)器監(jiān)聽(tīng)某臺(tái)客戶端機(jī)器的狀態(tài); 若客戶端機(jī)器下線,則zk服務(wù)器節(jié)點(diǎn)也刪除; 集群從而間接知曉 客戶端機(jī)器下線;(服務(wù)器節(jié)點(diǎn)動(dòng)態(tài)上下線) ?
4、類型4-臨時(shí)順序編號(hào)目錄節(jié)點(diǎn):客戶端與服務(wù)器斷開(kāi)后,該節(jié)點(diǎn)被刪除; 只是 zk 對(duì)該服務(wù)器節(jié)點(diǎn)名稱進(jìn)行了順序編號(hào);?
?
8)荔枝: 創(chuàng)建有序節(jié)點(diǎn) (-s) 創(chuàng)建臨時(shí)節(jié)點(diǎn) -e?
有序節(jié)點(diǎn)
-- 創(chuàng)建有序節(jié)點(diǎn) [zk: localhost:2181(CONNECTED) 24] ls /china [] [zk: localhost:2181(CONNECTED) 25] create -s /china/beijing "beijing" Created /china/beijing0000000000 [zk: localhost:2181(CONNECTED) 26] create -s /china/beijing "beijing" Created /china/beijing0000000001 [zk: localhost:2181(CONNECTED) 27] create -s /china/beijing "beijing" Created /china/beijing0000000002 [zk: localhost:2181(CONNECTED) 28] create -s /china/beijing "beijing" Created /china/beijing0000000003 [zk: localhost:2181(CONNECTED) 29] ls /china [beijing0000000001, beijing0000000000, beijing0000000003, beijing0000000002]創(chuàng)建臨時(shí)+有序節(jié)點(diǎn) (chongqing)
[zk: localhost:2181(CONNECTED) 31] create -e -s /china/chongqing "chongqing4" Created /china/chongqing0000000004 [zk: localhost:2181(CONNECTED) 32] [zk: localhost:2181(CONNECTED) 32] ls /china [beijing0000000001, beijing0000000000, chongqing0000000004, beijing0000000003, beijing0000000002]退出 當(dāng)前客戶端 quit 并再次登錄客戶端 , 發(fā)現(xiàn)chognqing節(jié)點(diǎn)被刪除了;?
[zk: localhost:2181(CONNECTED) 33] quit Quitting... 2020-12-20 04:38:48,888 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x1767ca215950000 closed 2020-12-20 04:38:48,890 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x1767ca215950000 [root@localhost zookeeper-3.4.10]# [root@localhost zookeeper-3.4.10]# bin/zkCli.sh[zk: localhost:2181(CONNECTED) 0] ls /china [beijing0000000001, beijing0000000000, beijing0000000003, beijing0000000002] [zk: localhost:2181(CONNECTED) 1]?
還有話說(shuō): 為啥 -s是有序,-e是臨時(shí)。看下zk的java api 的節(jié)點(diǎn)類型常量命名就知道了,如下:
/**** CreateMode value determines how the znode is created on ZooKeeper.*/ public enum CreateMode {/*** The znode will not be automatically deleted upon client's disconnect.*/PERSISTENT (0, false, false),/*** The znode will not be automatically deleted upon client's disconnect,* and its name will be appended with a monotonically increasing number.*/PERSISTENT_SEQUENTIAL (2, false, true),/*** The znode will be deleted upon the client's disconnect.*/EPHEMERAL (1, true, false),/*** The znode will be deleted upon the client's disconnect, and its name* will be appended with a monotonically increasing number.*/EPHEMERAL_SEQUENTIAL (3, true, true);最后, 可以通過(guò) zk提供的javaapi 連接 zk集群, refer 2?https://blog.csdn.net/PacosonSWJTU/article/details/111404364?
?
?
總結(jié)
以上是生活随笔為你收集整理的基于centos8搭建zookeeper集群的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Meta 首席技术官:在竞争对手面前并未
- 下一篇: shell脚本启动kafka集群的多台节