前述:
近日需要用到zookeeper叢集,硬體缺少隻能以軟體來補。在一台Centos7的機器上部署多個zookeeper的執行個體,這裡進行記 錄,或許以後再遇到叢集搭建,不必耗費時間去搜羅資料。
環境:
Centos7+java1.8。
準備:
zookeeper-3.4.14.tar.gz安裝包,下載下傳位址http://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/
檔案結構
|----opt
|---------zookeeper
|-----------------------zk1
|-----------------------zk2
|-----------------------zk3
操作一:
1、使用管理者權限操作,避免出現權限問題。
[[email protected] ~]$ su root
2、進入opt目錄,如果沒有此檔案請自行建立
[[email protected] administrator]# cd /opt/
3、建立zookeeper目錄
[[email protected] opt]# mkdir zookeeper
4、進入到zookeeper目錄
[[email protected] opt]# cd zookeeper
5、将zookeeper-3.4.14.tar.gz檔案放置到zookeeper路徑下,并解壓
[[email protected] zookeeper]# tar -xvf zookeeper-3.4.14.tar.gz
6、把解壓後的檔案zookeeper-3.4.14重命名為zk1
[[email protected] zookeeper]# mv zookeeper-3.4.14 ./zk1
7、在zk1的目錄下建立zkdata存放執行個體的myid
[[email protected] zookeeper]# cd zk1
[[email protected] zk1]#mkdir zkdata
[[email protected] zk1]#vi ./zkdata/myid
myid的檔案内容輸入數字1,儲存即可。此數值在叢集中是唯一的不可重複(數值範圍1-255)
8、建立zoo.cfg配置檔案
[[email protected] zookeeper]# cp /opt/zookeeper/zk1/conf/zoo_sample.cfg /opt/zookeeper/zk1/conf/zoo.cfg
9、配置zoo.cfg檔案
[[email protected] zookeeper]# vi /opt/zookeeper/zk1/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=/opt/zookeeper/zk1/zkdata
# the port at which the clients will connect
clientPort=2191
# 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=127.0.0.1:20881:30881
server.2=127.0.0.1:20882:30882
server.3=127.0.0.1:20883:30883
釋:
dataDir=/opt/zookeeper/zk1/zkdata #表示zookeeper服務的ID資料。
clientPort=2191 #用戶端的端口号。
server.1=127.0.0.1:20881:30881
server.2=127.0.0.1:20882:30882
server.3=127.0.0.1:20883:30883
叢集服務至少為三個,這裡三個服務的(服務名稱=服務位址:監聽端口:選舉端口)
此處服務位址全部為127.0.0.1,因為是在一台機器上部署3個執行個體;如果在多台機器部署,要根據實際ip位址配置。
操作二:
前面已經配置好一個執行個體,在此基礎上配置第二、第三個執行個體就會輕松些。具體操作這裡簡化一些。
1、根據zk1複制出zk2和zk3
[[email protected] zk1]# cd /opt/zookeeper
[[email protected] zookeeper]# cp /opt/zookeeper/zk1 /opt/zookeeper/zk2
[[email protected] zookeeper]# cp /opt/zookeeper/zk1 /opt/zookeeper/zk3
2、修改zk2和zk3的zoo.cfg檔案
[[email protected] zookeeper]# vi /opt/zookeeper/zk2/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=/opt/zookeeper/zk2/zkdata
# the port at which the clients will connect
clientPort=2192
# 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=127.0.0.1:20881:30881
server.2=127.0.0.1:20882:30882
server.3=127.0.0.1:20883:30883
配置zk3的zoo.cfg
[[email protected] zookeeper]# vi /opt/zookeeper/zk3/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=/opt/zookeeper/zk3/zkdata
# the port at which the clients will connect
clientPort=2193
# 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=127.0.0.1:20881:30881
server.2=127.0.0.1:20882:30882
server.3=127.0.0.1:20883:30883
3、修改zk2和zk3的myid檔案
[[email protected] zookeeper]# vi /opt/zookeeper/zk2/zkdata/myid
将數值變更為2
[[email protected] zookeeper]# vi /opt/zookeeper/zk3/zkdata/myid
将數值變更為3
截止目前,三個zookeeper執行個體已經配置完畢,但還有後續工作需要完成。
防火牆設定
1、zookeeper叢集的端口較多,如果防火牆不開啟所涉及的端口,服務會出現問題。在配置過程中我們已經得知已經涉及9個端口,下面我們逐個開啟這些端口。
[[email protected] zookeeper]# firewall-cmd --permanent --zone=public --add-port=2191-2193/tcp
[[email protected] zookeeper]# firewall-cmd --permanent --zone=public --add-port=20881-20883/tcp
[[email protected] zookeeper]# firewall-cmd --permanent --zone=public --add-port=30881-30883/tcp
2、重新加載防火牆
[[email protected] zookeeper]#firewall-cmd --reload
3、 檢視防火牆中這9個端口是否狀态
[[email protected] zookeeper]# firewall-cmd --list-ports
20881-20883/tcp 30881-30883/tcp 2191-2193/tcp
批處理啟動
1、為日常操作友善,特意編寫一個批處理檔案啟動執行個體
[[email protected] zookeeper]# vi zkStart.sh
内容如下:
#!/bin/sh
cd /opt/zookeeper/zk1/bin
./zkServer.sh start ../conf/zoo.cfg
cd /opt/zookeeper/zk2/bin
./zkServer.sh start ../conf/zoo.cfg
cd /opt/zookeeper/zk3/bin
./zkServer.sh start ../conf/zoo.cfg
2、運作批處理檔案來啟動這三個執行個體
[[email protected] zookeeper]# vi ./zkStart.sh
3、運作的結果
ZooKeeper JMX enabled by default
Using config: ../conf/zoo.cfg
Starting zookeeper ... STARTED
ZooKeeper JMX enabled by default
Using config: ../conf/zoo.cfg
Starting zookeeper ... STARTED
ZooKeeper JMX enabled by default
Using config: ../conf/zoo.cfg
Starting zookeeper ... STARTED
可以看出這三個zookeeper執行個體已經啟動成功。
4、再來看看啟動叢集狀态
[root@localhost zookeeper]# ps -ef|grep zookeeper|grep -v grep|wc -l
能看到控制台會出現數字3
至此已經配置成功,完結!