目錄
1.前提
2.配置主機名到IP位址的映射
3.修改zoo.cfg配置
4.建立myid檔案并寫入叢集辨別
5.在另外兩台機器進行相同操作
6.檢視叢集狀态配置結束
接着上一篇部落格linux下zookeeper單機搭建,本篇進階一下,利用三台機器進行叢集操作。保證高可用。
1.前提
作者預設讀者已經準備好了三台不同ip的機器或者虛拟機,并且都已經安裝好jdk,linux 下zookeeper安裝教程可以參考我的另一篇部落格。
https://blog.csdn.net/u010199866/article/details/81742866
以下是zookeeper真叢集配置教程
我們準備的三台機器ip分别為
xx.xx.xx.1
xx.xx.xx.2
xx.xx.xx.3
2.配置主機名到IP位址的映射
(此步驟不是必須的,我們可以直接在zk的配置檔案中填寫IP位址),這樣配置的好處是如果某個IP位址發生了變化,我們不需要重新開機zookeeper,直接修改主機對應的IP位址即可。
直接修改/etc/hosts檔案,設定主機zoo-1映射到x.x.x.1,設定主機zoo-2映射到x.x.x.2,設定主機zoo-3映射到x.x.x.3
三台機器都需要相同的hosts配置
vim /etc/hosts
x.x.x.1 zoo-1
x.x.x.2 zoo-2
x.x.x.3 zoo-3
3.修改zoo.cfg配置
vi zoo.cfg
以下是需要在zoo.cfg中增加的配置
server.1=zoo-1:2888:3888
server.2=zoo-2:2888:3888
server.3=zoo-3:2888:3888
配置完成效果
4.建立myid檔案并寫入叢集辨別
在dataDir目錄中,建立一個名為myid的檔案,并寫入機器對應的數字值,比如我們是在zoo-1的機器上,就将該值設定為1,即叢集中sever.1=zoo-1:2888:3888中server.後對應的數字。這是zookeeper用來識别是那一台叢集機器的辨別。
echo "1" > /usr/local/zookeeper-3.5.4-beta/myid
這裡一台機器的叢集配置完成了,接下來我們在另外兩台機器上進行相同的操作,安裝分别zk,配置一摸一樣就行,唯一一個地方需要修改的就是myid的檔案。
注:此處唯一不同的地方是每個dataDir下的myid中的内容要按照zoo.cfg配置檔案中的叢集資訊設定。比如:x.x.x.2對應叢集中的server.2,是以在myid中寫入2
5.在另外兩台機器進行相同操作
是以在其他兩台機器安裝時,直接scp指令複制過去就行,隻需要改這個myid檔案。
到此為止我們三台機器都配置了叢集資訊。
6.檢視叢集狀态配置結束
我們分别啟動機器1,機器2,機器3.叢集會自動選舉出一台伺服器作為leader,其餘伺服器為follower,可以在每台伺服器上使用指令檢視伺服器是什麼類型的。
zkServer.sh status
可以通過用戶端,連接配接到ZooKeeper叢集上。對于用戶端來說,ZooKeeper是一個整體(ensemble),你可以在任何一個結點上建立到服務叢集的連接配接,例如:
[[email protected] bin]# zkCli.sh -server x.x.x.1:2181
Connecting to 10.137.35.134:2181
2018-08-21 15:30:29,656 [myid:] - INFO [main:[email protected]] - Client environm ent:zookeeper.version=3.5.4-beta-7f51e5b68cf2f80176ff944a9ebd2abbc65e7327, built on 05/11/2018 16:27 GMT
2018-08-21 15:30:29,659 [myid:] - INFO [main:[email protected]] - Client environm ent:host.name=localhost
2018-08-21 15:30:29,660 [myid:] - INFO [main:[email protected]] - Client environm ent:java.version=1.8.0_181
2018-08-21 15:30:29,662 [myid:] - INFO [main:[email protected]] - Client environm ent:java.vendor=Oracle Corporation
2018-08-21 15:30:29,663 [myid:] - INFO [main:[email protected]] - Client environm ent:java.home=/usr/java/jdk1.8.0_181/jre
2018-08-21 15:30:29,663 [myid:] - INFO [main:[email protected]] - Client environm ent:java.class.path=/usr/local/zookeeper-3.5.4-beta/bin/../build/classes:/usr/lo cal/zookeeper-3.5.4-beta/bin/../build/lib/*.jar:/usr/local/zookeeper-3.5.4-beta/ bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/s lf4j-api-1.7.25.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/netty-3.10.6.Fina l.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/log4j-1.2.17.jar:/usr/local/zoo keeper-3.5.4-beta/bin/../lib/jline-2.11.jar:/usr/local/zookeeper-3.5.4-beta/bin/ ../lib/jetty-util-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.4-beta/bin/../li b/jetty-servlet-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/ jetty-server-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/jet ty-security-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/jett y-io-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/jetty-http- 9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/javax.servlet-ap i-3.1.0.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/jackson-mapper-asl-1.9.11 .jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/jackson-core-asl-1.9.11.jar:/usr /local/zookeeper-3.5.4-beta/bin/../lib/commons-cli-1.2.jar:/usr/local/zookeeper- 3.5.4-beta/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper-3.5.4- beta/bin/../zookeeper-3.5.4-beta.jar:/usr/local/zookeeper-3.5.4-beta/bin/../src/ java/lib/*.jar:/usr/local/zookeeper-3.5.4-beta/bin/../conf:.:/usr/java/jdk1.8.0_ 181/lib:/usr/java/jdk1.8.0_181/jre/lib:
2018-08-21 15:30:29,663 [myid:] - INFO [main:[email protected]] - Client environm ent:java.library.path=:/usr/local/lib:/usr/java/packages/lib/amd64:/usr/lib64:/l ib64:/lib:/usr/lib
2018-08-21 15:30:29,663 [myid:] - INFO [main:[email protected]] - Client environm ent:java.io.tmpdir=/tmp
2018-08-21 15:30:29,663 [myid:] - INFO [main:[email protected]] - Client environm ent:java.compiler=<NA>
2018-08-21 15:30:29,663 [myid:] - INFO [main:[email protected]] - Client environm ent:os.name=Linux
2018-08-21 15:30:29,663 [myid:] - INFO [main:[email protected]] - Client environm ent:os.arch=amd64
2018-08-21 15:30:29,664 [myid:] - INFO [main:[email protected]] - Client environm ent:os.version=2.6.32-431.el6.x86_64
2018-08-21 15:30:29,664 [myid:] - INFO [main:[email protected]] - Client environm ent:user.name=root
2018-08-21 15:30:29,664 [myid:] - INFO [main:[email protected]] - Client environm ent:user.home=/root
2018-08-21 15:30:29,664 [myid:] - INFO [main:[email protected]] - Client environm ent:user.dir=/usr/local/zookeeper-3.5.4-beta/bin
2018-08-21 15:30:29,664 [myid:] - INFO [main:[email protected]] - Client environm ent:os.memory.free=113MB
2018-08-21 15:30:29,666 [myid:] - INFO [main:[email protected]] - Client environm ent:os.memory.max=228MB
2018-08-21 15:30:29,666 [myid:] - INFO [main:[email protected]] - Client environm ent:os.memory.total=119MB
2018-08-21 15:30:29,671 [myid:] - INFO [main:[email protected]] - Initiating client connection, connectString=-------:2181 sessionTimeout=30000 watcher=org.a [email protected]
2018-08-21 15:30:29,712 [myid:] - INFO [main:[email protected]] - jute.maxbu ffer value is 4194304 Bytes
Welcome to ZooKeeper!
2018-08-21 15:30:29,736 [myid:1---------:2181] - INFO [main-SendThread(10.13 7.35.134:2181):[email protected]] - Opening socket connection to server 10.137.35.134/10.137.35.134:2181. Will not attempt to authenticate using SASL ( unknown error)
JLine support is enabled
2018-08-21 15:30:29,866 [myid:10.137.35.134:2181] - INFO [main-SendThread(------------:2181):[email protected]] - Socket connection established, initi ating session, client: /----------:56782, server: ----------/----------- :2181
[zk: -----------:2181(CONNECTING) 0] 2018-08-21 15:30:29,917 [myid:10.137.35.1 34:2181] - INFO [main-SendThread(10.137.35.134:2181):[email protected] ] - Session establishment complete on server 10.137.35.134/10.137.35.134:2181, s essionid = 0x3083dea7eed0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[[email protected] bin]# zkCli.sh -server x.x.x.2:2181
Connecting to 10.137.35.132:2181
2018-08-21 15:30:39,515 [myid:] - INFO [main:[email protected]] - Client environm ent:zookeeper.version=3.5.4-beta-7f51e5b68cf2f80176ff944a9ebd2abbc65e7327, built on 05/11/2018 16:27 GMT
2018-08-21 15:30:39,521 [myid:] - INFO [main:[email protected]] - Client environm ent:host.name=localhost
2018-08-21 15:30:39,521 [myid:] - INFO [main:[email protected]] - Client environm ent:java.version=1.8.0_181
2018-08-21 15:30:39,525 [myid:] - INFO [main:[email protected]] - Client environm ent:java.vendor=Oracle Corporation
2018-08-21 15:30:39,526 [myid:] - INFO [main:[email protected]] - Client environm ent:java.home=/usr/java/jdk1.8.0_181/jre
2018-08-21 15:30:39,526 [myid:] - INFO [main:[email protected]] - Client environm ent:java.class.path=/usr/local/zookeeper-3.5.4-beta/bin/../build/classes:/usr/lo cal/zookeeper-3.5.4-beta/bin/../build/lib/*.jar:/usr/local/zookeeper-3.5.4-beta/ bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/s lf4j-api-1.7.25.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/netty-3.10.6.Fina l.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/log4j-1.2.17.jar:/usr/local/zoo keeper-3.5.4-beta/bin/../lib/jline-2.11.jar:/usr/local/zookeeper-3.5.4-beta/bin/ ../lib/jetty-util-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.4-beta/bin/../li b/jetty-servlet-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/ jetty-server-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/jet ty-security-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/jett y-io-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/jetty-http- 9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/javax.servlet-ap i-3.1.0.jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/jackson-mapper-asl-1.9.11 .jar:/usr/local/zookeeper-3.5.4-beta/bin/../lib/jackson-core-asl-1.9.11.jar:/usr /local/zookeeper-3.5.4-beta/bin/../lib/commons-cli-1.2.jar:/usr/local/zookeeper- 3.5.4-beta/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper-3.5.4- beta/bin/../zookeeper-3.5.4-beta.jar:/usr/local/zookeeper-3.5.4-beta/bin/../src/ java/lib/*.jar:/usr/local/zookeeper-3.5.4-beta/bin/../conf:.:/usr/java/jdk1.8.0_ 181/lib:/usr/java/jdk1.8.0_181/jre/lib:
2018-08-21 15:30:39,526 [myid:] - INFO [main:[email protected]] - Client environm ent:java.library.path=:/usr/local/lib:/usr/java/packages/lib/amd64:/usr/lib64:/l ib64:/lib:/usr/lib
2018-08-21 15:30:39,526 [myid:] - INFO [main:[email protected]] - Client environm ent:java.io.tmpdir=/tmp
2018-08-21 15:30:39,527 [myid:] - INFO [main:[email protected]] - Client environm ent:java.compiler=<NA>
2018-08-21 15:30:39,527 [myid:] - INFO [main:[email protected]] - Client environm ent:os.name=Linux
2018-08-21 15:30:39,527 [myid:] - INFO [main:[email protected]] - Client environm ent:os.arch=amd64
2018-08-21 15:30:39,527 [myid:] - INFO [main:[email protected]] - Client environm ent:os.version=2.6.32-431.el6.x86_64
2018-08-21 15:30:39,527 [myid:] - INFO [main:[email protected]] - Client environm ent:user.name=root
2018-08-21 15:30:39,527 [myid:] - INFO [main:[email protected]] - Client environm ent:user.home=/root
2018-08-21 15:30:39,528 [myid:] - INFO [main:[email protected]] - Client environm ent:user.dir=/usr/local/zookeeper-3.5.4-beta/bin
2018-08-21 15:30:39,528 [myid:] - INFO [main:[email protected]] - Client environm ent:os.memory.free=113MB
2018-08-21 15:30:39,531 [myid:] - INFO [main:[email protected]] - Client environm ent:os.memory.max=228MB
2018-08-21 15:30:39,531 [myid:] - INFO [main:[email protected]] - Client environm ent:os.memory.total=119MB
2018-08-21 15:30:39,536 [myid:] - INFO [main:[email protected]] - Initiating client connection, connectString=10.137.35.132:2181 sessionTimeout=30000 watcher=org.a [email protected]
2018-08-21 15:30:39,555 [myid:] - INFO [main:[email protected]] - jute.maxbu ffer value is 4194304 Bytes
Welcome to ZooKeeper!
2018-08-21 15:30:39,577 [myid:10.137.35.132:2181] - INFO [main-SendThread(10.13 7.35.132:2181):[email protected]] - Opening socket connection to server 10.137.35.132/10.137.35.132:2181. Will not attempt to authenticate using SASL ( unknown error)
JLine support is enabled
2018-08-21 15:30:39,739 [myid:10.137.35.132:2181] - INFO [main-SendThread(10.13 7.35.132:2181):[email protected]] - Socket connection established, initi ating session, client: /10.137.35.134:34479, server: 10.137.35.132/10.137.35.132 :2181
2018-08-21 15:30:39,758 [myid:10.137.35.132:2181] - INFO [main-SendThread(10.13 7.35.132:2181):[email protected]] - Session establishment complete on s erver 10.137.35.132/10.137.35.132:2181, sessionid = 0x103486345220000, negotiate d timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 10.137.35.132:2181(CONNECTED) 0]