天天看點

zookeeper搭建叢集(單機僞叢集)搭建步驟

zookeeper搭建單機僞叢集

  • 搭建步驟

搭建步驟

  • 所需安裝包

本次示範的zookeeper版本是:zookeeper-3.4.14。

  • 步驟1、在放置zookeeper的目錄下,如“/usr/app/zookeeper”目錄,建立zookeeper-colony,然後先建立子目錄zk1,然後在zk1中建立資料存放目錄data和日志存放目錄logs。
## 建立zk叢集目錄後進入在建立節點zk1目錄,之後在建立data和logs目錄
mkdir zookeeper-colony
cd zookeeper-colony/
mkdir zk1
cd zk1
mkdir data
mkdir logs
           
  • 步驟2、複制單機節點中zookeeper的conf目錄中的zoo.cfg檔案到zk1下并重命名為zoo1.cfg,然後添加zk1節點的配置資訊。
## 拷貝單機機安裝目錄下zoo.cfg配置檔案,并重指令(當然也可不必這麼做),然後編輯修改。
cp /usr/app/zookeeper/zookeeper-3.4.14/conf/zoo.cfg /usr/app/zookeeper/zookeeper-colony/zk1 –a
mv zoo.cfg zoo1.cfg
vim zoo1.cfg
           

配置内容如示。

zoo1.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/app/zookeeper/zookeeper-colony/zk1/data
dataLogDir=/usr/app/zookeeper/zookeeper-colony/zk1/logs

# the port at which the clients will connect
## 修改預設端口号配置為單機叢集端口号,真實多台主機可以不改.
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.1=localhost:2889:3889
server.2=localhost:2890:3890
server.3=localhost:2891:3891
           

zookeeper 的三個端口作用:

1)2181:對 client 端提供服務

2)2888:叢集内機器通信使用

3)3888:選舉 leader 使用

  • 步驟3、在zk1中的data目錄下建立檔案myid,添加内容1。
## 建立myid檔案,可以直接使用 vim data/myid此指令會檢查是否存在這個檔案,沒有時建立
touch data/myid
vim data/myid
## zk1中的内容為1,zk2中的内容為2,zk3中的内容為3,和zoo1.cfg中的server.*中的 “*”對應
           
  • 步驟4、複制兩份zk1,命名為zk2、zk3、然後修改zoo.cfg檔案中的端口、資料位置、日志位置,以及myid中序号,分别為2、3。

zoo2.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/app/zookeeper/zookeeper-colony/zk2/data
dataLogDir=/usr/app/zookeeper/zookeeper-colony/zk2/logs

# the port at which the clients will connect
## 修改預設端口号配置為單機叢集端口号,真實多台主機可以不改.
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.1=localhost:2889:3889
server.2=localhost:2890:3890
server.3=localhost:2891:3891
           

zoo3.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/app/zookeeper/zookeeper-colony/zk3/data
dataLogDir=/usr/app/zookeeper/zookeeper-colony/zk3/logs

# the port at which the clients will connect
## 修改預設端口号配置為單機叢集端口号,真實多台主機可以不改.
clientPort=2184
# 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=localhost:2889:3889
server.2=localhost:2890:3890
server.3=localhost:2891:3891
           
  • 步驟5、在系統環境中配置zookeeper的路徑,友善指令啟動輸入(當然也可不做此步)。在

    /etc/profile

    檔案中添加
# 編輯檔案
vim /etc/profile

# 添加zk安裝位址
# zookeeper安裝目錄
export ZOOKEEPER_HOME=/usr/app/zookeeper/zookeeper-3.4.14
PATH=${ZOOKEEPER_HOME}/bin:$PATH
export PATH

# 重新開機環境變量
source /etc/profile
           
  • 步驟6、分别啟動三個zookeeper
# 啟動3個zookeeper服務
zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg

# 檢視每個zookeeper對應的角色
zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg

# 停止zookeeper服務
zkServer.sh stop /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh stop /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh stop /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg
           

啟動後檢視zk狀态,控制台輸入如下。

[[email protected] ~]# zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg 
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
Starting zookeeper ... STARTED
[[email protected] ~]# zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg 
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
Starting zookeeper ... STARTED
[[email protected] ~]# zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg 
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg
Starting zookeeper ... STARTED
[[email protected] ~]# zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg 
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
Mode: follower
[[email protected] ~]# zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg 
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
Mode: leader
[[email protected] ~]# zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg 
ZooKeeper JMX enabled by default
Using config: /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg
Mode: follower
           
  • 步驟7、ZooKeeper指令行驗證使用。(可選)
# 連接配接伺服器 zkCli -server IP:PORT
zkCli.sh -server 127.0.0.1:2182
# 幫助指令
help
# 擷取根節點清單,預設會有一個zookeeper節點,節點清單就像linux目錄一樣,根節點為/
ls /
# 建立znode并關聯值, create /節點名稱 “值”
create /myZnode "my znode"
# 擷取znode
get /myZnode
# 修改znode
set /myZnode "my znode value string"
# 删除單個znode
delete /myZnode
# 遞歸删除(删除myZnode的子節點和myZnode節點)
rmr /myZnode
# 登出
quit
           
  • 步驟8、外部通路端口開放。
# 開放配置zk端口2182、2183、2184
firewall-cmd --add-port=2182/tcp --permanent
firewall-cmd --add-port=2183/tcp --permanent
firewall-cmd --add-port=2184/tcp --permanent

# 重新開機服務
firewall-cmd --reload

# 檢視開放端口
firewall-cmd --list-port
           
  • 步驟10、在zookeeper-colony目錄下編寫快速啟動、重新開機、停止運作腳本,并且為這些檔案賦予可執行權限。(檔案名字尾為.sh)
# 編寫啟動腳本
vim zk-all-start.sh
## 内容如下。
zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh start /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg

# 編寫重新開機腳本
vim zk-all-restart.sh
## 内容如下。
zkServer.sh restart /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh restart /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh restart /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg

# 編寫停止腳本
vim zk-all-stop.sh
## 内容如下。
zkServer.sh stop /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh stop /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh stop /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg

# 編寫檢視腳本
vim zk-all-status.sh
## 内容如下。
zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk1/zoo1.cfg
zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk2/zoo2.cfg
zkServer.sh status /usr/app/zookeeper/zookeeper-colony/zk3/zoo3.cfg
           

為編寫檔案賦權可行指令:

chmod 755 zk-all-*

,而後測試驗證即可。

以上就安裝單機僞叢集結束。