Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 在CentOS 7 上安裝Zookeeper集群

在CentOS 7 上安裝Zookeeper集群

編輯:關於android開發

在CentOS 7 上安裝Zookeeper集群


測試機上需要安裝java軟件
$ rpm -qa|grep java
$ sudo yum install -y java-1.8.0-openjdk.x86_64
$ java -version
openjdk version "1.8.0_65"
OpenJDK Runtime Environment (build 1.8.0_65-b17)
OpenJDK 64-Bit Server VM (build 25.65-b01, mixed mode)

1、創建用戶:bjrenrui0001~0003
sudo groupadd zookeeper
sudo useradd -g zookeeper zookeeper
echo "zookeeper@1234"|sudo passwd zookeeper —stdin

2、創建工作目錄
bjrenrui0001:
sudo mkdir /yanfa/mq
sudo ln -s /yanfa/mq /mq
sudo chown -R dreamjobs.dreamjobs /mq

bjrenrui0002~0003:
sudo mkdir mq
sudo chown dreamjobs.dreamjobs mq
sudo ln -s /home/backupfile/mq /mq
sudo chown dreamjobs.dreamjobs mq

1. 單機模式(Standalone mode)
下載,解壓
cd /mq
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.7/zookeeper-3.4.7.tar.gz
tar zxvf zookeeper-3.4.7.tar.gz -C /mq
ln -s /mq/zookeeper-3.4.7 zookeeper

啟動
默認就是單機模式
cd /mq/zookeeper/
cp ./conf/zoo_sample.cfg ./conf/zoo.cfg
sh ./bin/zkServer.sh start

防火牆開啟2181端口

檢查服務狀態:
[dreamjobs@bjrenrui0001 bin]$ sh zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Mode: standalone

使用java 客戶端連接ZooKeeper
./bin/zkCli.sh -server 127.0.0.1:2181
然後就可以使用各種命令了,跟文件操作命令很類似,輸入help可以看到所有命令。

關閉
sh bin/zkServer.sh stop

2 分布式模式(Replicated mode)
在生產環境中,要配置成分布式模式,才能發揮威力。
ZooKeeper集群一般被稱為ZooKeeper ensemble(集成),或者 quorum(法定人數).

准備3台機器

假設有三台機器,hostname和ip對應關系是:
192.168.100.200 bjrenrui0001
192.168.100.201 bjrenrui0002
192.168.100.202 bjrenrui0003

ZooKeeper不存在明顯的master/slave關系,各個節點都是服務器,leader掛了,會立馬從follower中選舉一個出來作為leader.
由於沒有主從關系,也不用配置SSH無密碼登錄了,各個zk服務器是自己啟動的,互相之間通過TCP端口來交換數據。

創建日志和數據目錄:
bjrenrui0001~0003
cd /mq/zookeeper
mkdir -p zookeeperdatadir/{logs,data}

修改bjrenrui0001的配置文件conf/zoo.cfg:
[dreamjobs@bjrenrui0001 conf]$ grep -vE '^($|#)' zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/mq/zookeeper/zookeeperdatadir/data
dataLogDir=/mq/zookeeper/zookeeperdatadir/logs
clientPort=2181
server.1=bjrenrui0001:2888:3888
server.2=bjrenrui0002:2888:3888
server.3=bjrenrui0003:2888:3888
修改完後拷貝到 bjrenrui0002,以及 bjrenrui0003上

myid文件:
要在每台機器的dataDir下,新建一個myid文件,裡面存放一個數字,用來標識當前主機
bjrenrui0001: echo "1" >> /mq/zookeeper/zookeeperdatadir/data/myid
bjrenrui0002: echo "2" >> /mq/zookeeper/zookeeperdatadir/data/myid
bjrenrui0003: echo "3" >> /mq/zookeeper/zookeeperdatadir/data/myid

每台機器上啟動zookeeper服務:
bjrenrui0001: sh /mq/zookeeper/bin/zkServer.sh start
bjrenrui0002: sh /mq/zookeeper/bin/zkServer.sh start
bjrenrui0003: sh /mq/zookeeper/bin/zkServer.sh start
每台機器上停止zookeeper服務:
bjrenrui0001: sh /mq/zookeeper/bin/zkServer.sh stop
bjrenrui0002: sh /mq/zookeeper/bin/zkServer.sh stop
bjrenrui0003: sh /mq/zookeeper/bin/zkServer.sh stop
因為3個節點的啟動是有順序的,所以在陸續啟動三個節點的時候,前面先啟動的節點連接未啟動的節點的時候會報出一些錯誤,可以忽略。

防火牆開啟2181端口:
sudo vi /etc/sysconfig/iptables
-A INPUT -p tcp -s 192.168.100.0/24 -j ACCEPT
sudo systemctl restart iptables.service

查看狀態
[dreamjobs@bjrenrui0001 conf]$ gmbjyf.sh bjyfnbserver "cat /mq/zookeeper/zookeeperdatadir/data/myid"
bjrenrui0001
1
bjrenrui0002
2
bjrenrui0003
3
$ gmbjyf.sh bjyfnbserver "sh /mq/zookeeper/bin/zkServer.sh restart"
bjrenrui0001
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
bjrenrui0002
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
bjrenrui0003
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper … STARTED

$ gmbjyf.sh bjyfnbserver "sh /mq/zookeeper/bin/zkServer.sh status"
bjrenrui0001
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Mode: follower
bjrenrui0002
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Mode: leader
bjrenrui0003
ZooKeeper JMX enabled by default
Using config: /mq/zookeeper/bin/../conf/zoo.cfg
Mode: follower

使用java客戶端連接ZooKeeper集群
找一台機器,解壓zookeeper壓縮包,不用配置,就可以使用java客戶端連接ZooKeeper集群中的任意一台服務器了。
$ sh /mq/zookeeper/bin/zkCli.sh -server bjrenrui0001:2181
$ sh /mq/zookeeper/bin/zkCli.sh -server bjrenrui0002:2181
$ sh /mq/zookeeper/bin/zkCli.sh -server bjrenrui0003:2181

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved