虽然整体实现的自动安装,但还是有很多需要完善的地方,比如说:
1. 代码目前只能在root权限下运行,否则会出错,这方面需要加权限判断;
2.另外可以增加几个函数,减少代码冗余;
3.还有一些判断不够智能;
......
苦于能力和时间都有限,只能写到这里了。
installHadoop文件代码如下:
#!/bin/bash#root_password="123456"jdk_tar=jdk-8u65-linux-i586.tar.gzjdk_url=http://download.oracle.com/otn-pub/java/jdk/8u65-b17/jdk-8u65-linux-i586.tar.gzjdk_version=jdk1.8.0_65java_version=1.8.0_65jdk_install_path=/usr/local/developmenthadoop_url=http://101.44.1.4/files/2250000004303235/mirrors.hust.edu.cn/apache/hadoop/common/stable1/hadoop-1.2.1.tar.gzhadoop_version=hadoop-1.2.1hadoop_tar=hadoop-1.2.1.tar.gzhadoop_install_path=hadoophadoop_tmp_path=/home/hadoop/hadoop_tmphadoop_name_path=/home/hadoop/hdfs/namehadoop_data_path=/home/hadoop/hdfs/datauser_name=hadoopuser_passwd=hadoop#su #判断能否root#if [ $? -ne 0 ] ;then# echo "No root access"# exit#fishFilePath=$(pwd)#check jdk installed or notjava -version &> /dev/nullif [ $? -ne 0 ] ;then echo {Jdk has been installed in this pc} java -versionelse #检查~/../usr/local/development目录存在否,不存在就创建 #先进入当前用户的家目录 #cd ~ #cd ../../usr/local/$jdk_install_path &> /dev/null #if [ $? -ne 0 ] ;then if [ ! -d $jdk_install_path ] ;then echo "{Create $jdk_install_path folder to install jdk}" mkdir $jdk_install_path cd $jdk_install_path echo "{Success to create $jdk_install_path folder}" else echo "{$jdk_install_path folder has already exists}" cd $jdk_install_path fi #检查jdk是否解压 #ls | grep "$jdk_version" &> /dev/null if [ ! -d $jdk_version ] ;then #检查jdk是否已有压缩包 if [ ! -f $jdk_tar ] ;then echo "{Download $jdk_tar}" wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" $jdk_url fi echo "{Untar $jdk_tar}" tar -zxvf $jdk_tar else echo "{$jdk_version folder has already exists in $jdk_install_path/}" fi #set jdk environment echo {set java environment} cd ~ cd ../../../../etc/profile.d/ touch $jdk_install_path.sh #echo "#!bin/bash" > $jdk_install_path.sh echo "export JAVA_HOME=/usr/local/$jdk_install_path/$jdk_version" >> $jdk_install_path.sh echo "export JRE_HOME=\$JAVA_HOME/jre" >> $jdk_install_path.sh echo "export CLASSPATH=.:\$JAVA_HOME/lib:\$JRE_HOME/lib:\$CLASSPATH" >> $jdk_install_path.sh echo "PATH=\$JAVA_HOME/bin:\$JRE_HOME/bin:\$PATH" >> $jdk_install_path.sh source $jdk_install_path.sh #check the java version java -version | grep "$java_version" &> /dev/null if [ $? -ne 0 ] ;then echo "{Success to install $jdk_version}" fifi#no passwd when login via ssecho "{Config ssh service and login via ssh without no passwd}"sudo yum -y install ssh openssh-server#update /etc/ssh/sshd_config#RSAAuthenticationRSAAuthentication_lineNum=`awk '/RSAAuthentication yes/{print NR}' ~/../etc/ssh/sshd_config`RSAAuthentication="RSAAuthentication yes"sed -i "${RSAAuthentication_lineNum}s/^.*/${RSAAuthentication}/g" ~/../etc/ssh/sshd_config#PubkeyAuthenticationPubkeyAuthentication_lineNum=`awk '/PubkeyAuthentication yes/{print NR}' ~/../etc/ssh/sshd_config`PubkeyAuthentication="PubkeyAuthentication yes"sed -i "${PubkeyAuthentication_lineNum}s/^.*/${PubkeyAuthentication}/g" ~/../etc/ssh/sshd_config#AuthorizedKeysFileAuthorizedKeysFile_lineNum=`awk '/AuthorizedKeysFile/{print NR}' ~/../etc/ssh/sshd_config`AuthorizedKeysFile="AuthorizedKeysFile .ssh\/authorized_keys"sed -i "${AuthorizedKeysFile_lineNum}s/^.*/${AuthorizedKeysFile}/g" ~/../etc/ssh/sshd_configecho "{You change in sshd_config as follow}"sed -n "${RSAAuthentication_lineNum},${AuthorizedKeysFile_lineNum}p" ~/../etc/ssh/sshd_config#restart sshd service~/../sbin/service sshd restartecho "{Finish to update sshd_config}"#generate public keyif [ ! -d ~/.ssh ] ;then mkdir ~/.sshficd ~/.sshecho y | ssh-keygen -t rsa -P '' -f id_rsaif [ ! -f authorized_keys ] ;then touch authorized_keys cat id_rsa.pub > authorized_keyselse cat id_rsa.pub >> authorized_keysfichmod 700 ~/.sshchmod 600 ~/.ssh/authorized_keys#Download hadoopcd ~cd ../home/$hadoop_install_path &> /dev/nullif [ $? -ne 0 ] ;then echo "{Create /home/$hadoop_install_path folder to install jdk}" cd ../home mkdir $hadoop_install_path cd $hadoop_install_path echo "{Success to create $$hadoop_install_path folder}"else echo "{/home/$hadoop_install_path folder has already exists}" cd ~ cd ../home/$hadoop_install_pathfi#check hadoop-2.7.0 folder is exists or notif [ ! -d "$hadoop_version" ] ;then #check hadoop-2.7.0.tar.gz is exist or not if [ ! -f "$hadoop_tar" ] ;then echo "{Download $hadoop_tar}" wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" $hadoop_url fi echo "{Untar $hadoop_tar}" tar -zxvf $hadoop_tarelse echo "{$hadoop_version folder has already exists in /home/$hadoop_install_path/}"fi#enter into config foldercd $hadoop_versionif [ ! -d "conf" ] ;then cd etc/hadoop/else cd conffi#update hadoop-env.shjava_home_line_num=`awk '/export JAVA_HOME/{print NR}' hadoop-env.sh`JAVAHOME="export JAVA_HOME=\/usr\/local\/"$jdk_install_path"\/"$jdk_version#-i is directly modify the source filesed -i "${java_home_line_num}s/^.*/${JAVAHOME}/g" hadoop-env.shcat hadoop-env.sh | grep "JAVA_HOME"echo "{Finish to update hadoop-env.sh}"hadoop_config_path=$(pwd)#echo $cur_path#echo $shFilePath#unalias cp#cp -rf core-site.xml $curPath/cd $shFilePath#update core_site.xmlcat core-site.xml > $hadoop_config_path/core-site.xmlif [ ! -d $hadoop_tmp_path ] ;then mkdir $hadoop_tmp_pathfirm -rf $hadoop_tmp_path/*if [ ! -d $hadoop_name_path ] ;then mkdir $hadoop_name_pathfichmod g-w $hadoop_name_pathrm -rf $hadoop_name_path/*if [ ! -d $hadoop_data_path ] ;then mkdir $hadoop_data_pathfichmod g-w $hadoop_data_pathrm -rf $hadoop_data_path/*#update mapred-site.xmlcat mapred-site.xml > $hadoop_config_path/mapred-site.xml#update hdfs-site.xmlcat hdfs-site.xml > $hadoop_config_path/hdfs-site.xmlcd $hadoop_config_pathecho "{Check core-site.xml}"#cat core-site.xmlecho "{Check mapred-site.xml}"#cat mapred-site.xmlecho "{Check hdfs-site.xml}"#cat hdfs-site.xmlecho "{Finish config hadoop}"#add hadoop account and has admin accessid $user_nameif [ $? -ne 0 ] ;then echo "{add $user_name}" sudo useradd -mr $user_namefi#set passwd for hadoop accountecho $user_passwd | sudo passwd --stdin $user_nameecho "{Format hadoop}"echo Y | ../bin/hadoop namenode -formatcd ../bin/bash stop-all.shecho "{Start hadoop}"bash start-all.shresult=`jps | awk '{print $2}' | xargs`expect_result="JobTracker NameNode DataNode TaskTracker Jps SecondaryNameNode"if [ "$result" == "$expect_result" ] ;then echo "{Congratulations!!! Success to intall hadoop!}"else echo "{Sorry, fail to install hadoop and try to restart hadroop!}" bash stop-all.sh echo "{Start hadoop}" bash start-all.sh result=`jps | awk '{print $2}' | xargs` if [ "$result" == "$expect_result" ] ;then echo "{Sorry, fail to find all java thread and please check!}" else echo "{Congratulations!!! find all java thread, success to install hadoop!}" fifiecho {!!!finish!!!}
此外为了实现自动化配置hadoop, 还需要把core-site.xml, hdfs-site.xml和mapred-site.xml文件放到与installHadoop文件同级目录下。
core-site.xml文件:
fs.default.name hdfs://localhost:9000 hadoop.tmp.dir /home/hadoop/hadoop_tmp A base for other temporary directories.
hdfs-site.xml:
dfs.name.dir /home/hadoop/hdfs/name dfs.data.dir /home/hadoop/hdfs/data dfs.replication 1
mapred-site.xml
mapred.job.tracker localhost:9001
单机版的Hadoop安装可以参考:
http://www.linuxidc.com/Linux/2015-04/116447.htm
多台机器的Hadoop安装可以参考:
http://blog.csdn.net/ab198604/article/details/8250461