大数据开发中常用组件服务的集群管理脚本整理集合

大数据环境相关脚本

bash运行模式说明

bash的运行模式可分为login shell(用户名、密码登录)和non-login shell(SSH登录)。

两者登录方式加载情况:

1.login shell启动时会加载:/etc/profile,~/.bash_profile,~/.bashrc

2.non-login shell启动时会加载:~/.bashrc

注意:~/.bashrc实则会加载/etc/bashrc/etc/bashrc又加载/etc/profile.d/*.sh

SSH登录注意:

当SSH到其他节点的时候是使用non-login shell模式,默认是不加载/etc/profile配置文件,也就会导致环境变量信息未配置,从而会找不到部分命令的问题

创建shell脚本目录

创建/root/shell目录,用于存放shell脚本

/etc/bashrc文件配置shell脚本目录,将其作为环境变量,以便在任何地方使用新建的shell脚本

# My Shell
export PATH=$PATH:/root/shell

配置hosts

配置每个节点的hosts文件,设置节点IP与Name的映射。

vim /etc/hosts

172.29.234.1 node01
172.29.234.2 node02
172.29.234.3 node03
172.29.234.4 node04
172.29.234.5 node05

SSH自动配置脚本

执行脚本来自动配置各个节点免密登录。

vim ssh_config.sh

#! /bin/bash
function sshPasswordLogin() {
    1. 检测expect服务是否存在,不存在则使用yum安装expect
    expectIsExists=$(rpm -qa | grep expect)
    if [ -z "$expectIsExists" ]; then
        yum -y install expect
    fi

    1. 密钥对不存在则创建密钥
    if [ ! -f /root/.ssh/id_rsa.pub ]; then
        ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa
    fi

    1. 服务器列表
    1. servers=("IP地址1 用户名1 密码1" "IP地址2 用户名2 密码2" "IP地址3 用户名3 密码3")
    servers=("node01 root 123456" "node02 root 123456" "node03 root 123456" "node04 root 123456" "node05 root 123456")

    for server in "${servers[@]}"; do
        hostname=$(echo "$server" | cut -d " " -f1)
        username=$(echo "$server" | cut -d " " -f2)
        password=$(echo "$server" | cut -d " " -f3)

        echo "Configuring password login on $hostname..."

        expect