大數據實踐(五)–Zookeeper集羣搭建(Ubuntu)
一、簡介
Zookeeper 是一個開源的分佈式協調服務,目前由 Apache 進行維護。Zookeeper 可以用於實現分佈式系統中常見的發佈/訂閱、負載均衡、命令服務、分佈式協調/通知、集羣管理、Master 選舉、分佈式鎖和分佈式隊列等功能。它具有以下特性:
- 順序一致性:從一個客戶端發起的事務請求,最終都會嚴格按照其發起順序被應用到 Zookeeper 中;
- 原子性:所有事務請求的處理結果在整個集羣中所有機器上都是一致的;不存在部分機器應用了該事務,而另一部分沒有應用的情況;
- 單一視圖:所有客戶端看到的服務端數據模型都是一致的;
- 可靠性:一旦服務端成功應用了一個事務,則其引起的改變會一直保留,直到被另外一個事務所更改;
- 實時性:一旦一個事務被成功應用後,Zookeeper 可以保證客戶端立即可以讀取到這個事務變更後的最新狀態的數據。
使用zookeeeper單獨管理集羣更加高效。
二、集羣安裝
基本命令在所有機器上執行:
1、將zookeeper解壓
解壓到/usr/local/zookeeper
2、配置環境變量
#zoopkeeper home
export ZOOKEEPER_HOME=/usr/local/zookeeper
export PATH=$ZOOKEEPER_HOME/bin:$PATH
記得讓環境生效。
3、修改配置文件
進入安裝目錄的conf下:
cp zoo_sample.cfg 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=/usr/local/zookeeper/data
dataLogDir=/usr/local/zookeeper/log
# 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=master:2287:3387
server.2=node2:2287:3387
server.3=node3:2287:3387
4、節點配置
建立目錄
mkdir -vp /usr/local/zookeeper/data/
在各主機分別使用一個命令表示id
echo "1" > /usr/local/zookeeper/data/myid #在mster
echo "2" > /usr/local/zookeeper/data/myid #在node2
echo "3" > /usr/local/zookeeper/data/myid
5、測試
各個機器使用:啓動zookeeper
zkServer.sh start
使用 zkServer.sh status
查看集羣各個節點狀態。leader 節點和follower 節點。
zkServer.sh status