Linux伺服器搭建概要(四)
阿新 • • 發佈:2018-11-02
環境資訊
centos7.2
安裝目錄
zookeeper下載與安裝
1. 安裝前工作
基本介紹
1 zookeeper是一個類似hdfs的樹形檔案結構,zookeeper可以用來保證資料在(zk)叢集之間的資料的事務性一致、 2 zookeeper有watch事件,是一次性觸發的,當watch監視的資料發生變化時,通知設定了該watch的client,即watcher 3 zookeeper有三個角色:Learner,Follower,Observer 4 zookeeper應用場景: 統一命名服務(Name Service) 配置管理(Configuration Management) 叢集管理(Group Membership) 共享鎖(Locks) 佇列管理
Zookeeper配置(搭建zookeeper伺服器叢集)
目標
搭建Zookeeper偽叢集
搭建叢集說明
Zookeeper安裝方式有三種,單機模式和叢集模式以及偽叢集模式。 ■ 單機模式:Zookeeper只執行在一臺伺服器上,適合測試環境; ■ 偽叢集模式:就是在一臺物理機上執行多個Zookeeper 例項; ■ 叢集模式:Zookeeper運行於一個叢集上,適合生產環境,這個計算機叢集被稱為一個“集合體”(ensemble) Zookeeper通過複製來實現高可用性,只要集合體中半數以上的機器處於可用狀態,它就能夠保證服務繼續。為什麼一定要超過半數呢?這跟Zookeeper的複製策略有關:zookeeper確保對znode 樹的每一個修改都會被複制到集合體中超過半數的機器上。 此次搭建說明: 1. 搭建三個節點的Zookeeper叢集 2. 下面的配置三個節點配置都是需要改的,後面會給出例項 3. 在搭建偽叢集的時候是在同一個伺服器上建立三個節點的例項,這時候不能讓zookeeper的cliPore埠相同 4. 在啟動一個的時候不要去看zookeeper節點是否啟動成功,因為zookeeper叢集會檢測其他的節點是否啟動成功(zookeeper策略),如果檢測不到就會報錯,我們直接全部啟動,最後在看啟動的狀態 5. 啟動的時候如果是建立真正的叢集,需要確定節點之間埠對外是否可訪問,(可以增加埠開放,或者關閉防火牆/白名單)
下載與安裝
下載
wget "http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz"
解壓
[[email protected] installPath]# tar -zxvf zookeeper-3.4.12.tar.gz
移動複製建立叢集目錄
[[email protected] java]# pwd
/usr/local/java
[[email protected] java]# mkdir zookeeperColony
[[email protected] java]# cd zookeeperColony/
[[email protected] zookeeperColony]# pwd
/usr/local/java/zookeeperColony
[[email protected] zookeeperColony]# cp ./../installPath/zookeeper-3.4.12 zookeeper-3.4.12-One
[[email protected] zookeeperColony]# cp ./../installPath/zookeeper-3.4.12 zookeeper-3.4.12-Two
[[email protected] zookeeperColony]# cp ./../installPath/zookeeper-3.4.12 zookeeper-3.4.12-There
修改基本的配置
- 修改環境變數
[[email protected] zookeeper-3.4.12-One]# vim /etc/profile
追加下面的資料
export ZOOKEEPER_HOME=/usr/local/java/zookeeperColony/zookeeper-3.4.12-One
export PATH=.:${PATH}:${MAVEN_HOME}/bin:$ZOOKEEPER_HOME/bin
重新整理:
source /etc/profile
- 修改zookeeper配置檔案()
[[email protected] conf]# pwd
/usr/local/java/zookeeperColony/zookeeper-3.4.12-One/conf
修改配置檔名稱
到zookeeper下修改配置檔案
mv zoo_sample.cfg zoo.cfg
修改conf: vi zoo.cfg 修改兩處
(1)dataDir=/usr/local/zookeeper/data
(2)最後面新增 (這個是因為偽叢集,所以保證埠不一樣)
server.0=ip:2887:3887
server.1=ip:2888:3888
server.2=ip:2889:3889
伺服器標識配置:
[[email protected] zookeeper-3.4.12-One]# pwd
/usr/local/java/zookeeperColony/zookeeper-3.4.12-One
建立資料夾:mkdir data
建立檔案(在上面建立的data目錄下)myid並填寫內容為0:vi myid (內容為伺服器標識 : 0)
- 修改之後的三個節點的配置(zoo.cfg):
節點1:
# The number of milliseconds of each tick
#基本事件單元,以毫秒為單位。這個時間是作為 Zookeeper 伺服器之間或客戶端與伺服器之間維持心跳的時間間隔,
# 也就是每隔 tickTime時間就會發送一個心跳。
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
#這個配置項是用來配置 Zookeeper 接受客戶端初始化連線時最長能忍受多少個心跳時間間隔數,
# 當已經超過 10 個心跳的時間(也就是 tickTime)長度後 Zookeeper 伺服器還沒有收到客戶端的返回資訊,
# 那麼表明這個客戶端連線失敗。總的時間長度就是 10*2000=20 秒。
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
#這個配置項標識 Leader 與 Follower 之間傳送訊息,請求和應答時間長度,
# 最長不能超過多少個 tickTime 的時間長度,總的時間長度就是 5*2000=10 秒
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#儲存記憶體中資料庫快照的位置,顧名思義就是 Zookeeper 儲存資料的目錄,預設情況下,Zookeeper 將寫資料的日誌檔案也儲存在這個目錄裡。儲存記憶體中資料庫快照的位置,顧名思義就是 Zookeeper 儲存資料的目錄,預設情況下,Zookeeper 將寫資料的日誌檔案也儲存在這個目錄裡。
dataDir=/usr/local/java/zookeeperColony/zookeeper-3.4.12-One/data
# the port at which the clients will connect
#這個埠就是客戶端連線 Zookeeper 伺服器的埠,Zookeeper 會監聽這個埠,接受客戶端的訪問請求。
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.0=172.17.0.11:2887:3887
server.1=172.17.0.11:2888:3888
server.2=172.17.0.11:2889:3889
節點2:
# The number of milliseconds of each tick
#基本事件單元,以毫秒為單位。這個時間是作為 Zookeeper 伺服器之間或客戶端與伺服器之間維持心跳的時間間隔,
# 也就是每隔 tickTime時間就會發送一個心跳。
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
#這個配置項是用來配置 Zookeeper 接受客戶端初始化連線時最長能忍受多少個心跳時間間隔數,
# 當已經超過 10 個心跳的時間(也就是 tickTime)長度後 Zookeeper 伺服器還沒有收到客戶端的返回資訊,
# 那麼表明這個客戶端連線失敗。總的時間長度就是 10*2000=20 秒。
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
#這個配置項標識 Leader 與 Follower 之間傳送訊息,請求和應答時間長度,
# 最長不能超過多少個 tickTime 的時間長度,總的時間長度就是 5*2000=10 秒
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#儲存記憶體中資料庫快照的位置,顧名思義就是 Zookeeper 儲存資料的目錄,預設情況下,Zookeeper 將寫資料的日誌檔案也儲存在這個目錄裡。儲存記憶體中資料庫快照的位置,顧名思義就是 Zookeeper 儲存資料的目錄,預設情況下,Zookeeper 將寫資料的日誌檔案也儲存在這個目錄裡。
dataDir=/usr/local/java/zookeeperColony/zookeeper-3.4.12-Two/data
# the port at which the clients will connect
#這個埠就是客戶端連線 Zookeeper 伺服器的埠,Zookeeper 會監聽這個埠,接受客戶端的訪問請求。
clientPort=2182
# 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.0=172.17.0.11:2887:3887
server.1=172.17.0.11:2888:3888
server.2=172.17.0.11:2889:3889
節點3:
# The number of milliseconds of each tick
#基本事件單元,以毫秒為單位。這個時間是作為 Zookeeper 伺服器之間或客戶端與伺服器之間維持心跳的時間間隔,
# 也就是每隔 tickTime時間就會發送一個心跳。
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
#這個配置項是用來配置 Zookeeper 接受客戶端初始化連線時最長能忍受多少個心跳時間間隔數,
# 當已經超過 10 個心跳的時間(也就是 tickTime)長度後 Zookeeper 伺服器還沒有收到客戶端的返回資訊,
# 那麼表明這個客戶端連線失敗。總的時間長度就是 10*2000=20 秒。
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
#這個配置項標識 Leader 與 Follower 之間傳送訊息,請求和應答時間長度,
# 最長不能超過多少個 tickTime 的時間長度,總的時間長度就是 5*2000=10 秒
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#儲存記憶體中資料庫快照的位置,顧名思義就是 Zookeeper 儲存資料的目錄,預設情況下,Zookeeper 將寫資料的日誌檔案也儲存在這個目錄裡。儲存記憶體中資料庫快照的位置,顧名思義就是 Zookeeper 儲存資料的目錄,預設情況下,Zookeeper 將寫資料的日誌檔案也儲存在這個目錄裡。
dataDir=/usr/local/java/zookeeperColony/zookeeper-3.4.12-There/data
# the port at which the clients will connect
#這個埠就是客戶端連線 Zookeeper 伺服器的埠,Zookeeper 會監聽這個埠,接受客戶端的訪問請求。
clientPort=2183
# 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.0=172.17.0.11:2887:3887
server.1=172.17.0.11:2888:3888
server.2=172.17.0.11:2889:3889
- 操作zookeeper (shell)
zkCli.sh 進入zookeeper客戶端
根據提示命令進行操作:
查詢:ls / ls /zookeeper
建立並賦值:create /bhz hadoop
獲取:get /bhz
設值:set /bhz baihezhuo
建立: create /bhz/test1 value1
疊刪除: rmr /bhz/test1
可以看到zookeeper叢集的資料一致性
建立節點有倆種類型:短暫(ephemeral) 持久(persistent)
- zoo.cfg詳解:
tickTime: 基本事件單元,以毫秒為單位。這個時間是作為 Zookeeper 伺服器之間或客戶端與伺服器之間維持心跳的時間間隔,
也就是每隔 tickTime時間就會發送一個心跳。
dataDir: 儲存記憶體中資料庫快照的位置,顧名思義就是 Zookeeper 儲存資料的目錄,預設情況下,Zookeeper 將寫資料的日誌檔案也儲存在這個目錄裡。
clientPort: 這個埠就是客戶端連線 Zookeeper 伺服器的埠,Zookeeper 會監聽這個埠,接受客戶端的訪問請求。
initLimit: 這個配置項是用來配置 Zookeeper 接受客戶端初始化連線時最長能忍受多少個心跳時間間隔數,
當已經超過 10 個心跳的時間(也就是 tickTime)長度後 Zookeeper 伺服器還沒有收到客戶端的返回資訊,
那麼表明這個客戶端連線失敗。總的時間長度就是 10*2000=20 秒。
syncLimit: 這個配置項標識 Leader 與 Follower 之間傳送訊息,請求和應答時間長度,
最長不能超過多少個 tickTime 的時間長度,總的時間長度就是 5*2000=10 秒
server.A = B:C:D :
A表示這個是第幾號伺服器,
B 是這個伺服器的 ip 地址;
C 表示的是這個伺服器與叢集中的 Leader 伺服器交換資訊的埠;
D 表示的是萬一叢集中的 Leader 伺服器掛了,需要一個埠來重新進行選舉,選出一個新的 Leader
- 檢視zookeeper節點資料工具(ZooInspector)
ZooInspector連線工具傳送門