離線自動化部署CDH

来源:https://www.cnblogs.com/shun57/archive/2022/05/10/16254592.html
-Advertisement-
Play Games

離線CDH集群自動化部署工具 離線CDH集群安裝與部署的自動化腳本工具,簡單支持「離線一鍵裝機」。 腳本將對系統配置做出一定修改,使用前請務必確認當前伺服器無其他人員、任務使用,以免造成不必要的麻煩,建議提前使用測試伺服器或虛擬機測試體驗。 一、Features 已實現的自動化功能(僅支持Redha ...


離線CDH集群自動化部署工具

離線CDH集群安裝與部署的自動化腳本工具,簡單支持「離線一鍵裝機」。

腳本將對系統配置做出一定修改,使用前請務必確認當前伺服器無其他人員、任務使用,以免造成不必要的麻煩,建議提前使用測試伺服器或虛擬機測試體驗。

一、Features

已實現的自動化功能(僅支持Redhat/CentOS系列):
Usage: init_ssh|install_softs|init_sys|init_dev|init_mysql|test_sys|init_cm|install_all

  1. init_ssh : Configure password-free login from the control machine to other machines
  2. install_softs : Make a custom yum source. Includes: Install http service, extract Centos7 rpm package, ansible rpm package, cdh related software.
  3. init_sys : Initialize the system configuration. Includes selinux, firewall, tuned, hugepage, swappiness, tmout, kernel, maxfiles
  4. init_dev : Configure jdk with version jdk-8u231-linux-x64
  5. init_mysql : Configure the scm configuration database, initialize the hive, scm, amon, ........... databases and authorize it
  6. test_sys : Test cluster network connectivity
  7. init_cm : Control node installation cloudera-scm-* and init the cloudera-scm-server with mysql
  8. install_all : Execute all scripts except init_ssh sequentially. sequen : install_softs->init_system->init_devenv->init_mysql->init_cm->test_system

二、Getting Start

在主機中選擇一臺執行操作過程,以下稱為「控制主機」,並確保主機外網可用。

2.1 安裝包准備

當前僅支持離線安裝,需要提前下載所需軟體安裝包,如下:

  • JDK
    • 下載地址:Cloudera Archive CM
    • 說明:版本無限制,僅支持 Oracle JDK RPM包,名稱格式為 oracle-j2sdk*.rpm
    • 示例安裝包:jdk-8u231-linux-x64.rpm
  • Mariadb
    • 下載地址:Mariadb
    • 說明:5.7以上版本,僅支持RPM Bundle包,名稱格式為 mariadb-server.x86_64.rpm
    • 示例安裝包:mariadb-server.x86_64.rpm
  • MySQL驅動包(5.1.46)
    • 下載地址:MySQL Connector
    • 說明:與MySQL版本對應,名稱格式為 mysql-connector-java-*.tar.gz
    • 示例安裝包:mysql-connector-java-5.1.46.tar.gz
  • Cloudera Manager
    • 下載地址:Cloudera Archive CM
    • 說明:版本無限制
    • 示例安裝包:
      • cloudera-manager-agent.x86_64 : The Cloudera Manager Agent
      • cloudera-manager-server.x86_64 : The Cloudera Manager Server
      • cloudera-manager-server-db-2.x86_64 : Embedded database for the Cloudera Manager Server
      • cloudera-manager-daemons.x86_64 : Provides daemons for monitoring Hadoop and related tools.
  • CDH
    • 下載地址:Cloudera Archive CDH
    • 說明:版本無限制
    • 示例安裝包:
      • manifest.json
      • CDH-6.3.2-1.cdh6.3.2.p0.1605554-el7.parcel
      • allkeys.asc
      • CDH-6.3.2-1.cdh6.3.2.p0.1605554-el7.parcel.sha1
      • CDH-6.3.2-1.cdh6.3.2.p0.1605554-el7.parcel.sha256
  • Centos rpm
    • Centos 官網 ISO
    • 說明:版本系列和機器版本保持一致
    • 示例安裝包:
      • centos7
  • ansible 安裝包
    • rpm倉庫
    • 說明:Centos7原生沒有對應rpm包,自行下載
    • 示例安裝包:
      • ansible-2.9.21-1.el7.noarch.rpm

百度雲打包下載所有腳本:

鏈接:https://pan.baidu.com/s/1Qz_BFu0-4aOZ84nvC1FnOw 
提取碼:lsom

上傳所有安裝包至控制主機的/tmp/autocdh/路徑下。

2.2 修改hosts文件

vi hosts

將集群所有節點的ip與hostname寫入hosts文件中,格式同/etc/hosts,ip與hosts之間空格隔開。
示例:

192.168.1.156 autocdh1
192.168.1.157 autocdh2
192.168.1.158 autocdh3

2.3 修改配置文件

vi deploy_robot.config
  • control_host: 控制主機節點hostname
  • host_user: 集群主機用戶名,需要有root許可權
  • host_passwd: 集群主機用戶密碼,所有主機需要一致
  • contrast_host: 網路測試使用的節點hostname
  • mysql_host: MySQL安裝的節點hostname
  • http_server_host: Http服務節點
  • mysql_root_passwd: MySQL 初始化之後將使用此密碼作為 root 用戶密碼
  • cm_host: cm server節點hostname
  • cm_db_passwd: cm資料庫初始化密碼

示例:

  • control_host=autocdh3
  • host_user=root
  • host_passwd=123123
  • contrast_host=autocdh3
  • http_server_host=autocdh2
  • mysql_host=autocdh1
  • mysql_root_passwd=123123
  • cm_host=autocdh3
  • cm_db_passwd=scm

2.4 執行腳本

# 配置ssh
sh deploy_robot.sh init_ssh
# 執行安裝
sh deploy_robot.sh install_all

That's All!

# 查看server啟動日誌
tail -f /var/log/cloudera-scm-server/cloudera-scm-server.log
# 等待幾分鐘後看到 Started Jetty server.

訪問cm主機的7180埠即可,賬號密碼為預設的admin/admin,登錄即可。

三、使用說明

3.1 腳本參數

sh deploy_robot.sh 
Usage: <type> init_ssh|install_softs|init_sys|init_dev|init_mysql|test_sys|init_cm|install_all

1. init_ssh : Configure password-free login from the control machine to other machines 
2. install_softs : Make a custom yum source. Includes: Install http service, extract Centos7 rpm package, ansible rpm package, cdh related software. 
3. init_sys : Initialize the system configuration. Includes selinux, firewall, tuned, hugepage, swappiness, tmout, kernel, maxfiles
4. init_dev : Configure jdk with version jdk-8u231-linux-x64
5. init_mysql : Configure the scm configuration database, initialize the hive, scm, amon, ........... databases and authorize it 
6. test_sys : Test cluster network connectivity
7. init_cm : Control node installation cloudera-scm-* and init the cloudera-scm-server with mysql
8. install_all : Execute all scripts except init_ssh sequentially. sequen :  install_softs->init_system->init_devenv->init_mysql->init_cm->test_system

執行腳本需要指定「執行類型」參數。

除了Features中各個功能的執行參數之外,install_all 將會依次執行2-7步驟。

3.2 yum軟體安裝列表

yum_requirements.txt 文件中描述了需要在集群各個節點上安裝的軟體名。

腳本執行過程中依賴相關軟體,預設的軟體列表不建議修改

四、其他

4.1 註意事項

  1. 使用init_ssh設置免密登錄時,如果已生成過ssh密鑰,那麼ssh的key與認證信息將被重置。如果之前配置過ssh信息請註意更新ssh key。或者選擇不要執行init_ssh,手動進行ssh配置。
  2. 建議在即將投入生產或者乾凈的主機上運行,以免破壞已有生產系統環境。
  3. CM安裝過程中不會啟用SSL

五、附錄

5.1 主控腳本

#!/bin/bash
#
if [ $# -lt 1 ]
then
    echo "Usage: <type> init_ssh|install_softs|init_sys|init_dev|init_mysql|test_sys|init_cm|install_all"
    echo "init_ssh : Configure password-free login from the control machine to other machines "
    echo "install_softs : Make a custom yum source. Includes: Install http service, extract Centos7 rpm package, ansible rpm package, cdh related software. "
    echo "init_sys : Initialize the system configuration. Includes selinux, firewall, tuned, hugepage, swappiness, tmout, kernel, maxfiles"
    echo "init_dev : Configure jdk with version jdk-8u231-linux-x64"
    echo "init_mysql : Configure the scm configuration database, initialize the hive, scm, amon, ........... databases and authorize it "
    echo "test_sys : Test cluster network connectivity"
    echo "init_cm : Control node installation cloudera-scm-* and init the cloudera-scm-server with mysql"
    echo "install_all : Execute all scripts except init_ssh sequentially. sequen :  install_softs->init_system->init_devenv->init_mysql->init_cm->test_system"
    exit 1
fi

# declare config
declare -A CONFIG_NANME=(
    ["CTRL_HOST"]="control_host"
    ["USER"]="host_user"
    ["PASSWD"]="host_passwd"
    ["CONTRAST"]="contrast_host"
    ["DB_HOST"]="mysql_host"
    ["DB_ROOT_PASSWD"]="mysql_root_passwd"
    ["CM_HOST"]="cm_host"
    ["CM_DB_PASSWD"]="cm_db_passwd"
    ["HTTP_SERVER_HOST"]="http_server_host"
    )

declare -A config_map=()
declare -A hostip_map=()

export TOP_PID=$$
trap 'exit 1' TERM

function quit {
    kill -s TERM $TOP_PID
}

function say {
    printf '\033[1;4;%sm %s: %s \033[0m\n' "$1" "$2" "$3"
}

function err {
    say "31" "!!!![error]!!!! deploy failed" "$1" >&2
    exit 1
}

function info {
    say "32" "####[info]#### process info" "$1" >&1
}

function check_cmd {
    command -v "$1" > /dev/null 2>&1
}

function need_cmd {
    if ! check_cmd "$1"; then
        err "need '$1' (command not found)"
    fi
}

function need_ok {
    if [[ $? -ne 0 ]]; then err "$1"; fi
}

function have_fun {
    fun=`cat $SELF/have_fun`
    printf '\033[1;32m %s \033[0m\n\n' "$fun"
}

function need_config {
    if [ -z $1 ]
    then
        err "need key to get config"
    else
        c=${config_map[$1]}
        if [ -z $c ]
        then
            err "config not found $1"
        fi
    fi
}

function get_config {
    need_config $1
    eval $2=${config_map[$1]}
    info "get config $1:${config_map[$1]}."
}


function have {
    if [ ! -f $1 ]
    then
        err "$1 file doesn't exists"
    fi
}


function init_config {
    info "config loading..."
    config_file=$1
    info "get config_file $config_file."
    have $config_file
    config_arr=`cat $config_file | grep '='`
    for c in ${config_arr}
    do
        arr=(${c//=/ })
        key=${arr[0]}
        value=${arr[1]}
        config_map[$key]=$value
    done
    info "config load finished."
    echo 
}

function get_home {
    if [ $1 == "root" ]
    then
        user_home="/root"
    else
        user_home="/home/$user"
    fi
    info "operate user home: $user_home"
    echo
}


function init_hosts {
    info "hosts loading..."
    info "get host_file $SELF/hosts"
    host_file=$SELF/hosts
    have $host_file
    host_arr=`cat $host_file | sed s'/ /,/'`
    for a in ${host_arr[*]}
    do
        if [ -n $a ]
        then
            arr=(${a//,/ })
            ip=${arr[0]}
            host=${arr[1]}
            need_ok "host dosen't formated: $a"
            hostip_map[$host]=$ip
        fi
    done
    #key
    hosts=${!hostip_map[@]} 
    info "hosts: $hosts"
    #value
    ips=${hostip_map[@]} 
    info "ips: $ips"
    info "hosts load finished."
    echo
}

function set_hosts {
    # 主機名設置
    info "start setting /etc/hosts and config ssh keys."
    rpm -ivh $install_path/expect-5.45-14.el7_1.x86_64.rpm $install_path/tcl-8.5.13-8.el7.x86_64.rpm
    need_cmd expect
    have $expect_file
    if [ -f /tmp/autocdh/hosts.bak ]
    then
        info "restore hosts from /tmp/hosts.bak"
        cat /tmp/autocdh/hosts.bak > /etc/hosts
    else
        info "backup hosts file to /tmp/hosts.bak"
        cat /etc/hosts > /tmp/autocdh/hosts.bak
    fi
    cat $host_file >> /etc/hosts
    info "hosts added to /etc/hosts"
    curr_hosts=`cat /etc/hosts`
    info "current hosts: ${curr_hosts[*]}"
    info "start config ssh key(all hosts)."
    for host in ${hosts[*]}
    do
        expect $expect_file ssh $host $user $passwd "rm -rf $user_home/.ssh"
        expect $expect_file ssh $host $user $passwd "hostnamectl set-hostname $host"
        expect $expect_file scp $host $user $passwd /etc/hosts
        expect $expect_file ssh $host $user $passwd "ssh-keygen -t rsa"
        key=`expect $expect_file ssh $host $user $passwd "cat $user_home/.ssh/id_rsa.pub"`
        echo $key | awk -F 'ssh-rsa' '{printf "ssh-rsa%s\n",$2}' >> $user_home/.ssh/authorized_keys
    done
    info "all hosts ssh key done."
    info "scp authorized_keys to all hosts."
    for host in ${hosts[*]}
    do
        expect $expect_file scp $host $user $passwd $user_home/.ssh/authorized_keys
    done
    info "scp authorized_keys done."
    ssh $db_host date
    need_ok "ssh failed."
    echo
}

function install_ansible {
    info "ansible control host clean and makecache"
    yum clean all && yum makecache
    info "start install ansible."
    yum install -y ansible
    need_cmd ansible
    echo "[all]" > /etc/ansible/hosts
    for host in ${hosts[*]}
    do
        echo $host >> /etc/ansible/hosts
    done
    info "ansible finish config."
    echo
}

function ansible_command {
    ansible all -a "$1"
    need_ok "ansible command failed: $1"
}

function ansible_shell {
    ansible all -m shell -a "$1"
    need_ok "ansible shell failed: $1"
}

function ansible_copy {
    ansible all -m copy -a "$1"
    need_ok "ansible copy failed: $1"
}


function install_httpd {
    info "httpd server  host: $http_server_host"
    ssh autocdh2 "httpd -version"
    if [[ $?  -eq 0 ]]; then
        info "httpd service already installed"
    else
        apr_file=`ls $install_path/apr-1.4.8-3.el7_4.1.x86_64.rpm`
        apr_util_file=`ls $install_path/apr-util-1.5.2-6.el7.x86_64.rpm`
        httpd_file=`ls $install_path/httpd-2.4.6-88.el7.centos.x86_64.rpm`
        httpd_tools_file=`ls $install_path/httpd-tools-2.4.6-88.el7.centos.x86_64.rpm`
        mailcap_file=`ls $install_path/mailcap-2.1.41-2.el7.noarch.rpm`
        have $apr_file
        have $apr_util_file
        have $httpd_file
        have $httpd_tools_file
        have $mailcap_file
        info "mkdir and scp to http host."
        ssh $http_server_host <<EOF
        mkdir /tmp/autocdh/
EOF
        scp $apr_file $http_server_host:$install_path/
        scp $apr_util_file $http_server_host:$install_path/
        scp $httpd_file $http_server_host:$install_path/
        scp $httpd_tools_file $http_server_host:$install_path/
        scp $mailcap_file $http_server_host:$install_path/
        info "scp httpd rpm done."
        
        info "install httpd, wait a moment..."
        ssh $http_server_host <<EOF
        cd /tmp/autocdh/
        rpm -ivh apr-1.4.8-3.el7_4.1.x86_64.rpm apr-util-1.5.2-6.el7.x86_64.rpm httpd-2.4.6-88.el7.centos.x86_64.rpm httpd-tools-2.4.6-88.el7.centos.x86_64.rpm mailcap-2.1.41-2.el7.noarch.rpm
        if [ -f /etc/httpd/conf/httpd.conf ]
        then
            echo "add the httpd conf to support .parcel"
            sed -i 's?AddType application/x-gzip .gz .tgz?AddType application/x-gzip .gz .tgz .parcel?' /etc/httpd/conf/httpd.conf 
            systemctl start httpd.service
            systemctl enable httpd.service
            systemctl stop firewalld
        else
            echo "http conf file not found"
            exit
        fi  
EOF
        info "httpd service install done"
    fi
    
}

function unpack_repo_gz {

    info "httpd server  host: $http_server_host"
    cdh_gz_file=`ls $install_path/cdh.tar.gz`
    rhel7_gz_file=`ls $install_path/centos7.tar.gz`
    ansible_gz_file=`ls $install_path/ansible.tar.gz`
    
    have $cdh_gz_file
    have $rhel7_gz_file
    have $ansible_gz_file

    info "mkdir and scp to http host."
    scp $cdh_gz_file $http_server_host:$install_path
    scp $rhel7_gz_file $http_server_host:$install_path
    scp $ansible_gz_file $http_server_host:$install_path
    info "scp  cdh.tar.gz and centos7.tar.gz and ansible.tar.gz done."

    ssh $http_server_host <<EOF
    echo "unpack the cdh.tar.gz and centos7.tar.gz and ansible.tar.gz into /var/www/html/"
    tar -zxvf /tmp/autocdh/cdh.tar.gz -C /var/www/html/
    tar -zxvf /tmp/autocdh/centos7.tar.gz -C /var/www/html/
    tar -zxvf /tmp/autocdh/ansible.tar.gz -C /var/www/html/
    echo "unpack the gz file success"
EOF

}

function delete_void_repo {

    info "delete void repo"
    ansible_shell "cd /etc/yum.repos.d/ && ls | grep -v TZ | xargs rm -rf"
    info "bakup all repo success"
}


function intranet_yum_repo {
    info "start set intranet yum repo"
    if [[ -f /etc/yum.repos.d/TZcdh.repo ]]; then
        info "cdh repo already exists"
    else 
        echo -e "[TZcdh]
name=TZcdh
baseurl=http://$http_server_host/cdh/
gpgcheck=0
enabled=1"  >> /etc/yum.repos.d/TZcdh.repo
    fi
    if [[ -f /etc/yum.repos.d/TZcentos7.repo ]]; then
        info "TZcentos7 repo already exists"
    else 
        echo -e "[base]
name=TZCentOS-7
baseurl=http://$http_server_host/CentOS-7/
gpgcheck=1
enabled=1
gpgkey=http://$http_server_host/CentOS-7/RPM-GPG-KEY-CentOS-7"  >> /etc/yum.repos.d/TZcentos7.repo
    fi
    if [[ -f /etc/yum.repos.d/TZansible.repo ]]; then
        info "TZansible repo already exists"
    else 
        echo -e "[TZansible]
name=TZansible
baseurl=http://$http_server_host/ansible/
gpgcheck=0
enabled=1"  >> /etc/yum.repos.d/TZansible.repo
    fi
    echo

}
function sync_repo {
    info "sync repo for all cluster "
    ansible all -m copy -a "src=/etc/yum.repos.d/TZcdh.repo dest=/etc/yum.repos.d/TZcdh.repo"
    ansible all -m copy -a "src=/etc/yum.repos.d/TZansible.repo dest=/etc/yum.repos.d/TZansible.repo"
    ansible all -m copy -a "src=/etc/yum.repos.d/TZcentos7.repo dest=/etc/yum.repos.d/TZcentos7.repo"
    info "yum repo sync done."
}

function set_yum {
    info "start set yum."
    info "clean and makecache,it may take a little time, please wait a moment..."
    ansible_shell "yum clean all && yum makecache"
    info "install softs."
    yum_requrements=`cat $SELF/yum_requirements.txt`
    echo "yum install -y $yum_requrements"
    ansible_command "yum install -y $yum_requrements"
    info "install softs done."
    echo
}

function set_selinux {
    selinux_stat=`getenforce | tr 'A-Z' 'a-z'`
    info "setting selinux, current: $selinux_stat."
    if [ $selinux_stat == "enforcing" ]
    then
        info "change to disable..."
        sed -i 's/SELINUX=enforcing/SELINUX=disable/' /etc/selinux/config
        sed -i 's/SELINUX=Enforcing/SELINUX=disable/' /etc/selinux/config
        curr=`cat /etc/selinux/config | grep -v '#' | grep SELINUX=`
        info "current: $curr."
    fi
    info "sync to hosts."
    ansible all -m copy -a "src=/etc/selinux/config dest=/etc/selinux/config"
    info "done."
    echo
}


function set_firewall {
    info "disable firewalld..."
    ansible all -a "systemctl status firewalld"
    ansible all -a "systemctl stop firewalld"
    ansible all -a "systemctl disable firewalld"
    info "firewalld disabled."
    echo
}


function set_ntp {
    # ntp配置
    info "start ntp server..."
    info "change timezone info to Shanghai."
    ansible all -m shell -a "rm -rf /etc/localtime && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime"
    info "install ntpd."
    ansible all -a "yum install ntp -y"
    info "sync time to 0.cn.pool.ntp.org"
    ansible all -a "ntpdate -u 0.cn.pool.ntp.org"
    info "add ntp1.aliyun.com to ntp.conf"
    is_exists=`cat /etc/ntp.conf | grep aliyun | wc -l`
    if [ $is_exists -eq 0 ]
    then
        echo "server ntp1.aliyun.com" >> /etc/ntp.conf
    fi
    ansible all -a "systemctl start ntpd"
    ansible all -a "systemctl enable ntpd"
    info "ntp done."
    echo
}

function set_java {
    info "start set java..."
    info "remove the old java | jdk"
    
    ansible all -a "rpm -qa | grep java | xargs rpm -e --nodeps"
    ansible all -a "rpm -qa | grep jdk | xargs rpm -e --nodeps"
    ansible all -a "rpm -ivh http://$http_server_host/cdh/jdk-8u231-linux-x64.rpm"    

    if ! check_cmd java; then
        info "java install failed"
    else
        info "java already installed."
    fi
    info "java install done."
    echo
}


function set_tuned {
    info "disable tuned..."
    ansible all -a "systemctl start tuned"
    ansible all -a "systemctl status tuned"
    ansible all -a "tuned-adm off"
    ansible all -a "tuned-adm list"
    ansible all -a "systemctl stop tuned"
    ansible all -a "systemctl disable tuned"    
    info "tuned config done."
    echo
}

function set_hugepage {
    info "disable hugepage..."
    curr0=`cat /sys/kernel/mm/transparent_hugepage/enabled`
    curr1=`cat /sys/kernel/mm/transparent_hugepage/defrag`
    info "current: $curr0 , $curr1"
    is_enable=`cat /sys/kernel/mm/transparent_hugepage/enabled | grep \\[always\\] | wc -l`
    is_defrag=`cat /sys/kernel/mm/transparent_hugepage/defrag | grep \\[always\\] | wc -l`
    if [ $is_enable -eq 1 ]
    then
        ansible all -m shell -a "echo never > /sys/kernel/mm/transparent_hugepage/enabled"
        ansible all -m shell -a "echo never > /sys/kernel/mm/transparent_hugepage/defrag"
        echo "echo never > /sys/kernel/mm/transparent_hugepage/defrag" >> /etc/rc.local
        echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
        chmod +x /etc/rc.d/rc.local
        ansible all -m copy -a "src=/etc/rc.d/rc.local dest=/etc/rc.d/rc.local"
        original=`cat /etc/default/grub | grep GRUB_CMDLINE_LINUX | awk -F '="' '{print $2}'`
        result="GRUB_CMDLINE_LINUX=\"transparent_hugepage=never "$original
        cat /etc/default/grub | grep -v GRUB_CMDLINE_LINUX > /etc/default/grub
        echo $result >> /etc/default/grub
        curr2=`cat /etc/default/grub | grep GRUB_CMDLINE_LINUX`
        info "current: $curr2"
        ansible all -m copy -a "src=/etc/default/grub dest=/etc/default/grub"
        ansible all -a "grub2-mkconfig -o /boot/grub2/grub.cfg"
    fi
    info "setting hugepage done."
    echo
}

function set_swappiness {
    info "set swappiness..."
    swap_stat=`cat /proc/sys/vm/swappiness`
    info "current: $swap_stat"
    if [ ! $swap_stat -eq 1 ]
    then
        ansible all -a "sysctl -w vm.swappiness=1"
        echo "vm.swappiness=1" >> /etc/sysctl.conf
        curr=`cat /etc/sysctl.conf | grep swappiness`
        info "current: $curr"
        ansible all -m copy -a "src=/etc/sysctl.conf dest=/etc/sysctl.conf"
    fi
    info "setting swappiness done."
    echo
}

function set_tmout {
    info "set tmout to 900"
    is_exists=`cat /etc/profile | grep TMOUT=900 | wc -l`
    if [ $is_exists -eq 0 ]
    then
        echo "TMOUT=900" >> /etc/profile
        ansible all -m copy -a "src=/etc/profile dest=/etc/profile"
    else
        info "tmout already installed"
    fi
    info "setting tmout done."
    echo
}

function set_kernel {
    info "setting kernel..."
    is_exists=`cat /etc/sysctl.conf | grep pid_max | wc -l`
    if [ $is_exists -eq 0 ]
    then
        echo -e "\nnet.ipv4.tcp_tw_reuse = 1
        \nnet.ipv4.tcp_tw_recycle = 1
        \nnet.ipv4.tcp_keepalive_time = 1200
        \nnet.ipv4.ip_local_port_range = 10000 65000
        \nnet.ipv4.tcp_max_syn_backlog = 8192
        \nnet.ipv4.tcp_max_tw_buckets = 5000
        \nfs.file-max = 655350
        \nnet.ipv4.route.gc_timeout = 100
        \nnet.ipv4.tcp_syn_retries = 1
        \nnet.ipv4.tcp_synack_retries = 1
        \nnet.core.netdev_max_backlog = 16384
        \nnet.ipv4.tcp_max_orphans = 16384
        \nnet.ipv4.tcp_fin_timeout = 2
        \net.core.somaxconn=32768
        \nkernel.threads-max=196605
        \nkernel.pid_max=196605
        \nvm.max_map_count=393210"  >> /etc/sysctl.conf
    else
        info "kernel already installed."
    fi
    ansible all -m copy -a "src=/etc/sysctl.conf dest=/etc/sysctl.conf"
    info "setting kernel done."
    echo
}

function set_maxfiles {
    curr=`ulimit -a`
    info "current max files: $curr \n change to 196605..."
    is_exists=`cat /etc/security/limits.conf | grep 196605 | wc -l`
    if [ $is_exists -eq 0 ]
    then
        sed -i '$ a\* soft nofile 196605' /etc/security/limits.conf
        sed -i '$ a\* hard nofile 196605' /etc/security/limits.conf
        echo "* soft nproc 196605" >> /etc/security/limits.conf
        echo "* hard nproc 196605" >> /etc/security/limits.conf
    else
        info "ulimit already installed."
    fi
    ansible all -m copy -a "src=/etc/security/limits.conf dest=/etc/security/limits.conf"
    info "setting maxfiles done."
    echo
}

function get_sysinfo {
    sys_version=`cat /etc/redhat-release`
    info "operator system version: $sys_version"
    umask_info=`umask`
    info "umask: $umask_info"
    java_version=`java -version`
    info "java version: $java_version"
    info "java_home: $jdk_path"
    total_mem=`free -h | grep Mem | awk '{print $2}'`
    info "total memory: $total_mem"
    cpu_num=`cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l`
    info "number of cpu: $cpu_num"
    cpu_cores=`cat /proc/cpuinfo| grep "cpu cores"| uniq`
    info "core number per cpu: $cpu_cores"
    total_cpu=`cat /proc/cpuinfo| grep "processor"| wc -l`
    info "logic cpu cores: $total_cpu"
    cpu_info=`cat /proc/cpuinfo`
    info "cpu info: \n$cpu_info"
    echo
}

function test_network {
    info "start network test..."
    info "contrast host is: $contrast_host, start iperf server."
    nohup iperf3 -s -p 12345 -i 1 > /dev/null 2>&1 &
    pid=`ps -ef | grep iperf3 | grep -v 'grep' | awk '{print $2}'`
    if [ $? -eq 0 ]
    then
        info "iperf3 server pid: $pid"
    else
        err "iperf3 server start failed."
    fi
    info "start iperf client, please wait a moment..."
    echo "####network test####" > $test_sys_file
    ssh $contrast_host "iperf3 -c $ctrl_host -p 12345 -i 1 -t 10 -w 100K" >> $test_sys_file
    echo "" >> $test_sys_file
    echo "" >> $test_sys_file
    info "test done,save result to $test_sys_file killing iperf server, pid: $pid"
    kill -9 $pid
    echo
}



function set_mariadb {
    info "start set mariadb..."
    ssh $db_host "mysql" 
    if ! "$?" -eq 0 ; then
        info "mariadb host: $db_host"
        ssh $db_host <<EOF
        yum install -y mariadb-server
        systemctl start mariadb
        systemctl enable mariadb
        mysql_secure_installation << EOF
            
            y
            $mysql_passwd
            $mysql_passwd
            y
            y
            y
            y
        EOF
EOF
    else 
        info "mysql already installed."
    fi

    # 初始化cm相關庫
    ssh $db_host <<EOF
    mysql -u root -p"$mysql_passwd" -e "drop database scm;"
    mysql -u root -p"$mysql_passwd" -e "drop database amon;"
    mysql -u root -p"$mysql_passwd" -e "drop database hive;"
    mysql -u root -p"$mysql_passwd" -e "CREATE DATABASE scm DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;"
    mysql -u root -p"$mysql_passwd" -e "GRANT ALL ON scm.* TO 'scm'@'%' IDENTIFIED BY 'scm';"
    mysql -u root -p"$mysql_passwd" -e "CREATE DATABASE amon DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;"
    mysql -u root -p"$mysql_passwd" -e "GRANT ALL ON amon.* TO 'amon'@'%' IDENTIFIED BY 'amon';"
    mysql -u root -p"$mysql_passwd" -e "CREATE DATABASE hive DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;"
    mysql -u root -p"$mysql_passwd" -e "GRANT ALL ON hive.* TO 'hive'@'%' IDENTIFIED BY 'hive';"
    mysql -u root -p"$mysql_passwd" -e "SHOW DATABASES;"
    mysql -u root -p"$mysql_passwd" -e "flush privileges;"
EOF
    info "done."
    echo


}

function set_scm_server {
    info "start install cloudera-scm-server "
    is_exists=`systemctl status cloudera-scm-server`
    if [ ! -z "$is_exists" ]
    then
        info "cloudera-scm-server already installed."
    else 
        info "yum install  cloudera-scm-server"
        yum install -y cloudera*
        ansible_copy "src=$install_path/mysql-connector-java.jar dest=/usr/share/java/"
        ansible_copy "src=$install_path/mysql-connector-java.jar dest=/opt/cloudera/cm/lib/"
        /opt/cloudera/cm/schema/scm_prepare_database.sh -h $db_host -P 3306 mysql scm scm scm
        service cloudera-scm-server start 
        systemctl enable cloudera-scm-server
    fi

}


function need_ssh {
    if [ ! -f /tmp/autocdh/hosts.bak ]
    then
        err "hosts not set, it may means ssh not init. try tu use <sh deploy_robot.sh init_ssh>."
    fi
}

function init_ssh {
    set_hosts
}

function install_softs {
    install_httpd
    unpack_repo_gz
    bakup_repo
    intranet_yum_repo
    install_ansible
    delete_void_repo
    sync_repo
    set_yum
    need_ssh
}

function init_system {
    need_ssh
    need_cmd ansible
    set_selinux
    set_firewall
    set_tuned
    set_hugepage
    set_swappiness
    set_tmout
    set_kernel
    set_maxfiles
}

function init_devenv {
    need_ssh
    need_cmd ansible
    set_java
}

function init_mysql {
    need_ssh
    set_mariadb
}

function test_system {
    need_ssh
    test_network
}

function init_cm {
    need_ssh
    need_cmd ansible
    set_scm_server
}



SELF=$(cd $(dirname $0) && pwd)
cd $SELF
tmp_path=/tmp/autocdh
install_path=$tmp_path
test_sys_file=$SELF/test_sys.log
config_file=$SELF/deploy_robot.config
expect_file=$SELF/expect.sh

info "start deploy process."
exec=$1

init_config $config_file
# 控制主機與用戶名、密碼
get_config ${CONFIG_NANME[CTRL_HOST]} ctrl_host
get_config ${CONFIG_NANME[USER]} user
get_config ${CONFIG_NANME[PASSWD]} passwd
get_config ${CONFIG_NANME[CONTRAST]} contrast_host
get_config ${CONFIG_NANME[DB_HOST]} db_host
get_config ${CONFIG_NANME[DB_ROOT_PASSWD]} mysql_passwd
get_config ${CONFIG_NANME[CM_HOST]} cm_host
get_config ${CONFIG_NANME[CM_DB_PASSWD]} cm_db_passwd
get_config ${CONFIG_NANME[HTTP_SERVER_HOST]} http_server_host
get_home $user
init_hosts

if [ $exec == "init_ssh" ]
then
    init_ssh
elif [ $exec == "install_softs" ]
then
    install_softs
elif [ $exec == "init_sys" ]
then
    init_system
elif [ $exec == "init_dev" ]
then
    init_devenv
elif [ $exec == "init_mysql" ]
then    
    init_mysql
elif [ $exec == "test_sys" ]
then 
    test_system
elif [ $exec == "init_cm" ]
then
    init_cm
elif [ $exec == "install_all" ]
then
    install_softs
    init_system
    init_devenv
    init_mysql
    init_cm
    test_system
else
    info "nothing todo,  exit...
try to use init_ssh|install_softs|init_sys|init_dev|init_mysql|test_sys|init_cm|install_all ?"
    exit 0
fi

get_sysinfo
have_fun
info "install all done!!!"

5.2 主控配置文件

# 控制主機-執行腳本的機器
control_host=autocdh3
# 控制主機用戶名
host_user=root
# 控制主機密碼
host_passwd=123123
# 網路測試主機
contrast_host=autocdh3
# http服務主機
http_server_host=autocdh2
# mysql伺服器地址
mysql_host=autocdh1
# mysql root密碼
mysql_root_passwd=123123
# cm伺服器地址
cm_host=autocdh3
# cm資料庫密碼
cm_db_passwd=scm

5.3 except 腳本

#!/bin/expect

if {$argc != 5} {
    puts "Usage:cmd <exec> <host> <user> <passwd> <command>"
    exit 1
}

proc init_mysql_passwd {init_passwd new_passwd} {
  expect {
    "*Enter password for user root*" { send "$init_passwd\n"; exp_continue}
    "*New password*" { send "$new_passwd\n" ; exp_continue}
    "*Re-enter new password*" { send "$new_passwd\n"; exp_continue}
    "*Do you wish to continue with the password provided*" { send "y\n"; exp_continue}
    "*Change the password for root*" { send "y\n"; exp_continue}
    "*Remove anonymous users*" { send "y\n"; exp_continue}
    "*Disallow root login remotely*" { send "\n"; exp_continue}
    "*Remove test database and access to it*" { send "y\n"; exp_continue}
    "*Reload privilege tables now*" { send "y\n"; exp_continue}
    "*Enter SCM password*" { send "$new_passwd\n"; exp_continue}
    "*#" { return 0}
  }
  return 1
}

proc connect {passwd} {
   expect {
       "(yes/no)?" {
           send "yes\n"
           expect "*password:" {
                send "$passwd\n"
                expect {
                    "*#" {
                        return 0
                    }
                }
           }
       }
       "*'s password:" {
           send "$passwd\n"
           expect {
                "Overwrite (y/n)" {
                    send "y\n"
                    exp_continue
                }
                "*file in which to save the key*" {
                    send "\n"
                    exp_continue
                }
                "*Enter passphrase*" {
                    send "\n"
                    exp_continue
                }
                "*Enter same passphrase again*" {
                    send "\n"
                    exp_continue
                }
               "*#" {
                   return 0
               }
           }
       }
   }
   return 1
}

set timeout 30
set exec [lindex $argv 0] 
set host [lindex $argv 1] 
set user [lindex $argv 2] 
set passwd [lindex $argv 3]
set cmd  [lindex $argv 4]

if {$exec == "ssh"} {
    spawn ssh [email protected]$host $cmd
    if {[connect $passwd]} {
        exit 1
    }
} elseif {$exec == "scp"} {
    spawn scp $cmd [email protected]$host:$cmd
    if {[connect $passwd]} {
        exit 1
    }
} elseif {$exec == "get"} {
    spawn scp [email protected]$host:$cmd $cmd
    if {[connect $passwd]} {
        exit 1
    }
} elseif {$exec == "mysql_init"} {
    spawn /usr/bin/mysql_secure_installation
    if {[init_mysql_passwd $passwd $cmd]} {
        exit 1
    }
} elseif {$exec == "cm_init" } {
    spawn /opt/cloudera/cm/schema/scm_prepare_database.sh -h $host mysql scm scm
    if {[init_mysql_passwd $passwd $cmd]} {
        exit 1
    }
} else {
    puts "exec type error"
}

expect eof

5.4 軟體安裝列表

expect
bc
net-tools
iotop
zip
unzip
telnet
wget
iperf3
fio
ntfs-3g
lzo
iftop
vim
lrzsz
net-tools

運行結果


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1、WebFirst框架描述 WebFirst 是果糖大數據團隊開發的新一代 高性能 代碼生成器&資料庫設計工具,由.net core + sqlsugar 開發 導入1000個表只要1-2秒,用法簡單,功能強大,支持多種資料庫 ,具體功能如下: 一、 建庫、CodeFirst方式線上建表,沒用到C ...
  • 一、docker安裝 centos安裝docker sudo yum update sudo yum install -y yum-utils device-mapper-persistent-data 1vm2 sudo yum-config-manager --add-repo http://m ...
  • 鏡像下載、功能變數名稱解析、時間同步請點擊 阿裡雲開源鏡像站 畢設要開始做Apollo相關的課題,自己在這方面完全就是一個小白,光是安裝apollo就已經花了不少功夫,也走了一些彎路,所以在這裡記錄一下,一方面做個總結,另一方面也希望可以幫到和我一樣的朋友,最近在做Apollo的朋友也可以點個關註,大家一起 ...
  • 一、單個文件上傳 前端代碼可參考elementUI,後端代碼主要上傳一個文件MultipartFile multipartFile @PostMapping("/upload") public ObjectRestResponse uploadKnowledge(@RequestParam(valu ...
  • 本文例子參考《STM32單片機開發實例——基於Proteus虛擬模擬與HAL/LL庫》 源代碼:https://github.com/LanLinnet/STM33F103R6 項目要求 通過定時器中斷的方式,實現流水燈的效果。 硬體設計 在第一節的基礎上,在Proteus中添加電路如下圖所示。 在 ...
  • 一、window 概念 視窗(window)是處理無限流的核心。視窗將流分割成有限大小的“桶”,我們可以在桶上應用計算。本文檔重點介紹如何在Flink中執行視窗操作,以及程式員如何從其提供的功能中獲得最大的好處。 一個有視窗的Flink程式的一般結構如下所示。第一個片段指的是鍵控流,而第二個片段指的 ...
  • 本文力求以簡單易懂的語言描述出資料庫發展史,儘量避免出現複雜的概念介紹。資料庫演進史如圖1所示: 圖1 資料庫演進 一、穿孔紙帶和文件系統 在現代意義的資料庫出現之前(20世紀60年代),人們通過人工和文件系統的方式來存儲、管理數據。在人工管理時期,人們常使用穿孔紙帶來管理數據(圖2),雖然穿孔紙帶 ...
  • 19c單實例配置GoldenGate 併進行用戶數據同步測試 當前數據使用的是經典模式配置GoldenGate 進行數據同步,關於容器資料庫配置GoldenGate 請參閱: Configuring Oracle GoldenGate in a Multitenant Container Datab ...
一周排行
    -Advertisement-
    Play Games
  • 分組和樹形結構是不一樣的。 樹形結構是以遞歸形式存在。分組是以鍵值對存在的形式,類似於GroupBy這樣的形式。 舉個例子 ID NAME SEX Class 1 張三 男 1 2 李四 女 2 3 王二 男 1 當以Sex為分組依據時則是 Key Value 男 1 張三 男 1 3 王二 男 1 ...
  • NetCore中將SQLServer資料庫備份為Sql腳本 描述: 最近寫項目收到了一個需求, 就是將SQL Server資料庫備份為Sql腳本, 如果是My Sql之類的還好說, 但是在網上搜了一大堆, 全是教你怎麼操作SSMS的, 就很d疼! 解決方案: 通過各種查找資料, 還有一些老哥的幫助, ...
  • 我的Notion Clowd.Squirrel Squirrel.Windows 是一組工具和適用於.Net的庫,用於管理 Desktop Windows 應用程式的安裝和更新。 Squirrel.Windows 對 Windows 應用程式的實現語言沒有任何要求,甚至無需服務端即可完成增量更新。 ...
  • 轉載請註明來源 https://www.cnblogs.com/brucejiao/p/16188865.html 謝謝! 轉載請註明來源 https://www.cnblogs.com/brucejiao/p/16188865.html 謝謝! 轉載請註明來源 https://www.cnblog ...
  • 1. Netty源碼研究筆記(3)——Channel系列 依舊是通過先縱向再橫向的研究方法,在開篇中,我們發現不管是Sever還是Client,最終的啟動是通過調用channel的對應方法來完成的,而這個動作實際在channel綁定的eventLoop中執行。 接下來,我們繼續EchoSever、E ...
  • 大家好,今天給大家介紹一款輕量、快速、穩定可編排的組件式規則引擎框架LiteFlow。 一、LiteFlow的介紹 LiteFlow官方網站和代碼倉庫地址 官方網站:https://yomahub.com/liteflow Gitee托管倉庫:https://gitee.com/dromara/li ...
  • 我使用Spring AOP實現了用戶操作日誌功能 今天答辯完了,復盤了一下系統,發現還是有一些東西值得拿出來和大家分享一下。 需求分析 系統需要對用戶的操作進行記錄,方便未來溯源 首先想到的就是在每個方法中,去實現記錄的邏輯,但是這樣做肯定是不現實的,首先工作量大,其次違背了軟體工程設計原則(開閉原 ...
  • 《零基礎學Java》 繪製幾何圖形 Java可以分別使用 Graphics 和 Graphics2D 繪製圖形,Graphics類 使用不同的方法繪製不同的圖形(drawLine()方法可f以繪製線、drawRect()方法用於繪製矩形、drawOval()方法用於繪製橢圓形)。 Graphics類 ...
  • 本期教程人臉識別第三方平臺為虹軟科技,本文章講解的是人臉識別RGB活體追蹤技術,免費的功能很多可以自行搭配,希望在你看完本章課程有所收穫。 ...
  • 很多人都喜歡使用黑色的主題樣式,包括我自己,使用了差不多三年的黑色主題,但是個人覺得在進行視窗轉換的時候很廢眼睛。 比如IDEA是全黑的,然後需要看PDF或者WORD又變成白色的了,這樣來回切換導致眼睛很累,畢竟現在網頁以及大部分軟體的界面都是白色的。那麼還是老老實實的使用原來比較順眼的模式吧。 1 ...