基于centos8搭建zookeeper集群
【README】 本文基于centos8 搭建
1,其他linux版本,命令可能不同;
2,集群包括3個節點,如下(因為采用NAT模型進行網絡連接,需要讓windows和linux機器在同一個網段):
centos01: 192.168.163.201 centos02: 192.168.163.202 centos02: 192.168.163.203【1】 軟件準備
1、遠程文件傳輸軟件
window 與 centos 文件傳輸, sz, rz?
yum -y install lrzsz
yum -y install vim
yum -y install scp??
yum -y install rsync ? 遠程同步文件?
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】搭建單機模式的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設置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: 啟動zk,并查看zk狀態
[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 為單機模式 ;?
提醒:這里可能報錯? Error contacting service. It is probably not running. ?
解決方法1: 關閉防火墻 查看防火墻狀態:firewall-cmd --state 關閉防火墻:systemctl stop firewalld.service解決方法2:?https://blog.csdn.net/PacosonSWJTU/article/details/111260961 懂的查看 zk 目錄下的 zookeeper.out 日志文件;步驟6: 啟動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補充:zoo.cfg 配置解析
| 序號 | 屬性 | 描述 |
| 1 | tickTime=2000 | 心跳,2000毫秒一個心跳;(2秒) |
| 2 | initLimit=10 | 10個2秒, 20秒是Leader和Follower 剛開始(初始化)通信的最大延時時間; 如果超過這個時間, 則zk服務器認為 兩者連接失敗;? |
| 3 | syncLimit=5 | 5個2秒, 10秒是 Leader 和 Follower的在集群正常啟動后的通信的最大延時時間; ?如果超過這個時間, 則zk服務器認為 兩者連接失敗;? |
| 4 | dataDir=/opt/module/zookeeper-3.4.10/zkdata | 設置zk數據文件存儲路徑;? |
| 5 | clientPort=2181 | 客戶端訪問 zk 服務的所在主機的端口號; |
?
【3】搭建集群模式的zk?
步驟1:201機器,在zoo.cfg下增加集群配置;
server.A=B.C.D , 如 server.1=192.168.163.201:2888:3888
其中A 等于myid文件的值1;?
B表示本服務器地址 192.168.163.201;?
C表示本服務器與集群中 Leader服務器交換信息的端口 ; 兩者同步的數據是 數據副本;? 2888;
D表示 在選舉Leader服務器時,服務器間相互通信的端口; 3888;
小結:添加如下配置:
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機器,設置服務器編號,在zkdata文件夾下新建myid,內容為1;
[root@localhost zkdata]# pwd /opt/module/zookeeper-3.4.10/zkdata [root@localhost zkdata]# echo "1" > myid步驟3:把201機器下的 zookeeper 文件夾同步到202,203機器 ;
[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機器的myid為2, 203機器的myid為3 ;
-- 202機器 [root@localhost zookeeper-3.4.10]# cat zkdata/myid 2 -- 203 機器 [root@localhost zookeeper-3.4.10]# cat zkdata/myid 3步驟5: 啟動zk集群
-- 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機器-啟動成功-并且成為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的選舉機制,自行google, 這里不再贅述;?
-- 203機器-啟動成功-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我們再回頭查看201機器的zk狀態 ; 當202,203啟動zk線程后, 我們再次查看201的zk狀態為 成功且是 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開啟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集群啟動成功;?
?
【4】其他?
【4.1】zookeeper 選舉機制?
1)半數機制: 當有半數以上機器存活,則集群可用;否則不可用; (不包括半數);建議zookeeper 集群的機器數量為奇數 ;?
2)雖然配置文件沒有指定 master 和 slave,但在工作時, zookeeper臨時選擇一條機器作為 master或 leader, 其他機器為 slave或follower;
【4.2】zk客戶端命令行
-- zk客戶端常用命令 ls create get delete rmr set登錄zk客戶端
[root@localhost zookeeper-3.4.10]# bin/zkCli.sh1) ls 查看節點
ls2 查看節點詳情?
[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結構體
| 序號 | key | 描述 |
| 1 | cZxid | 創建事務編號 |
| 2 | ctime | 創建時間 |
| 3 | mZxid | 修改事務編號 |
| 4 | mtime | 修改時間 |
| 5 | pZxid | 最后更新的子節點 |
| 6 | cversion | 子節點變化編號 |
| 7 | dataVersion | 數據變化編號 |
| 8 | aclVersion | 訪問控制列表的變化號 |
| 9 | ephemeralOwner | 若是臨時節點,表示的是znode擁有者的session id,如果不是,則是0 |
| 10 | dataLength | znode的數據長度 |
| 11 | numChildren | znode的子節點個數 |
2)create 創建節點
[zk: localhost:2181(CONNECTED) 2] create /sichuan "sichuan" Created /sichuan [zk: localhost:2181(CONNECTED) 3] ls / [sichuan, zookeeper]3)get 獲取節點
[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 刪除節點
[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?遞歸刪除當前目錄及其下的所有子目錄?
[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 設置節點存儲的值?
[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種節點類型:
1、類型1-持久目錄節點:?客戶端與服務器斷開后,創建的節點不刪除;
2、類型2-持久化順序編號目錄節點:客戶端與服務器斷開連接后, 該節點依舊存在,只是zookeeper 給該節點名稱進行順序編號;?
應用場景:在分布式系統中,順序號可以被用于為所有的事件進行全局排序,這樣客戶端可以通過順序號推斷事件的順序; (哪臺服務器先上,哪臺服務器后上)
3、類型3-臨時目錄節點:兩者斷開后, 創建的節點被刪除; ?
應用場景: zk 服務器監聽某臺客戶端機器的狀態; 若客戶端機器下線,則zk服務器節點也刪除; 集群從而間接知曉 客戶端機器下線;(服務器節點動態上下線) ?
4、類型4-臨時順序編號目錄節點:客戶端與服務器斷開后,該節點被刪除; 只是 zk 對該服務器節點名稱進行了順序編號;?
?
8)荔枝: 創建有序節點 (-s) 創建臨時節點 -e?
有序節點
-- 創建有序節點 [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]創建臨時+有序節點 (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]退出 當前客戶端 quit 并再次登錄客戶端 , 發現chognqing節點被刪除了;?
[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]?
還有話說: 為啥 -s是有序,-e是臨時。看下zk的java api 的節點類型常量命名就知道了,如下:
/**** 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);最后, 可以通過 zk提供的javaapi 連接 zk集群, refer 2?https://blog.csdn.net/PacosonSWJTU/article/details/111404364?
?
?
總結
以上是生活随笔為你收集整理的基于centos8搭建zookeeper集群的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Meta 首席技术官:在竞争对手面前并未
- 下一篇: 中考大捷的意思 中考大捷是什么意思