通过自动化脚本安装以及配置Zookeeper

  • zookeeper_boot.sh
    #!/bin/bash

    SERVERS="master slave1 slave2"
    PASSWORD=nn1234
    
    auto_ssh_copy_id(){
         expect -c "set timeout -1;
              spawn ssh-copy-id $1;
              expect {
                      *(yes/no)* {send -- yes\r;exp_continue;}
                      *password* {send -- $2\r;exp_continue;}
                      eof        {exit 0;}                    
              }";
    }
    
    ssh_copy_id_to_all(){
        for SERVER in $SERVERS
        do
            auto_ssh_copy_id $SERVER $PASSWORD
        done
    }
    
    ssh_copy_id_to_all
    
    sid=1
    
    for SERVER in $SERVERS
    do
      scp zookeeper_install.sh root@$SERVER:/root
      ssh root@$SERVER /root/zookeeper_install.sh $sid  #sid是设置给myid的值
      let sid++
    done
    
  • zookeeper_install.sh

    #!/bin/bash
    
    PACKAGE_SERVER=192.168.1.10
    BASE_DIR=/usr/app/zookeeper-3.4.10
    
    wget -P /usr/app/ $PACKAGE_SERVER/file/zookeeper-3.4.10.tar.gz
    cd /usr/app/
    tar -zxvf zookeeper-3.4.10.tar.gz
    rm -rf zookeeper-3.4.10.tar.gz
    cp $BASE_DIR/conf/zoo_sample.cfg $BASE_DIR/conf/zoo.cfg
    sed -i 's/\/tmp\/zookeeper/\/usr\/app\/zookeeper-3.4.10\/zoo_data/' $BASE_DIR/conf/zoo.cfg  # 修改dataDir目录
    # 配置集群节点
    cat >> $BASE_DIR/conf/zoo.cfg << EOF
    server.1=master:2888:3888   #2888是leader与flower之间的访问端口(心跳端口),3888是投票选举leader端口(数据端口)
    server.2=slave1:2888:3888
    server.3=slave2:2888:3888
    EOF
    
    mkdir $BASE_DIR/zoo_data/
    cd $BASE_DIR/zoo_data/
    echo $1 > myid  # 通过参数的方式来配置myid
    
    rm -f ~/zookeeper_install.sh
    
  • zkServer启动脚本zkServerStart.sh
    #!/bin/bash

    #  export A=1  定义的变量,会对自己所在的shell进程及其子进程生效
    # B=1 定义的变量,只对自己所在的shell进程生效
    # 在script.sh中定义的变量,在当前登陆的shell进程中 source script.sh时,脚本中定>义的变量也会进入当前登陆的进程
    
    SERVERS="master slave1 slave2"
    
    for server in $SERVERS
    do
      echo "start zkServer:  $server"
      ssh root@$server "source /etc/profile;/usr/app/zookeeper-3.4.10/bin/zkServer.sh start"
    done
    原文作者:JustCode
    原文地址: https://www.jianshu.com/p/6b27e059c7d5
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞