(1)安裝:
|
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# tar -zxvf zookeeper-3.4.11.tar.gz
[root@localhost src]# mv zookeeper-3.4.11 /usr/local/zookeeper
|
(2)添加環境變量:
|
[root@localhost local]# vim /etc/profile
|
添加以下內容:
|
export ZOOKEEPER_HOME=/usr/local/zookeeper
export PATH=.:$PATH:$ZOOKEEPER_HOME/bin
|
|
[root@localhost local]# source /etc/profile
|
(3)修改ZooKeeper配置文件:
因為這裡是在單服務器上面搭建zookeeper偽集群,所以需要分別在zookeeper路徑(/usr/local/zookeeper)新建三個目錄:server1 、server2 、server2。用於存儲每個zookeeper節點的配置文件和數據
|
[root@localhost local]# cd /usr/local/zookeeper/
[root@localhost zookeeper]# mkdir server1 server2 server3
|
然後在每個server目錄裡面分別新建目錄:data、dataLog
|
[root@localhost zookeeper]# cd /usr/local/zookeeper/server1/
[root@localhost server1]# mkdir data dataLog
[root@localhost zookeeper]# cd /usr/local/zookeeper/server2/
[root@localhost server2]# mkdir data dataLog
[root@localhost zookeeper]# cd /usr/local/zookeeper/server3/
[root@localhost server3]# mkdir data dataLog
|
在每個節點的data目錄裡面分別寫節點序號
|
[root@localhost zookeeper]# echo '1' > /usr/local/zookeeper/server1/data/myid
[root@localhost zookeeper]# echo '2' > /usr/local/zookeeper/server2/data/myid
[root@localhost zookeeper]# echo '3' > /usr/local/zookeeper/server3/data/myid
|
每個節點分別編輯配置文件zoo.cfg
|
[root@localhost server1]# vim /usr/local/zookeeper/server1/zoo.cfg
|
其內容如下:
|
#server1
tickTime=2000
initLimit=5
syncLimit=2
dataDir=/usr/local/zookeeper/server1/data
dataLogDir=/usr/local/zookeeper/server1/dataLog
clientPort=2100
server.1=192.168.1.159:2200:2300
server.2=192.168.1.159:2201:2301
server.3=192.168.1.159:2202:2302
|
|
[root@localhost server1]# vim /usr/local/zookeeper/server2/zoo.cfg
|
其內容如下:
|
#server2
tickTime=2000
initLimit=5
syncLimit=2
dataDir=/usr/local/zookeeper/server2/data
dataLogDir=/usr/local/zookeeper/server2/dataLog
clientPort=2101
server.1=192.168.1.159:2200:2300
server.2=192.168.1.159:2201:2301
server.3=192.168.1.159:2202:2302
|
|
[root@localhost server1]# vim /usr/local/zookeeper/server3/zoo.cfg
|
其內容如下:
|
#server3
tickTime=2000
initLimit=5
syncLimit=2
dataDir=/usr/local/zookeeper/server3/data
dataLogDir=/usr/local/zookeeper/server3/dataLog
clientPort=2102
server.1=192.168.1.159:2200:2300
server.2=192.168.1.159:2201:2301
server.3=192.168.1.159:2202:2302
|
注意事項:
- 每個節點的dataDir和dataLogDir都需要配置成不一樣,不然會提示此zookeeper節點已啟動
- 每個節點的clientPort 也需要配置成不一樣,這是客戶端連接端口,每個zookeeper節點不能衝突
- 端口2200和端口2300分別為各個節點的連接端口和選舉端口,如果是在單個服務器上面搭建偽集群,則需要配置成不同的端口,不然啟動時會爆端口已被佔用的錯誤
(4)啟動:
|
[root@localhost zookeeper]# cd /usr/local/zookeeper/bin
|
分別啟動三個節點:
|
[root@localhost bin]# ./zkServer.sh start /usr/local/zookeeper/server1/zoo.cfg
[root@localhost bin]# ./zkServer.sh start /usr/local/zookeeper/server2/zoo.cfg
[root@localhost bin]# ./zkServer.sh start /usr/local/zookeeper/server3/zoo.cfg
|
檢查集群狀態:
|
[root@localhost bin]# ./zkServer.sh status /usr/local/zookeeper/server1/zoo.cfg
|
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/server1/zoo.cfg
Mode: follower
|
[root@localhost bin]# ./zkServer.sh status /usr/local/zookeeper/server2/zoo.cfg
|
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/server2/zoo.cfg
Mode: follower
|
[root@localhost bin]# ./zkServer.sh status /usr/local/zookeeper/server3/zoo.cfg
|
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/server3/zoo.cfg
Mode: leader
(5)連接並測試基本操作:
連接任意一個節點:
|
[root@localhost bin]# ./zkCli.sh -server 192.168.1.159:2100
|
注意:這裡連接的端口是clientPort
i)ls命令:
查看指定節點中包含的子節點,如:ls /
ii)create命令:
創建節點並賦值,如:create /zk_test “TEST”
iii)get命令:
讀取指定節點內容,如:get /zk_test
iv)set命令:
重新設置節點內容,如:set /zk_test “NEW TEST VALUE”
v)delete命令:
刪除節點,如:delete /zk_test
vi)history命令:
列出命令執行歷史
vii)quit命令:
退出客戶端連接
留言
張貼留言