hadoop-bigdata-v2.2 版本shell脚本一键部署 master、slaver1、slaver2三个节点源代码.

#/bin/bash
printf "\033[33m \n ------>   准备开始部署平台一键脚本........   <------- \n\033[0m"
sleep 2
printf "\033[33m \n ------>   温馨提示:请写入正确的IP到ip.txt文件中,若写错则无法完成部署服务!!!!\n\033[0m"
sleep 2  
printf "\033[33m \n ------>   准备五秒后开始部署---->5s........   <------- \n\033[0m"
sleep 1
printf "\033[33m \n ------>   准备四秒后开始部署---->4s........   <------- \n\033[0m"
sleep 1
printf "\033[33m \n ------>   准备三秒后开始部署---->3s........   <------- \n\033[0m"
sleep 1
printf "\033[33m \n ------>   准备二秒后开始部署---->2s........   <------- \n\033[0m"
sleep 1
printf "\033[33m \n ------>   准备一秒后开始部署---->1s........   <------- \n\033[0m"
sleep 1
printf "\033[33m \n ------>    准备开始部署---->0s........   <------- \n\033[0m"




#避免公钥回答    




printf "\033[29m \n ------>  避免公钥验证........   -------> \n\033[0m"
sed -i 's/#   StrictHostKeyChecking ask/StrictHostKeyChecking no/' /etc/ssh/ssh_config


sleep 0.5


sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/ssh_config


sleep 0.5


service sshd restart
if [[ "0" -eq "$?" ]]
then
echo "重启sshd服务完成!"
sleep 1
else
echo "重启sshd服务失败!"
fi
sleep 2






#检索所需内容是否齐全


printf "\033[36m \n ---------->   检测脚本所需附件是否存在...\n\033[0m" 
sleep 3


ls ip.txt
if [[ "0" -eq "$?" ]]
then
echo "ip.txt文件存在,下一步!"
else
echo "ip.txt文件不存在,请退出后手动编写!"
sleep 2000
fi


sleep 5


#检索Centos镜像


ls CentOS-7-x86_64-DVD-1511.iso
if [[ "0" -eq "$?" ]]
then
echo "centos镜像存在,下一步!"
else
echo "centos镜像不存在,正在尝试导入中......"
sleep 3
D=`awk 'NR==4{print$1}' ip.txt`
scp $D:/root/CentOS-7-x86_64-DVD-1511.iso /root
if [[ "0" -eq "$?" ]]
then
echo "完成导入!"
else
echo "请将镜像源IP写入ip.txt文件中第四行!,请退出!!!"
sleep 200
fi
sleep 1
echo "Centos镜像导入完成!,下一步!"
fi


sleep 3


#检索XianDian镜像




ls XianDian-BigData-v2.2.iso
if [[ "0" -eq "$?" ]]
then
echo "BigData镜像存在,下一步!"
else
echo "BigData镜像不存在,在尝试导入中......"
sleep 2
D=`awk 'NR==4{print$1}' ip.txt`
scp $D:/root/XianDian-BigData-v2.2.iso  /root
if [[ "0" != "$?" ]]
then
echo "请将镜像源IP写入ip.txt文件中第四行!,请退出!!!"
sleep 200
else
echo "完成导入!"
fi
sleep 1
echo "BigData镜像导入完成!下一步!"
fi


sleep 2


#获取Slaver1、Slaver2节点IP


slaver1=`awk 'NR==2{print$1}' ip.txt`
ssh -t -t $slaver1  -o StrictHostKeyChecking=no "ip a|grep inet|awk 'NR==3{print\$2}'|sed 's/\/24//' > slaver1 "
if [[ "0" -eq "$?" ]]
then
echo "执行slaver1地址ip获取成功.."
sleep 2
else
echo "执行slaver1地址ip获取失败.."
fi




slaver2=`awk 'NR==3{print$1}' ip.txt`
ssh -t -t $slaver2  -o StrictHostKeyChecking=no "ip a|grep inet|awk 'NR==3{print\$2}'|sed 's/\/24//' > slaver2 "
if [[ "0" -eq "$?" ]]
then
echo "执行slaver2地址ip获取成功.."
sleep 2
else
echo "执行slaver2地址ip获取失败.."
fi








#解压部署所需要的安装包


printf "\033[36m \n --->   正在解压centos文件到/opt目录.... \n\033[1m"
mkdir /opt/centos
mount -o loop CentOS-7-x86_64-DVD-1511.iso /opt/centos/
if [[ "0" -eq "$?" ]]
then
echo "挂载操作成功!"
else
echo "挂载操作失败!"
sleep 5
fi
printf "\033[36m \n --->   解压centos完成!.... \n\033[0m"
sleep 2


printf "\033[34m \n --->   正在尝试解压ambari安装包.... \n\033[0m"
mount -o loop XianDian-BigData-v2.2.iso  /mnt/
cp -rvf /mnt/ambari-2.6.0.0 /opt/
if [[ "0" -eq "$?" ]]
then
echo "ambari文件导出成功!进入下一步..."
sleep 1
else
echo "ambari文件导出失败!"
sleep 10
fi




#部署jdk




printf "\033[29m \n ------>   部署jdk中........   -------> \n\033[0m"
mkdir /usr/jdk64
if [[ "0" -eq "$?" ]]
then
echo "创建jdk文件夹成功,进入下一步..."
sleep 1
else
echo "创建jdk文件夹失败..."
sleep 10
fi


tar -zxvf /mnt/jdk-8u77-linux-x64.tar.gz  -C /usr/jdk64
if [[ "0" -eq "$?" ]]
then
echo "解压jdk压缩包完成,进入下一步..."
sleep 1
else
echo "解压jdk压缩包失败..."
sleep 10
fi


sleep 0.5


cat >> /etc/profile <<-EOF
export JAVA_HOME=/usr/jdk64/jdk1.8.0_77
export PATH=\$JAVA_HOME/bin:\$PATH
EOF
if [[ "0" -eq "$?" ]]
then
echo "写入成功。"
sleep 1
else
echo "写入失败"
sleep 10
fi


source /etc/profile
java -version
sleep 1
printf "\033[34m \n ------>  部署、验证成功........   -------> \n\033[0m"


sleep 0.5






#更改主机名






printf "\033[29m \n ------>  更改主机名儿........   -------> \n\033[0m"
hostnamectl set-hostname master
name=`hostname`
if [[ "master" -eq "$name" ]]
then
echo "主机名修改正确"
sleep 1
else
echo "主机名修改错误"
fi
ip=`ifconfig|grep inet|awk 'NR==1{print$2}'`
echo "$ip master.hadoop master"  >> /etc/hosts








#避免公钥验证 Yes/No 回答




printf "\033[29m \n ------>  避免公钥验证操作中........   -------> \n\033[0m"
sed -i 's/#   StrictHostKeyChecking ask/StrictHostKeyChecking no/' /etc/ssh/ssh_config


sleep 0.5


sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/ssh_config


sleep 0.5


service sshd restart
if [[ "0" -eq "$?" ]]
then
echo "重启sshd服务完成!"
sleep 1
else
echo "重启sshd服务失败!"
fi
sleep 2




#编写yum源


printf "\033[29m \n ------>  重新写入yum源中........   -------> \n\033[0m"


rm -rvf /etc/yum.repos.d/*
if [[ "0" -eq "$?" ]]
then
echo "写入yum源成功!"
sleep 1
else
echo "写入yum源失败!"
fi
sleep 2


touch /etc/yum.repos.d/local.repo
cat >> /etc/yum.repos.d/local.repo <<-EOF
[centos]
name=centos
baseurl=file:///opt/centos/
gpgcheck=0
enabled=1
[ambari]
name=ambari
baseurl=file:///opt/ambari-2.6.0.0/
gpgcheck=0
enabled=1
EOF
printf "\033[29m \n ------>  验证yum源是否成功........   -------> \n\033[0m"
yum clean all
yum makecache
if [[ "0" -eq "$?" ]]
then
echo "验证yum源成功!"
sleep 2
else
echo "验证yum源失败..."
sleep 10
fi




sleep 1.5


printf "\033[34m \n ------>  yum源分发中........   -------> \n\033[0m"
slaver1=`awk 'NR==2{print$1}' ip.txt`
sed  's/file:\/\/\//ftp:\/\//g' /etc/yum.repos.d/local.repo > 1
if [[ "0" -eq "$?" ]]
then
echo "更改yum源文件成功!"
sleep 2
else
echo "更改yum源文件失败"
fi


sed  's/opt/master/' 1 > slaver1.repo
if [[ "0" -eq "$?" ]]
then
echo "更改yum源成功!"
sleep 2
else
echo "更改yum源失败"
fi
scp   slaver1.repo $slaver1:/etc/yum.repos.d/
if [[ "0" -eq "$?" ]]
then
echo "传输yum源文件成功1!"
sleep 2
else
echo "传输yum源文件失败1"
fi
sleep 1


slaver2=`awk 'NR==3{print$1}' ip.txt`
scp   slaver1.repo $slaver2:/etc/yum.repos.d/
if [[ "0" -eq "$?" ]]
then
echo "传输yum源文件成功2!"
sleep 2
else
echo "传输yum源文件失败2"
sleep 10
fi
sleep 1




printf "\033[29m \n ------>  安装FTP服务........   -------> \n\033[0m"
yum install vsftpd -y
echo 'anon_root=/opt' >> /etc/vsftpd/vsftpd.conf
printf "\033[29m \n ------>  重启服务中........   -------> \n\033[0m"
systemctl restart vsftpd
systemctl enable vsftpd
printf "\033[34m \n ------>  重启成功........   -------> \n\033[0m"




echo never > /sys/kernel/mm/transparent_hugepage/enabled 
echo never > /sys/kernel/mm/transparent_hugepage/defrag 




printf "\033[29m \n ------>  正在安装HTTP服务中........   -------> \n\033[0m"
yum install httpd -y
printf "\033[34m \n ------>  安装成功........   -------> \n\033[0m"
sleep 1
printf "\033[29m \n ------>  文件复制中........   -------> \n\033[0m"
cp -rvf /mnt/HDP-* /var/www/html/
sleep 1
printf "\033[29m \n ------> 重启服务中........   -------> \n\033[0m"
systemctl restart httpd
systemctl enable httpd
sleep 1


printf "\033[29m \n ------> 五秒后安装ambari-server........   -------> \n\033[0m"
sleep 5
yum install ambari-server -y
if [[ "0" -eq "$?" ]]
then
echo "ambari-server 安装成功!"
sleep 1
else
echo "ambari-server 安装失败..."
sleep 20
fi


printf "\033[29m \n ------> 三秒后安装expect组件........   -------> \n\033[0m"
sleep 3
yum install expect -y
if [[ "0" -eq "$?" ]]
then
echo "expect组件安装完成!"
sleep 1
else
echo "expect组件安装失败!"
sleep 10
fi




#IP地址映射写入


printf "\033[29m \n ------> 制作地址映射中........   -------> \n\033[0m"
sleep 3


slaver1=`awk 'NR==2{print$1}' ip.txt`
expect -c "
spawn ssh-copy-id $ip
expect \"*?\" {send \"yes\n\"}
expect eof
"
sleep 1


slaver1=`awk 'NR==2{print$1}' ip.txt`
scp $slaver1:/root/slaver1 /root
echo "`cat slaver1`  slaver1.hadoop slaver1" >> /etc/hosts
if [[ "0" -eq "$?" ]]
then
echo "写入slaver1地址ip成功!"
sleep 2
else
echo "写入slaver1地址ip失败"
sleep 10
fi




slaver2=`awk 'NR==3{print$1}' ip.txt`
expect -c "
spawn ssh-copy-id $ip
expect \"*?\" {send \"yes\n\"}
expect eof
"
sleep 1


slaver2=`awk 'NR==3{print$1}' ip.txt`
scp $slaver2:/root/slaver2 /root
echo "`cat slaver2`  slaver2.hadoop slaver2"  >> /etc/hosts
if [[ "0" -eq "$?" ]]
then
echo "写入slaver2地址ip成功!"
sleep 2
else
echo "写入slaver2地址ip失败"
sleep 10
fi




sleep 1


#分发地址映射文件
printf "\033[29m \n ------>  分发文件中........   -------> \n\033[0m"


slaver1=`awk 'NR==2{print$1}' ip.txt`
scp /etc/hosts  $slaver1:/etc/
if [[ "0" -eq "$?" ]]
then
echo "分发slaver1操作完成!"
sleep 2
else
echo "分发Slaver1操作失败,正在退出"
fi
sleep 1




slaver2=`awk 'NR==3{print$1}' ip.txt`
scp /etc/hosts  $slaver2:/etc/
if [[ "0" -eq "$?" ]]
then
echo "分发slaver2操作完成!"
sleep 2
else
echo "分发Slaver2操作失败!"
fi


sleep 1






#安装数据库服务




printf "\033[29m \n ------> 三秒后——安装数据库所需要的服务........   -------> \n\033[0m"
sleep 3
yum install mariadb mariadb-server mysql-connector-java -y


printf "\033[29m \n ------> 重启服务中........   -------> \n\033[0m"
sleep 1
systemctl enable mariadb
systemctl restart mariadb
printf "\033[34m \n ------>  重启完成........   -------> \n\033[0m"










#初始化数据库


printf "\033[29m \n ------>  初始化数据库中........   -------> \n\033[0m"
sleep 0.5
(echo -e "\n"
sleep 1
echo -e "bigdata"
sleep 1
echo -e "bigdata"
sleep 1
echo -e "y\n"
sleep 1
echo -e "n\n"
sleep 1
echo -e "y\n"
sleep 1
echo -e "y\n")|mysql_secure_installation
sleep 0.5
printf "\033[34m \n ------>  初始化完成........   -------> \n\033[0m"


sleep 1






#创建数据库


printf "\033[29m \n ------>  五秒后为您创建数据库,请稍后........   -------> \n\033[0m"
sleep 5
mysql -uroot -pbigdata -e "create database ambari;"
mysql -uroot -pbigdata -e "grant all privileges on ambari.* to 'ambari'@'localhost' identified by 'bigdata';"
mysql -uroot -pbigdata -e "grant all privileges on ambari.* to 'ambari'@'%' identified by 'bigdata';"
mysql -uroot -pbigdata -e "create database hive;"
mysql -uroot -pbigdata -e "grant all privileges on hive.* to 'hive'@'localhost' identified by 'bigdata';"
mysql -uroot -pbigdata -e "grant all privileges on hive.* to 'hive'@'%' identified by 'bigdata';"
printf "\033[34m \n ------>  创建数据库成功........   -------> \n\033[0m"
sleep 0.2


printf "\033[29m \n ------>  正在为您导入数据库中,请稍后........   -------> \n\033[0m"
sleep 0.5
(echo -e "use ambari;"
sleep 1
echo -e "source  /var/lib/ambari-server/resources/Ambari-DDL-MySQL-CREATE.sql;"
sleep 6
echo -e "exit\n")|mysql -uroot -pbigdata
printf "\033[34m \n ------>  导入数据库成功........   -------> \n\033[0m"
sleep 0.2










#始化ambari服务




printf "\033[29m \n ------>  五秒后为您初始化ambari服务,请稍后........   -------> \n\033[0m"
sleep 5
expect -c "
spawn /etc/rc.d/init.d/ambari-server setup
expect \"*? \"
send \"n\n\"
expect \"Enter choice (1): \"
send \"3\r\"
expect \"Path to JAVA_HOME: \"
send \"/usr/jdk64/jdk1.8.0_77\r\"
expect \"*? \"
send \"y\r\"
expect \"Enter choice (1): \"
send \"3\r\"
expect \"Hostname (localhost): \"
send \"localhost\r\"
expect \"Port (3306): \"
send \"3306\r\"
expect \"Database name (ambari): \"
send \"ambari\r\"
expect \"Username (ambari): \"
send \"ambari\r\"
expect \"Enter Database Password (bigdata): \"
send \"bigdata\r\"
expect \"*? \"
send \"y\r\"
expect eof
"
sleep 1


#装数据库驱动


printf "\033[29m \n ------>  三秒后为您安装数据库驱动 ,请稍后........   -------> \n\033[0m"


sleep 3


ambari-server setup --jdbc-db=mysql --jdbc-driver=/usr/share/java/mysql-connector-java.jar


sleep 1


#ambari相关操作


printf "\033[29m \n ------>  三秒后为您重启ambari服务,请稍后........   -------> \n\033[0m"


sleep 3


ambari-server restart


sleep 0.5


printf "\033[29m \n ------>  为您重启ambari服务成功........   -------> \n\033[0m"


sleep 0.5


printf "\033[29m \n ------>  三秒后为您安装agent服务,请稍后........   -------> \n\033[0m"
sleep 3
yum install ambari-agent -y


sleep 0.5


sed -i "s/localhost/master.hadoop/" /etc/ambari-agent/conf/ambari-agent.ini
if [[ "0" -eq "$?" ]]
then
ehco "编写成功" 
else
echo "编写失败" >> shibai.log
fi


sleep 0.5
ambari-agent restart






#Slaver1节点操作




printf "\033[29m \n ------>  正在为您传输所需要的准备工作,请稍后........   -------> \n\033[0m"


sleep 1




sleep 0.2


slaver1=`awk 'NR==2{print$1}' ip.txt`
scp -r /usr/jdk64 $slaver1:/usr/
sleep 1
scp /etc/profile $slaver1:/etc/ 
sleep 2
printf "\033[34m \n ------>  正在远程slaver2操作,请稍后........   -------> \n\033[0m"
sleep 3


ssh -t -t $slaver1 -o StrictHostKeyChecking=no  << eeooff


hostnamectl set-hostname slaver1


sleep 0.5


sed -i 's/#   StrictHostKeyChecking ask/StrictHostKeyChecking no/' /etc/ssh/ssh_config


sleep 0.5


sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/ssh_config


sleep 0.5


service sshd restart


rm -rvf /etc/yum.repos.d/CentOS-*


echo done!
source  /etc/profile
sleep 1


yum clean all
yum makecache
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag




yum install ambari-agent -y


sleep 1


sed -i "s/localhost/master.hadoop/" /etc/ambari-agent/conf/ambari-agent.ini
if [[ "0" -eq "$?" ]]
then
ehco "编写成功" 
else
echo "编写失败" > shibai.log
fi


rm -rvf slaver*
sleep 1


ambari-agent restart
sleep 1
exit
eeooff


#Slaver2节点操作


printf "\033[34m \n ------>  正在为您准备下一个节点........   -------> \n\033[0m"
sleep 5






slaver2=`awk 'NR==3{print$1}' ip.txt`
scp -r /usr/jdk64 $slaver2:/usr/
sleep 0.5
scp /etc/profile $slaver2:/etc/
sleep 1


printf "\033[34m \n ------>  正在远程slaver2操作,请稍后........   -------> \n\033[0m"
sleep 3


ssh -t -t $slaver2 -o StrictHostKeyChecking=no << eeooff


hostnamectl  set-hostname slaver2


sed -i 's/#   StrictHostKeyChecking ask/StrictHostKeyChecking no/' /etc/ssh/ssh_config


sleep 0.5


sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/ssh_config


sleep 0.5


service sshd restart
echo done!


rm -rvf /etc/yum.repos.d/CentOS-*
if [[ "0" -eq "$?" ]]
then
echo "删除其他yum源文件成功!"
else
echo "删除其他yum源文件失败!"
sleep 200
fi 






source  /etc/profile
sleep 1


rm -rvf slaver*


echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag




yum install ambari-agent -y
sleep 0.5
sed -i "s/localhost/master.hadoop/" /etc/ambari-agent/conf/ambari-agent.ini
sleep 2 


ambari-agent restart
sleep 2


exit
eeooff
echo done!
sleep 1

你可能感兴趣的:(运维)