摘自:http://www.wangsenfeng.com/articles/2016/10/27/1477556261953.html
1 概述
最近自己写了一个Hadoop自动化部署脚本,包括Hadoop集群自动化部署脚本和Hadoop增加单节点自动化部署脚本。需要快速部署Hadoop集群的童鞋可以使用该脚本。这些脚本我在用5台虚拟机进行了测试,如果在使用中还有bug,欢迎指出。本文主要介绍Hadoop集群自动化部署脚本,安装的Hadoop版本为2.6.0。
2 依赖
安装Hadoop2.6.0集群需要依赖JDK和Zookeeper。本文安装的JDK版本为jdk-7u60-linux-x64,Zookeeper版本为zookeeper-3.4.6。
3 各文件及配置说明
该部署脚本由两部分构成:root用户下执行的脚本和Hadoop启动用户下执行的脚本。这些脚本都只需要在一台服务器上执行即可,执行脚本的服务器作为Hadoop的Master服务器。下面分别进行说明。
3.1 root脚本说明
root脚本的目录结构如下:
- conf — 配置文件目录
- init.conf
- expect — expect脚本目录
- password.expect
- scp.expect
- otherInstall.expect
- file — 安装文件目录
installRoot.sh — 脚本执行文件- hadoop-2.6.0.tar.gz
- jdk-7u60-linux-x64.tar.gz
- zookeeper-3.4.6.tar.gz
3.1.1 conf目录
该目录下的init.conf文件为root执行脚本使用的配置文件,在执行脚本之前需要对该配置文件进行修改。文件内容如下:
#jdk file and version
JDK_FILE_TAR=jdk-7u60-linux-x64.tar.gz
#jdk unpack name
JDK_FILE=jdk1.7.0_60
#java home
JAVAHOME=/usr/java
#Whether install the package for dependence,0 means no,1 means yes
IF_INSTALL_PACKAGE=1
#host conf
ALLHOST="hadoop1master hadoop1masterha hadoop1slave1 hadoop1slave2 hadoop1slave3"
ALLIP="192.168.0.180 192.168.0.184 192.168.0.181 192.168.0.182 192.168.0.183"
#zookeeper conf
ZOOKEEPER_TAR=zookeeper-3.4.6.tar.gz
ZOOKEEPERHOME=/usr/local/zookeeper-3.4.6
SLAVELIST="hadoop1slave1 hadoop1slave2 hadoop1slave3"
#hadoop conf
HADOOP_TAR=hadoop-2.6.0.tar.gz
HADOOPHOME=/usr/local/hadoop-2.6.0
HADOOP_USER=hadoop2
HADOOP_PASSWORD=hadoop2
#root conf: $MASTER_HA $SLAVE1 $SLAVE2 $SLAVE3
ROOT_PASSWORD="hadoop hadoop hadoop hadoop"
下面是个别参数的解释及注意事项:
- ALLHOST为Hadoop集群各个服务器的hostname,使用空格分隔;ALLIP为Hadoop集群各个服务器的ip地址,使用空格分隔。要求ALLHOST和ALLIP要一一对应。
- SLAVELIST为zookeeper集群部署的服务器的hostname。
- ROOT_PASSWORD为除了Master服务器以外的其他服务器root用户的密码,使用逗号隔开。(在实际情况下,可能各个服务器的root密码并不相同。)
3.1.2 expect目录
该目录下包含password.expect、scp.expect、otherInstall.expect三个文件。password.expect用来设置hadoop启动用户的密码;scp.expect用来远程传输文件;otherInstall.expect用来远程执行其他服务器上的installRoot.sh。这三个文件都在installRoot.sh中被调用。
password.expect文件内容如下:
#!/usr/bin/expect -f
set user [lindex $argv 0]
set password [lindex $argv 1]
spawn passwd $user
expect "New password:"
send "$password
"
expect "Retype new password:"
send "$password
"
expect eof
其中argv 0和argv 1都是在installRoot.sh脚本中进行传值的。其他两个文件argv *也是这样传值的。
scp.expect文件内容如下:
#!/usr/bin/expect -f
# set dir, host, user, password
set dir [lindex $argv 0]
set host [lindex $argv 1]
set user [lindex $argv 2]
set password [lindex $argv 3]
set timeout -1
spawn scp -r $dir $user@$host:/root/
expect {
"(yes/no)?"
{
send "yes
"
expect "*assword:" { send "$password
"}
}
"*assword:"
{
send "$password
"
}
}
expect eof
otherInstall.expect文件内容如下:
#!/usr/bin/expect -f
# set dir, host, user, password
set dir [lindex $argv 0]
set name [lindex $argv 1]
set host [lindex $argv 2]
set user [lindex $argv 3]
set password [lindex $argv 4]
set timeout -1
spawn ssh -q $user@$host "$dir/$name"
expect {
"(yes/no)?"
{
send "yes
"
expect "*assword:" { send "$password
"}
}
"*assword:"
{
send "$password
"
}
}
expect eof
3.1.3 file目录
这里就是安装Hadoop集群及其依赖所需的安装包。
3.1.4 installRoot.sh脚本
该脚本是在root用户下需要执行的脚本,文件内容如下:
#!/bin/bash
if [ $USER != "root" ]; then
echo "[ERROR]:Must run as root"; exit 1
fi
# Get absolute path and name of this shell
readonly PROGDIR=$(readlink -m $(dirname $0))
readonly PROGNAME=$(basename $0)
hostname=`hostname`
source /etc/profile
# import init.conf
source $PROGDIR/conf/init.conf
echo "install start..."
# install package for dependence
if [ $IF_INSTALL_PACKAGE -eq 1 ]; then
yum -y install expect >/dev/null 2>&1
echo "expect install successful."
# yum install openssh-clients #scp
fi
#stop iptables or open ports, now stop iptables
service iptables stop
chkconfig iptables off
FF_INFO=`service iptables status`
if [ -n "`echo $FF_INFO | grep "Firewall is not running"`" ]; then
echo "Firewall is already stop."
else
echo "[ERROR]:Failed to shut down the firewall.Exit shell."
exit 1
fi
#stop selinux
setenforce 0
SL_INFO=`getenforce`
if [ $SL_INFO == "Permissive" -o $SL_INFO == "disabled" ]; then
echo "selinux is already stop."