zookeeper的安裝和使用

文章做者:foochane html

原文連接:https://foochane.cn/article/2019062601.htmlnode

zookeeper數據存儲形式 zookeeper安裝 zookeeper命令行客戶端的使用

1 zookeeper數據存儲形式

zookeeper中對用戶的數據採用kv形式存儲shell

key:是以路徑的形式表示的,各key之間有父子關係,好比 / 是頂層keyapache

用戶建的key只能在/ 下做爲子節點,好比建一個key: /aa 這個key能夠帶value數據bash

也能夠建一個key/bbssh

也能夠建多個key/aa/xx ide

zookeeper中,對每個數據key,稱做一個znodeoop

2 znode類型

zookeeper中的znode有多種類型:ui

  • 一、PERSISTENT 持久的:建立者就算跟集羣斷開聯繫,該類節點也會持久存在與zk集羣中
  • 二、EPHEMERAL 短暫的:建立者一旦跟集羣斷開聯繫,zk就會將這個節點刪除
  • 三、SEQUENTIAL 帶序號的:這類節點,zk會自動拼接上一個序號,並且序號是遞增的

組合類型:this

  • PERSISTENT :持久不帶序號
  • EPHEMERAL :短暫不帶序號
  • PERSISTENTSEQUENTIAL :持久且帶序號
  • EPHEMERALSEQUENTIAL :短暫且帶序號

3 安裝zookeeper

解壓安裝包 zookeeper-3.4.6.tar.gz

修改conf/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/bigdata/data/zkdata
# 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:2888:3888
server.2=Slave01:2888:3888
server.3=Slave02:2888:3888

對3臺節點,都建立目錄 /usr/local/bigdata/data/zkdata

對3臺節點,在工做目錄中生成myid文件,但內容要分別爲各自的id1,2,3

Master上:   echo 1 > /usr/local/bigdata/data/zkdata/myid
Slave01上:  echo 2 > /usr/local/bigdata/data/zkdata/myid
Slave02上:  echo 3 > /usr/local/bigdata/data/zkdata/myid

4 啓動zookeeper集羣

zookeeper沒有提供自動批量啓動腳本,須要手動一臺一臺地起zookeeper進程
在每一臺節點上,運行命令:

$ bin/zkServer.sh start

啓動後,用jps應該能看到一個進程:QuorumPeerMain

查看狀態

$ bin/zkServer.sh status

5 編寫啓動腳本zkmanage.sh

zookeeper沒有提供批量腳本,不能像hadoop同樣在一臺機器上同時啓動全部節點,能夠本身編寫腳本批量啓動。

#!/bin/bash
for host in Master Slave01 Slave02
do
echo "${host}:${1}ing....."
ssh $host "source ~/.bashrc;/usr/local/bigdata/zookeeper-3.4.6/bin/zkServer.sh $1"
done

sleep 2

for host in Master Slave01 Slave02
do
ssh $host "source ~/.bashrc;/usr/local/bigdata/zookeeper-3.4.6/bin/zkServer.sh status"
done
  • $1 :指接收第一個參數

運行命令:

sh zkmanage.sh start #啓動
sh zkmanage.sh stop  #中止

6 zookeeper命令行客戶端

啓動本地客戶端:

$ bin/zkCli.sh

啓動其餘機器的客戶端:

$ bin/zkCli.sh -server Master:2181

基本命令:

  • 查看幫助:help
  • 查看目錄:ls /
  • 查看節點數據:get /zookeeper
  • 插入數據: create /節點 數據 , 如:create /aa hello
  • 更改某節點數據: set /aa helloworld
  • 刪除數據:rmr /aa/bb
  • 註冊監聽:get /aa watch -->數據發生改變會通知 ; ls /aa watch -->目錄發現改變也會通知
相關文章
相關標籤/搜索