6. zk集群安装

作者: 光小月 | 来源:发表于2019-06-11 14:39 被阅读1次

    文章参考: https://segmentfault.com/a/1190000011797152

    zk 多节点集群安装

    伪分布式 , 两种方式,一种多份程序,多份配置信息; 另一种,一份程序,多份配置信息

    方案一:

    1. 环境:

    /usr/zkCluster/zk1
    /usr/zkCluster/zk2
    /usr/zkCluster/zk3

    2.编辑配置文件

    [root@localhost conf]# vi /usr/zkCluster/zk1/conf/zoo.cfg
    tickTime=2000
    dataDir=/usr/zkCluster/zk1/data
    clientPort=2182
    server.1=192.168.56.103:2886:2996
    server.2=192.168.56.103:2887:2997
    server.3=192.168.56.103:2888:2998
    
    [root@localhost conf]# vi /usr/zkCluster/zk2/conf/zoo.cfg
    tickTime=2000
    dataDir=/usr/zkCluster/zk2/data
    clientPort=2183
    server.1=192.168.56.103:2886:2996
    server.2=192.168.56.103:2887:2997
    server.3=192.168.56.103:2888:2998
    
    [root@localhost conf]# vi /usr/zkCluster/zk3/conf/zoo.cfg
    tickTime=2000
    dataDir=/usr/zkCluster/zk3/data
    clientPort=2184
    server.1=192.168.56.103:2886:2996
    server.2=192.168.56.103:2887:2997
    server.3=192.168.56.103:2888:2998
    
    

    配置说明:

    initLimit:这个参数是最初连接到领导者的跟随者的超时时间,以毫秒数表示 syncLimit:指定追随者与领导者同步的超时时间
    这两个超时是以心跳时间为单位指定的。 因此,在我们的示例中,initLimit的超时时间为2000毫秒为一个心跳一共五次心跳,或10秒钟。
    
    server.id=host:port:port格式的上述示例中的其他三个条目是构成quorum的服务器列表。.id标识符是一个数字,用于具有quorum主机名的服务器。
    在我们的示例配置中,为zoo1仲裁成员主机分配了一个标识符1。
    
    需要在该服务器的数据目录中myid的文件中指定标识符。 重要的是,myid文件应该包含仅包含该服务器ID的文本(ASCII)的单行。
    该集合中的id必须是唯一的,并且应该具有介于1到255之间的值。
    
    还有,我们在每个服务器主机名后面有两个端口号:2888和3888,这里解释说明:
    
    端口号2886,主要用于quorum中的对等通信,例如将追随者与领导者联系起来。一个追随者使用这个端口打开一个到领导者的TCP连接。
    端口号2996,用于领导者选举,以防新领导者出现在仲裁中。由于所有的通信都发生在TCP上,因此需要第二个端口来响应仲裁内部的领导选举。
    

    注意事项:
    第二个端口port 每个文件是不一致的,跟分布集群不同
    不要写成

    server.1=192.168.56.103:2886:2999
    server.2=192.168.56.103:2887:2999
    server.3=192.168.56.103:2888:2999
    

    3. 编写myid

    [root@localhost conf]#echo "1"> vi /usr/zkCluster/zk1/data/myid
    [root@localhost conf]#echo "2"> vi /usr/zkCluster/zk2/data/myid
    [root@localhost conf]#echo "3"> vi /usr/zkCluster/zk3/data/myid
    

    4. 启动

    [root@localhost conf]#/usr/zkCluster/zk1/bin/zkServer.sh start
    [root@localhost conf]#/usr/zkCluster/zk2/bin/zkServer.sh start
    [root@localhost conf]#/usr/zkCluster/zk3/bin/zkServer.sh start
    

    5.测试

    ## 检测是否成功启动
    [root@localhost conf]# /usr/zkCluster/zk1/bin/zkServer.sh status 
    ZooKeeper JMX enabled by default
    Client port found: 2182. Client address: localhost.
    Mode: follower 
    [root@localhost conf]# /usr/zkCluster/zk2/bin/zkServer.sh status 
    ZooKeeper JMX enabled by default
    Client port found: 2183. Client address: localhost.
    Mode: leader
    [root@localhost conf]# /usr/zkCluster/zk3/bin/zkServer.sh status 
    ZooKeeper JMX enabled by default
    Client port found: 2184. Client address: localhost.
    Mode: follower
    ## 连接客户端
    [root@localhost zkCluster]# /usr/zkCluster/zk3/bin/zkCli.sh  -server localhost:2182,localhost:2183,localhost:2184
    Connecting to localhost:2182,localhost:2183,localhost:2184
    ......
    
    
    

    方案二:

    这里以shell 编程的方式呈现

    1. 环境

    /usr/zkCluster/zk4
    

    2. 配置信息

    [root@localhost zkCluster]#vi  /usr/zkCluster/zk_conf.sh
    -----------------------------
    #!/usr/bin/env bash
    
    # 创建data与myid
    mkdir /usr/zkCluster/zk4/data/zk1/
    mkdir /usr/zkCluster/zk4/data/zk2/
    mkdir /usr/zkCluster/zk4/data/zk3/
    
    echo '1'>/usr/zkCluster/zk4/data/zk1/myid
    echo '2'>/usr/zkCluster/zk4/data/zk2/myid
    echo '3'>/usr/zkCluster/zk4/data/zk3/myid
    
    # create cfg
    
    rm -rf /usr/zkCluster/zk4/conf/zoo1.cfg
    
    echo "tickTime=2000
    dataDir=/usr/zkCluster/zk4/data/zk1
    clientPort=2182
    initLimit=5
    syncLimit=2
    server.1=192.168.56.103:2886:2996
    server.2=192.168.56.103:2887:2997
    server.3=192.168.56.103:2888:2998" >> /usr/zkCluster/zk4/conf/zoo1.cfg
    
    
    rm -rf /usr/zkCluster/zk4/conf/zoo2.cfg
    
    echo "tickTime=2000
    dataDir=/usr/zkCluster/zk4/data/zk2
    clientPort=2183
    initLimit=5
    syncLimit=2
    server.1=192.168.56.103:2886:2996
    server.2=192.168.56.103:2887:2997
    server.3=192.168.56.103:2888:2998" >> /usr/zkCluster/zk4/conf/zoo2.cfg
    
    rm -rf /usr/zkCluster/zk4/conf/zoo3.cfg
    
    echo "tickTime=2000
    dataDir=/usr/zkCluster/zk4/data/zk3
    clientPort=2184
    initLimit=5
    syncLimit=2
    server.1=192.168.56.103:2886:2996
    server.2=192.168.56.103:2887:2997
    server.3=192.168.56.103:2888:2998" >> /usr/zkCluster/zk4/conf/zoo3.cfg
    -----------------------------
    
    [root@localhost zkCluster]# chmod +x  /usr/zkCluster/zk_conf.sh
    [root@localhost zkCluster]# ./zk4_conf.sh
    
    

    2. 编写运行信息

    [root@localhost zkCluster]# vi  /usr/zkCluster/zk4_autorun.sh
    ----------------------
    #!/usr/bin/env bash
    /usr/zkCluster/zk4/bin/zkServer.sh start /usr/zkCluster/zk4/conf/zoo1.cfg 
    /usr/zkCluster/zk4/bin/zkServer.sh start /usr/zkCluster/zk4/conf/zoo2.cfg 
    /usr/zkCluster/zk4/bin/zkServer.sh start /usr/zkCluster/zk4/conf/zoo3.cfg 
    ----------------------
    [root@localhost zkCluster]# chmod +x  /usr/zkCluster/zk4_autorun.sh
    [root@localhost zkCluster]# ./zk4_autorun.sh
    
    

    3. 编写查看状态信息

    [root@localhost zkCluster]# vi  /usr/zkCluster/zk4_status.sh 
    
    ------------------------------------------
    
    #!/usr/bin/env bash
    /usr/zkCluster/zk4/bin/zkServer.sh status /usr/zkCluster/zk4/conf/zoo1.cfg
    /usr/zkCluster/zk4/bin/zkServer.sh status /usr/zkCluster/zk4/conf/zoo2.cfg
    /usr/zkCluster/zk4/bin/zkServer.sh status /usr/zkCluster/zk4/conf/zoo3.cfg
    ------------------------------------------
    
    [root@localhost zkCluster]#chmod +x  /usr/zkCluster/zk4_status.sh
    [root@localhost zkCluster]#./zk4_status.sfh
    ZooKeeper JMX enabled by default
    Using config: /usr/zkCluster/zk4/conf/zoo1.cfg
    Client port found: 2182. Client address: localhost.
    Mode: follower
    ZooKeeper JMX enabled by default
    Using config: /usr/zkCluster/zk4/conf/zoo2.cfg
    Client port found: 2183. Client address: localhost.
    Mode: leader
    ZooKeeper JMX enabled by default
    Using config: /usr/zkCluster/zk4/conf/zoo3.cfg
    Client port found: 2184. Client address: localhost.
    Mode: follower
    
    

    4. 基于java的shell 连接客户端

    [root@localhost zkCluster]#  /usr/zkCluster/zk4/bin/zkCli.sh  -server localhost:2182,localhost:2183,localhost:2184
    Connecting to localhost:2182,localhost:2183,localhost:2184
    2019-06-11 14:34:13,033 [myid:] - INFO  [main:Environment@109] - Client environment:zookeeper.version=3.5.5-390fe37ea45dee01bf87dc1c042b5e3dcce88653, built on 06/04/2019 06:11 GMT
    2019-06-11 14:34:13,037 [myid:] - INFO  [main:Environment@109] - Client environment:host.name=localhost
    2019-06-11 14:34:13,037 [myid:] - INFO  [main:Environment@109] - Client environment:java.version=1.8.0_141
    2019-06-11 14:34:13,039 [myid:] - INFO  [main:Environment@109] - Client environment:java.vendor=Oracle Corporation
    2019-06-11 14:34:13,040 [myid:] - INFO  [main:Environment@109] - Client environment:java.home=/usr/java/jdk1.8.0_141/jre
    2019-06-11 14:34:13,040 [myid:] - INFO  [main:Environment@109] - Client environment:java.class.path=/usr/zkCluster/zk4/bin/../zookeeper-server/target/classes:/usr/zkCluster/zk4/bin/../build/classes:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/zookeeper-jute-3.5.5.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/token-provider-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/spotbugs-annotations-3.1.9.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/slf4j-log4j12-1.7.25.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/slf4j-api-1.7.25.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/nimbus-jose-jwt-4.41.2.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/netty-all-4.1.29.Final.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/mockito-all-1.8.5.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/log4j-1.2.17.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerby-xdr-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerby-util-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerby-pkix-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerby-config-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerby-asn1-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerb-util-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerb-simplekdc-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerb-server-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerb-identity-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerb-crypto-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerb-core-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerb-common-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerb-client-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/kerb-admin-1.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/junit-4.12.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/jsr305-3.0.2.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/json-smart-2.3.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/json-simple-1.1.1.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/jline-2.11.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/jetty-util-9.4.17.v20190418.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/jetty-servlet-9.4.17.v20190418.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/jetty-server-9.4.17.v20190418.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/jetty-security-9.4.17.v20190418.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/jetty-io-9.4.17.v20190418.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/jetty-http-9.4.17.v20190418.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/jcip-annotations-1.0-1.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/javax.servlet-api-3.1.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/jackson-databind-2.9.8.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/jackson-core-2.9.8.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/jackson-annotations-2.9.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/hamcrest-core-1.3.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/hamcrest-all-1.3.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/commons-io-2.6.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/commons-collections-3.2.2.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/commons-cli-1.2.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/bcprov-jdk15on-1.60.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/bcpkix-jdk15on-1.60.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/audience-annotations-0.5.0.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/asm-5.0.4.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/target/lib/accessors-smart-1.2.jar:/usr/zkCluster/zk4/bin/../build/lib/*.jar:/usr/zkCluster/zk4/bin/../lib/*.jar:/usr/zkCluster/zk4/bin/../zookeeper-*.jar:/usr/zkCluster/zk4/bin/../zookeeper-server/src/main/resources/lib/*.jar:/usr/zkCluster/zk4/bin/../conf:/usr/java/jdk1.8.0_141/lib/
    2019-06-11 14:34:13,040 [myid:] - INFO  [main:Environment@109] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
    2019-06-11 14:34:13,040 [myid:] - INFO  [main:Environment@109] - Client environment:java.io.tmpdir=/tmp
    2019-06-11 14:34:13,040 [myid:] - INFO  [main:Environment@109] - Client environment:java.compiler=<NA>
    2019-06-11 14:34:13,040 [myid:] - INFO  [main:Environment@109] - Client environment:os.name=Linux
    2019-06-11 14:34:13,040 [myid:] - INFO  [main:Environment@109] - Client environment:os.arch=amd64
    2019-06-11 14:34:13,041 [myid:] - INFO  [main:Environment@109] - Client environment:os.version=3.10.0-693.el7.x86_64
    2019-06-11 14:34:13,041 [myid:] - INFO  [main:Environment@109] - Client environment:user.name=root
    2019-06-11 14:34:13,041 [myid:] - INFO  [main:Environment@109] - Client environment:user.home=/root
    2019-06-11 14:34:13,041 [myid:] - INFO  [main:Environment@109] - Client environment:user.dir=/usr/zkCluster
    2019-06-11 14:34:13,042 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.free=16MB
    2019-06-11 14:34:13,050 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.max=247MB
    2019-06-11 14:34:13,050 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.total=23MB
    2019-06-11 14:34:13,057 [myid:] - INFO  [main:ZooKeeper@868] - Initiating client connection, connectString=localhost:2182,localhost:2183,localhost:2184 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@6477463f
    2019-06-11 14:34:13,074 [myid:] - INFO  [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
    2019-06-11 14:34:13,084 [myid:] - INFO  [main:ClientCnxnSocket@237] - jute.maxbuffer value is 4194304 Bytes
    2019-06-11 14:34:13,108 [myid:] - INFO  [main:ClientCnxn@1653] - zookeeper.request.timeout value is 0. feature enabled=
    2019-06-11 14:34:13,118 [myid:localhost:2182] - INFO  [main-SendThread(localhost:2182):ClientCnxn$SendThread@1112] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2182. Will not attempt to authenticate using SASL (unknown error)
    Welcome to ZooKeeper!
    JLine support is enabled
    2019-06-11 14:34:13,269 [myid:localhost:2182] - INFO  [main-SendThread(localhost:2182):ClientCnxn$SendThread@959] - Socket connection established, initiating session, client: /0:0:0:0:0:0:0:1:42104, server: localhost/0:0:0:0:0:0:0:1:2182
    2019-06-11 14:34:13,297 [myid:localhost:2182] - INFO  [main-SendThread(localhost:2182):ClientCnxn$SendThread@1394] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2182, sessionid = 0x10023af84cd0002, negotiated timeout = 30000
    
    WATCHER::
    
    WatchedEvent state:SyncConnected type:None path:null
    [zk: localhost:2182,localhost:2183,localhost:2184(CONNECTED) 0] 
    

    5. 编写清除配置信息

    [root@localhost zkCluster]# vi /usr/zkCluster/zk4_clean.sh
    ---
    #!/usr/bin/env bash
    # del conf
    rm -rf /usr/zkCluster/zk4/conf/zoo1.cfg
    rm -rf /usr/zkCluster/zk4/conf/zoo2.cfg
    rm -rf /usr/zkCluster/zk4/conf/zoo3.cfg
    
    rm -rf /usr/zkCluster/zk4/data/*
    ---
    [root@localhost zkCluster]# chmod +x /usr/zkCluster/zk4_clean.sh
    
    

    6. 编写暂停信息

    [root@localhost zkCluster]# vi /usr/zkCluster/zk4_stop.sh
    ---
    #!/usr/bin/env bash
    /usr/zkCluster/zk4/bin/zkServer.sh stop /usr/zkCluster/zk4/conf/zoo1.cfg
    /usr/zkCluster/zk4/bin/zkServer.sh stop /usr/zkCluster/zk4/conf/zoo2.cfg
    /usr/zkCluster/zk4/bin/zkServer.sh stop /usr/zkCluster/zk4/conf/zoo3.cfg
    
    ---
    [root@localhost zkCluster]# chmod +x /usr/zkCluster/zk4_stop.sh
    

    问题

    1. Opening socket connection to server localhost/127.0.0.1:2182. Will not attempt to authenticate using SASL (unknown error)
      解决: 查看是否都启动了,一定确保启动了,才可以连接

    PS: 若你觉得可以、还行、过得去、甚至不太差的话,可以“关注”或者“点赞”一下,就此谢过!

    相关文章

      网友评论

        本文标题:6. zk集群安装

        本文链接:https://www.haomeiwen.com/subject/cdkffctx.html