zookeeperの導入とクラスタテスト

32603 ワード

zookeeperの導入とクラスタテスト

記事転載:https://www.cnblogs.com/rwxwsblog/p/5806075.html

環境


3台の試験機.
  • オペレーティングシステム:centos 7;hostname: c1 ; ip: 192.168.1.80
  • オペレーティングシステム:centos 7;hostname: c2 ; ip: 192.168.1.81
  • オペレーティングシステム:centos 7;hostname: c3 ; ip: 192.168.1.82

  • コメント
     

    ディスクの状態の表示
    ディスクの使用ムラによるシステムの問題を回避するために、ディスクの分散状況を確認します.
    [root@sso conf]# df -lh
    Filesystem               Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root   50G   18G   33G  36% /
    devtmpfs                 7.8G     0  7.8G   0% /dev
    tmpfs                    7.8G     0  7.8G   0% /dev/shm
    tmpfs                    7.8G   65M  7.7G   1% /run
    tmpfs                    7.8G     0  7.8G   0% /sys/fs/cgroup
    /dev/mapper/centos-var   900G  529M  900G   1% /var
    /dev/mapper/centos-home   66G   12G   55G  18% /home
    /dev/sda1                497M  168M  330M  34% /boot
    tmpfs                    1.6G     0  1.6G   0% /run/user/0
    [root@sso conf]#

    /varディレクトリは900 Gで、1%未満しか使用されていないため、後続のすべてのファイルとプログラムをこのディレクトリに保存することができます.もちろん、これはあなたの実際の状況によって決まります.

    zookeeperインストール


    インストール

     mkdir -p /var/download && cd /var/download
     wget -c http://www.eu.apache.org/dist/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
     tar zxvf zookeeper-3.4.6.tar.gz
     mkdir -p /var/local/server
     mv zookeeper-3.4.6 /var/local/server/zookeeper
     cd /var/local/server/zookeeper/conf
     touch zoo.cfg

    編集zoo.cfgには以下の内容が含まれています.
    tickTime=2000
    dataDir=/var/data/zookeeper
    dataLogDir=/var/local/server/zookeeper/log
    clientPort=2181
    initLimit=5
    syncLimit=2
    server.1=c1:2888:3888
    server.2=c2:2888:3888
    server.3=c3:2888:3888

    保存#ホゾン#
    データ・ディレクトリとログ・ディレクトリを作成するには、次の手順に従います.
    mkdir -p /var/data/zookeeper
    mkdir -p /var/local/server/zookeeper/log

    起動を作成しないと、次の例外が表示されます.
    [root@sso conf]# /var/local/server/zookeeper/bin/zkServer.sh status
    JMX enabled by default
    Using config: /var/local/server/zookeeper/bin/../conf/zoo.cfg
    Error contacting service. It is probably not running.

    myidファイル、idとzooを作成します.cfgにおけるシーケンス番号対応echo 1 > /var/data/zookeeper/myid
    注意:
  • 注意、c2およびc3であれば、対応する修正
  • が行われる.
  • c2に変更する:echo 2 > /var/data/zookeeper/myid
  • c3に変更する:echo 3 > /var/data/zookeeper/myid
  • ホストファイルの構成:/etc/hostsを編集し、次の内容を追加します.
    192.168.1.80 c1
    192.168.1.81 c2
    192.168.1.82 c3

    共通コマンド


    開始

    /var/local/server/zookeeper/bin/zkServer.sh start

    ステータスの表示

    /var/local/server/zookeeper/bin/zkServer.sh status

    再起動

    /var/local/server/zookeeper/bin/zkServer.sh restart

    閉じる

    /var/local/server/zookeeper/bin/zkServer.sh stop

    いずれかのクライアントで接続

    /var/local/server/zookeeper/bin/zkCli.sh -server c1:2181

    操作例:

  • 起動:/var/local/server/zookeeper/bin/zkServer.sh start
  • [root@sso conf]# /var/local/server/zookeeper/bin/zkServer.sh start
    JMX enabled by default
    Using config: /var/local/server/zookeeper/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
  • ステータスの表示:/var/local/server/zookeeper/bin/zkServer.sh status
  • [root@sso conf]# /var/local/server/zookeeper/bin/zkServer.sh status
    JMX enabled by default
    Using config: /var/local/server/zookeeper/bin/../conf/zoo.cfg
    Error contacting service. It is probably not running.

    zookeeperが正常に動作していないことがわかりました.
    ログmore zookeeper.outを表示すると、次の異常が発生しました.
    2016-08-25 09:55:54,088 [myid:1] - WARN  [WorkerSender[myid=1]:QuorumCnxManager@382] - Cannot open channel to 2 at election address c2/192.168.1.81:3888
    java.net.NoRouteToHostException: No route to host
            at java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
            at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
            at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
            at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
            at java.net.Socket.connect(Socket.java:579)
            at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:368)
            at org.apache.zookeeper.server.quorum.QuorumCnxManager.toSend(QuorumCnxManager.java:341)
            at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.process(FastLeaderElection.java:449)
            at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.run(FastLeaderElection.java:430)
            at java.lang.Thread.run(Thread.java:745)
    2016-08-25 09:55:54,101 [myid:1] - WARN  [WorkerSender[myid=1]:QuorumCnxManager@382] - Cannot open channel to 3 at election address c3/192.168.1.82:3888
    java.net.ConnectException: Connection refused
            at java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
            at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
            at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
            at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
            at java.net.Socket.connect(Socket.java:579)
            at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:368)
            at org.apache.zookeeper.server.quorum.QuorumCnxManager.toSend(QuorumCnxManager.java:341)
            at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.process(FastLeaderElection.java:449)
            at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.run(FastLeaderElection.java:430)
            at java.lang.Thread.run(Thread.java:745)
    2016-08-25 09:55:54,287 [myid:1] - WARN  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@382] - Cannot open channel to 2 at election address c2/192.168.1.81:3888
    java.net.NoRouteToHostException: No route to host
            at java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
            at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
            at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
            at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
            at java.net.Socket.connect(Socket.java:579)
            at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:368)
            at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:402)
            at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:840)
            at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:762)

    これは2、3号機が正常に配置されていないためです.zoo.cfgにおいて、 を再試行してもよい.
    tickTime=2000
    dataDir=/var/data/zookeeper
    dataLogDir=/var/local/server/zookeeper/log
    clientPort=2181
    initLimit=5
    syncLimit=2
    server.1=c1:2888:3888
    #server.2=c2:2888:3888
    #server.3=c3:2888:3888

    通常の起動効果:
    [root@sso conf]# /var/local/server/zookeeper/bin/zkServer.sh start
    JMX enabled by default
    Using config: /var/local/server/zookeeper/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@sso conf]# /var/local/server/zookeeper/bin/zkServer.sh status
    JMX enabled by default
    Using config: /var/local/server/zookeeper/bin/../conf/zoo.cfg
    Mode: standalone

    テスト


    c 1にc 1のzookeeperを接続する/var/local/server/zookeeper/bin/zkCli.sh -server c1:2181
    [root@sso conf]# /var/local/server/zookeeper/bin/zkCli.sh -server c1:2181
    Connecting to c1:2181
    2016-08-25 10:06:30,235 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
    2016-08-25 10:06:30,244 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=sso.xxx.com
    2016-08-25 10:06:30,244 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.7.0_79
    2016-08-25 10:06:30,250 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
    2016-08-25 10:06:30,251 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/local/jdk1.7.0_79/jre
    2016-08-25 10:06:30,251 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/var/local/server/zookeeper/bin/../build/classes:/var/local/server/zookeeper/bin/../build/lib/*.jar:/var/local/server/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/var/local/server/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/var/local/server/zookeeper/bin/../lib/netty-3.7.0.Final.jar:/var/local/server/zookeeper/bin/../lib/log4j-1.2.16.jar:/var/local/server/zookeeper/bin/../lib/jline-0.9.94.jar:/var/local/server/zookeeper/bin/../zookeeper-3.4.6.jar:/var/local/server/zookeeper/bin/../src/java/lib/*.jar:/var/local/server/zookeeper/bin/../conf:
    2016-08-25 10:06:30,251 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
    2016-08-25 10:06:30,252 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
    2016-08-25 10:06:30,252 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=
    2016-08-25 10:06:30,252 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
    2016-08-25 10:06:30,252 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
    2016-08-25 10:06:30,253 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-327.10.1.el7.x86_64
    2016-08-25 10:06:30,253 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
    2016-08-25 10:06:30,253 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
    2016-08-25 10:06:30,254 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/var/local/server/zookeeper/conf
    2016-08-25 10:06:30,257 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=c1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@194d62f1
    Welcome to ZooKeeper!
    2016-08-25 10:06:30,335 [myid:] - INFO  [main-SendThread(sso.xxx.com:2181):ClientCnxn$SendThread@975] - Opening socket connection to server sso.xxx.com/192.168.1.80:2181. Will not attempt to authenticate using SASL (unknown error)
    JLine support is enabled
    2016-08-25 10:06:30,349 [myid:] - INFO  [main-SendThread(sso.xxx.com:2181):ClientCnxn$SendThread@852] - Socket connection established to sso.xxx.com/192.168.1.80:2181, initiating session
    2016-08-25 10:06:30,412 [myid:] - INFO  [main-SendThread(sso.xxx.com:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server sso.xxx.com/192.168.1.80:2181, sessionid = 0x156bf70d7580000, negotiated timeout = 30000
    
    WATCHER::
    
    WatchedEvent state:SyncConnected type:None path:null
    [zk: c1:2181(CONNECTED) 0]

    ルートノードディレクトリzookeeper_projectを追加create /project zookeeper_project
    [zk: c1:2181(CONNECTED) 0] create /project  zookeeper_project
    Created /project
    [zk: c1:2181(CONNECTED) 1] get /project
    zookeeper_project
    cZxid = 0x2
    ctime = Thu Aug 25 10:09:09 CST 2016
    mZxid = 0x2
    mtime = Thu Aug 25 10:09:09 CST 2016
    pZxid = 0x2
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 17
    numChildren = 0

    マルチマシンクラスタ


    キャンセルcfg中のserver.2server.3の注釈、最後にzoo.cfgプロファイルは次のとおりです.
    tickTime=2000
    dataDir=/var/data/zookeeper
    dataLogDir=/var/local/server/zookeeper/log
    clientPort=2181
    initLimit=5
    syncLimit=2
    server.1=c1:2888:3888
    server.2=c2:2888:3888
    server.3=c3:2888:3888

    c 2およびc 3で同様の操作を実行する
    マルチマシン連携テスト
    c 1実行結果:
    [root@sso conf]# /var/local/server/zookeeper/bin/zkServer.sh status
    JMX enabled by default
    Using config: /var/local/server/zookeeper/bin/../conf/zoo.cfg
    Mode: leader

    c 2実行結果:
    [root@cas zookeeper]# /var/local/server/zookeeper/bin/zkServer.sh status
    JMX enabled by default
    Using config: /var/local/server/zookeeper/bin/../conf/zoo.cfg
    Mode: follower

    c 3実行結果:
    [root@localhost zookeeper]# /var/local/server/zookeeper/bin/zkServer.sh status
    JMX enabled by default
    Using config: /var/local/server/zookeeper/bin/../conf/zoo.cfg
    Mode: follower

    同期テスト


    c 1で実行:create /c1project c1projecttestおよびget /c1project
    [zk: c1:2181(CONNECTED) 2] create /c1project c1projecttest
    Created /c1project
    [zk: c1:2181(CONNECTED) 1] get /c1project
    c1projecttest
    cZxid = 0x100000013
    ctime = Thu Aug 25 11:03:49 CST 2016
    mZxid = 0x100000013
    mtime = Thu Aug 25 11:03:49 CST 2016
    pZxid = 0x100000013
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 13
    numChildren = 0

    c 2で同期結果を表示するには、次の手順に従います.get /c1project
    [zk: c2:2181(CONNECTED) 0] get /c1project
    c1projecttest
    cZxid = 0x100000013
    ctime = Thu Aug 25 11:03:49 CST 2016
    mZxid = 0x100000013
    mtime = Thu Aug 25 11:03:49 CST 2016
    pZxid = 0x100000013
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 13
    numChildren = 0

    c 3で同期結果を表示するには、次の手順に従います.
    [zk: c3:2181(CONNECTED) 4] get /c1project
    c1projecttest
    cZxid = 0x100000013
    ctime = Thu Aug 25 11:03:49 CST 2016
    mZxid = 0x100000013
    mtime = Thu Aug 25 11:03:49 CST 2016
    pZxid = 0x100000013
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 13
    numChildren = 0

    zookeeperクラスタ構成が成功したことを示します.

    ノードからプライマリノードへのプロアクティブなアップグレードと管理

    leaderfollowerの自動切替は、c 1がleaderであることを前に知っていたが、leader downが落ちた後、ノードからleaderにアップグレードされたかどうか.
    c 1で実行/var/local/server/zookeeper/bin/zkServer.sh stop
    c 2上のステータスの表示
    [root@cas ~]# /var/local/server/zookeeper/bin/zkServer.sh status
    JMX enabled by default
    Using config: /var/local/server/zookeeper/bin/../conf/zoo.cfg
    Mode: follower

    c 2は依然としてスレーブノードであることがわかる
    c 3を見てみましょう
    [root@localhost ~]# /var/local/server/zookeeper/bin/zkServer.sh status
    JMX enabled by default
    Using config: /var/local/server/zookeeper/bin/../conf/zoo.cfg
    Mode: leader

    このときc 3はプライマリノードにアップグレードされる
    c 1を再起動し、ステータスを表示
    [root@sso conf]# /var/local/server/zookeeper/bin/zkServer.sh start
    JMX enabled by default
    Using config: /var/local/server/zookeeper/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@sso conf]#
    [root@sso conf]# /var/local/server/zookeeper/bin/zkServer.sh status
    JMX enabled by default
    Using config: /var/local/server/zookeeper/bin/../conf/zoo.cfg
    Mode: follower

    c 1がスレーブノードとして存在することが分かった.
    オリジナル作者:https://www.cnblogs.com/rwxwsblog/p/5806075.html