工具

集群环境自动配置脚本

2018-07-14  本文已影响63人  帅可儿妞

在学习的过程中,有时候就把环境搞坏了,反复反复反复,浪费了很多时间,所以就搞了一个自动化环境配置脚本,在这里记一下

一、环境准备

  1. 安装多台虚拟机实例,这是使用的是CentOS 6.9;
  2. 保证每台虚拟机实例可以联网,至少可以内部互通;
  3. 在一台机器A上安装httpd,遇到的问题具体的配置这里,把jdk放在html文件夹中;

二、配置脚本

  1. 配置A机器的脚本:install.sh
    #!/bin/bash
    
    SERVERS="192.168.70.131 192.168.70.132 192.168.70.133"
    PASSWORD=CentOS6.9
    BASE_SERVER=192.168.70.100
    
    config_ssh_auto_login() {
        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;}
            }";
    }
    
    config_ssh_auto_login_batch() {
        echo ~~~~~~~~~~~~~~~~~~~~~~~~~~ Starting ssh-copy-id ~~~~~~~~~~~~~~~~~~~~~~~~~~
        for SERVER in $SERVERS
        do
            config_ssh_auto_login $SERVER $PASSWORD
        done
    }
    
    config_local_env() {
        yum install -y openssh-clients;
        yum install -y expect;
    }
    
    config_remote_openssh_clients() {
        for SERVER in $SERVERS
        do
            expect -c "set timeout -1;
                spawn ssh root@$SERVER yum install -y openssh-clients;
                expect {
                    *password:* {send -- $PASSWORD\r;exp_continue;}
                    *(yes/no)* {send -- yes\r;exp_continue;}
                    eof        {exit 0;}
                }";
        done
    }
    
    make_rsa_key() {
        expect -c "set timeout -1;
            spawn ssh-keygen;
            expect {
                */.ssh/id_rsa* {send -- \r;exp_continue;}
                *empty* {send -- \r;exp_continue;}
                *again* {send -- \r;exp_continue;}
                eof        {exit 0;}
            }";
    }
    
    echo ~~~~~~~~~~~~~~~~~~~~~~~~~~ config local environment ~~~~~~~~~~~~~~~~~~~~~~~~~~
    config_local_env
    
    echo ~~~~~~~~~~~~~~~~~~~~~~~~~~ config remote openssh-clients ~~~~~~~~~~~~~~~~~~~~~~~~~~
    config_remote_openssh_clients
    
    echo ~~~~~~~~~~~~~~~~~~~~~~~~~~ make rsa key ~~~~~~~~~~~~~~~~~~~~~~~~~~
    make_rsa_key
    
    echo ****************************** config ssh auto login ~~~~~~~~~~~~~~~~~~~~~~~~~~
    config_ssh_auto_login_batch
    
    echo ~~~~~~~~~~~~~~~~~~~~~~~~~~ copy and execute config.sh ~~~~~~~~~~~~~~~~~~~~~~~~~~
    for SERVER in $SERVERS
    do
        ssh root@$SERVER mkdir /root/scripts
        scp config.sh root@$SERVER:/root/scripts
        ssh root@$SERVER /root/scripts/config.sh
    done
    
  2. 自动配置其他机器:config.sh
    #!/bin/bash
    
    BASE_SERVER=192.168.70.100
    
    yum install -y openssh-clients;
    yum install -y expect;
    yum install -y wget
    
    # Download jdk-8u171-linux-x64.tar.gz
    wget $BASE_SERVER/jdk-8u171-linux-x64.tar.gz
    tar -zxvf jdk-8u171-linux-x64.tar.gz -C /usr/local/bin
    cat >> /etc/profile << EOF
    export JAVA_HOME=/usr/local/bin/jdk1.8.0_171
    export PATH=\$PATH:\$JAVA_HOME/bin
    EOF
    

三、最后

  1. 这个脚本的原型源自网络,自己再加工了一下;
  2. 内部还有很多改善的地方,后面慢慢改善吧。。。
上一篇 下一篇

猜你喜欢

热点阅读