搭建 Zookeeper 集羣環境
-
下載安裝包
# 下載 wget https://apache.org/dist/zookeeper/stable/apache-zookeeper-3.5.5-bin.tar.gz # 解壓 tar -zxvf apache-zookeeper-3.5.5-bin.tar.gz # 移動到要安裝的目錄並 cd 進入相應路徑 # 。。。命令省略。。。
-
配置
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=/home/shaw/data/zookeeper # 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.100.117:2888:3888 server.2=192.168.100.118:2888:3888 server.3=192.168.100.119:2888:3888
注:
- 上面的配置中,Zookeeper 的數據目錄配置爲
/home/shaw/data/zookeeper
; - 配置中的
192.168.100.117
、192.168.100.118
、192.168.100.117
爲三個 zookeeper 集羣服務器的 IP 地址; - 配置中的
2888
和3888
分別爲集羣間進行通信、選舉的端口; server.1
、server.2
、server.3
中的1
、2
、3
分別對應/home/shaw/data/zookeeper/myid
中的內容,例如server.1
服務器上的/home/shaw/data/zookeeper/myid
內容便是1
,server.2
服務器上的/home/shaw/data/zookeeper/myid
內容便是2
;
- 上面的配置中,Zookeeper 的數據目錄配置爲
-
在 zookeeper 的數據目錄(本例中爲
/home/shaw/data/zookeeper
)下配置 myid; -
啓動各個服務器上的 zookeeper 服務:
./bin/zkServer.sh start
-
查看各個服務器上的 zookeeper 啓動狀態:
./bin/zkServer.sh status
-
連接到 zookeeper 服務器:
# 連接到 192.168.100.117 zookeeper 服務器,端口號 2181 ./bin/zkCli.sh -server 192.168.100.117:2181
-
一些常用命令:
# 查看根目錄下的節點 ls / # 創建 /shawearn 節點 create /shawearn # 或創建的同時指定節點值 create /shawearn hello # 查看節點值 get /shawearn # 設置節點值 set /shawearn HelloWorld! # 刪除指定節點,節點下面必須不含子節點 delete /shawearn # 刪除指定節點及其下屬所有節點 deleteall /shawearn