Zookeeper安裝與配置
- 一、了解 Zookeeper
- 二、下載 Zookeeper
- 三、安裝
- zookeeper 具有單機模式和集群模式
- 1、單機模式
- 2、集群模式
一、了解 Zookeeper
ZooKeeper是一個分布式的,開放原始碼的分布式應用程式協調服務,是Google的Chubby一個開源的實作,是Hadoop和Hbase的重要組件,Zookeeper維護一個類似檔案系統的資料結構,
Zab協議有兩種模式:
- 恢復模式(選主)
- 廣播模式(同步)
當服務啟動或者在領導者崩潰后,Zab就進入了恢復模式,當領導者被選舉出來,且大多數Server完成了和 leader的狀態同步以后,恢復模式就結束了,狀態同步保證了leader和Server具有相同的系統狀態,
需要在Hadoop集群上面搭建,可以選擇搭建單機模式和集群模式
二、下載 Zookeeper
下載鏈接:http://mirrors.hust.edu.cn/apache/zookeeper/
注:選擇與Hadoop兼容版本
三、安裝
解壓:tar -zxvf
[root@master dev]# tar -zxvf apache-zookeeper-3.5.8-bin.tar.gz
解壓之后將檔案名更改為Zookeeper
[root@master dev]# mv apache-zookeeper-3.5.8-bin.tar.gz zookeeper
在home目錄下創建apps檔案夾將zookeeper檔案夾放入
[root@master dev]# mkdir -p /home/apps
[root@master dev]# mv zookeeper /home/apps/
[root@master dev]# ll
total 8
drwxr-xr-x. 9 centos centos 4096 Dec 19 2017 sqoop
drwxr-xr-x. 8 root root 4096 Jan 15 11:17 zookeeper
zookeeper 具有單機模式和集群模式
單機模式較簡單,是指只部署一個zk行程,客戶端直接與該zk行程進行通信,
在開發測驗環境下,通過來說沒有較多的物理資源,因此我們常使用單機模式,當然在單臺物理機上也可以部署集群模式,但這會增加單臺物理機的資源消耗,故在開發環境中,我們一般使用單機模式,
但是要注意,生產環境下不可用單機模式,這是由于無論從系統可靠性還是讀寫性能,單機模式都不能滿足生產的需求,
1、單機模式
新建data和logs目錄(data目錄用來存放資料庫快照,logs目錄用來存放日志檔案)
[root@master dev]# mkdir -p /home/apps/zookeeper/data
[root@master dev]# mkdir -p /home/apps/zookeeper/logs
找到zookeeper目錄下的 conf 組態檔夾
[root@master dev]# cd /home/apps/zookeeper/conf/
[root@master conf]# ll
total 16
-rw-r--r--. 1 root root 535 May 4 2020 configuration.xsl
-rw-r--r--. 1 root root 2712 May 4 2020 log4j.properties
-rw-r--r--. 1 root root 922 May 4 2020 zoo_sample.cfg
-rw-r--r--. 1 root root 152 Jan 15 13:05 zoo.cfg.dynamic.next
[root@master dev]#
重命名 zoo_sample.cfg 為zoo.cfg
[root@master conf]# mv zoo_sample.cfg zoo.cfg
用vi命令打開zoo.cfg檔案
[root@master conf]# vi zoo.cfg
# The number of milliseconds of each tick
tickTime=2000 #Zookeeper 服務器之間或客戶端與服務器之間維持心跳的時間間隔,單位為毫秒
# The number of ticks that the initial
# synchronization phase can take
initLimit=10 #表示允許從服務器連接到leader并完成資料同步的時間,總的時間長度就是 initLimit * tickTime 秒
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5 #配置 Leader 與 Follower 之間發送訊息,請求和應答時間長度,最長不能超過多少個 tickTime 的時間長度,總的時間長度就是 syncLimit * tickTime 秒
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/home/apps/zookeeper/data #Zookeeper 保存資料的資料庫快照的位置
dataLogDir=/home/apps/zookeeper/logs #事務日志日志路徑,若沒提供的話則用dataDir
# the port at which the clients will connect
clientPort=2181 #Zookeeper服務器監聽的埠,以接受客戶端的訪問請求
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60 # 限制連接到ZK上的客戶端數量,并且限制并發連接數量,值為0表示不做任何限制
#
# 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 # 自動清理日志,該引數設定保留多少個快照檔案和對應的事務日志檔案,默認為3,如果小于3則自動調整為3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
配置完之后就可以直接啟動Zookeeper,
[root@master conf]# cd /home/apps/zookeeper/bin
[root@master bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/apps/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@master bin]#
啟動成功
可以使用 status 命令查看zookeeper狀態
[root@master bin]# ./zkServer.sh status
2、集群模式
與單機模式同樣的方法,先新建data和logs目錄,更改 zoo_sample.cfg檔案名為zoo.cfg
用vi命令打開zoo.cfg檔案
[root@master conf]# vi zoo.cfg
# The number of milliseconds of each tick
tickTime=2000 #Zookeeper 服務器之間或客戶端與服務器之間維持心跳的時間間隔,單位為毫秒
# The number of ticks that the initial
# synchronization phase can take
initLimit=10 #表示允許從服務器連接到leader并完成資料同步的時間,總的時間長度就是 initLimit * tickTime 秒
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5 #配置 Leader 與 Follower 之間發送訊息,請求和應答時間長度,最長不能超過多少個 tickTime 的時間長度,總的時間長度就是 syncLimit * tickTime 秒
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/home/apps/zookeeper/data #Zookeeper 保存資料的資料庫快照的位置
dataLogDir=/home/apps/zookeeper/logs #事務日志日志路徑,若沒提供的話則用dataDir
# the port at which the clients will connect
clientPort=2181 #Zookeeper服務器監聽的埠,以接受客戶端的訪問請求
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60 # 限制連接到ZK上的客戶端數量,并且限制并發連接數量,值為0表示不做任何限制
#
# 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 # 自動清理日志,該引數設定保留多少個快照檔案和對應的事務日志檔案,默認為3,如果小于3則自動調整為3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
# server.n n是一個數字,表示這個是第幾號服務器,“=”后面可跟主機地址或者IP地址,2888為集群中從服務器(follower)連接到主服務器(leader)的埠,為主服務器(leader)使用;3888為進行選舉(leader)的時使用的埠
server.1=192.168.1.121:2888:3888
server.2=192.168.1.122:2888:3888
server.3=192.168.1.123:2888:3888
添加代碼:
server.1=192.168.1.121:2888:3888
server.2=192.168.1.122:2888:3888
server.3=192.168.1.123:2888:3888
在data目錄下面新建一個myid檔案,將剛才添加代碼里面server.n的n寫入myid檔案里面,只寫入數字,
[root@master bin]# vi /home/apps/zookeeper/data/myid
1
~
~
~
~
~
~
~
~
~
~
~
"myid" [New File]
將配置好了的zookeeper檔案傳給另外兩臺服務器
先在另外兩臺服務器上面創建目錄
slave1:
[root@slave1 ~]# mkdir -p /home/apps/zookeeper
slave2:
[root@slave2 ~]# mkdir -p /home/apps/zookeeper
用scp命令進行檔案傳送
注:先設定SSL免密登錄
[root@master ~]#scp -r /home/apps/zookeeper root@slave1:/home/apps/
[root@master ~]#scp -r /home/apps/zookeeper root@slave2:/home/apps/
傳輸完成之后在slave1和slave2上面更改myid檔案,將他里面的數字改為slave1與slave2對應的IP地址(上面更改的zoo.cfg檔案里面添加的服務器主機名或者IP是一 一對應的),
slave1:
[root@slave1 ~]#vi /home/apps/zookeeper/data/myid
2
~
~
~
~
~
~
~
~
~
~
~
data/myid" 1L, 2C
slave2:
[root@slave2 ~]# vi /home/apps/zookeeper/data/myid
3
~
~
~
~
~
~
~
~
~
~
~
data/myid" 1L, 2C
關閉防火墻
關閉防火墻
關閉防火墻
重要的事說三遍,防火墻很重要,非常重要,如果沒有關閉防火墻,很有可能導致啟動不成功,
[root@slave2 ~]# systemctl stop firewalld
[root@slave2 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
首先啟動master主服務器上面的zookeeper,其次slave1和slave2
master:
[root@master ~]# cd /home/apps/zookeeper/bin
[root@master bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/apps/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
slave1:
[root@master ~]# cd /home/apps/zookeeper/bin
[root@master bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/apps/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
slave2:
[root@master ~]# cd /home/apps/zookeeper/bin
[root@master bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/apps/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
在master上面查看是否運行成功
master:
[root@master bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/apps/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
啟動成功
結語:大資料Hadoop筆記 Zookeeper安裝與配置.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/250654.html
標籤:其他
下一篇:雜談 跟編程無關的事情5
