安裝zookeeper的前提是必須有java環境html
# 選擇目錄進行下載安裝 cd /app # 下載zk,能夠去官方網站下載,本身上傳 wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz # 解壓zk tar -zxvf zookeeper-3.4.11.tar.gz # 設定軟鏈接 ln -s zookeeper-3.4.11 zookeeper # 添加兩個目錄,一個是數據目錄,一個是日誌目錄 cd zookeeper/ mkdir data mkdir log # 添加配置文件zoo.cfg在zookeeper的config目錄下面見【2】使用:wq! 進行保存 cd conf cp zoo_sample.cfg zoo.cfg # 修改 dataDir=/app/zookeeper/data dataLogDir=/app/zookeeper/log 目錄 vi zoo.cfg # 進入bin目錄進行啓動 cd ../bin ./zkServer.sh start # 顯示以下:Starting zookeeper ... STARTED即爲成功
# 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=/app/zookeeper/data dataLogDir=/app/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
# 進入zookeeper目錄使用命令,鏈接成功則說明經過 ./zkCli.sh # 啓動zk服務 ./zkServer.sh start # 查看zk服務狀態 ./zkServer.sh status # 中止zk服務 ./zkServer.sh stop # 重啓zk服務 ./zkServer.sh restart
虛擬機1 | 虛擬機2 | 虛擬機3 |
---|---|---|
172.16.48.129 | 172.16.48.130 | 172.16.48.131 |
myid:1 | myid:2 | myid:3 |
# 在每一個虛擬機的dataDir=/app/zookeeper/data目錄下面建立myid文件 cd /app/zookeeper/data # 建立myid文件,內容依照表格1,2,3.使用:wq進保存 vim myid
# 其餘配置同單機配置 # 在zoo.cfg下面添加以下的集羣配置server.myid # 對應其餘實例的內網ip地址 server.1=172.16.48.129:2888:3888 server.2=172.16.48.130:2888:3888 server.3=172.16.48.131:2888:3888
cd /app/zookeeper/bin ./zkServer.sh start ./zkServer.sh status # 分別在狀態中顯示了leader仍是follower
虛擬機1 | 虛擬機2 | 虛擬機3 |
---|---|---|
172.16.48.129 | 172.16.48.130 | 172.16.48.131 |
follower | follower | leader |
按照道理zookeeper高可用3臺的狀況下只要兩臺掛了,集羣就沒法提供服務了。
java
一臺掛了的狀況: 關閉虛擬機3的zookeeper,調用status命令apache
虛擬機1 | 虛擬機2 | 虛擬機3 |
---|---|---|
172.16.48.129 | 172.16.48.130 | 172.16.48.131 |
follower | leader | Error contacting service. It is probably not running. |
虛擬機2轉爲了leader,虛擬機1和2一塊兒提供服務vim
兩臺掛了的狀況: 關閉虛擬機2的zookeeper,調用status命令app
虛擬機1 | 虛擬機2 | 虛擬機3 |
---|---|---|
172.16.48.129 | 172.16.48.130 | 172.16.48.131 |
Error contacting service. It is probably not running. | Error contacting service. It is probably not running. | Error contacting service. It is probably not running. |
從新啓動一臺後,虛擬機1和虛擬機2可以提供服務ide