Hadoop集群脚本部署

此部分脚本为Hadoop部署脚本,算是刚学完shell编程后拿来练手的小作业,不保证正确可靠,但保证可运行

系统信息及配置

1
2
3
4
5
6
系统版本:CentOS Linux release 7.9.2009 (Core)
内核版本:3.10.0-1160.el7.x86_64
内存:1GB
处理器数量(p):1
每个处理器的内核数量(c):1
硬盘:30GB

脚本用途

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
00.network_conf.sh 
此脚本用于系统网络配置和修改主机名,运行结束后,重启即可生效

01.set_ssh_hosts.sh
此脚本用于设置集群主机间的免密登录,配置 hosts映射

02.ntp_server_install.sh
此脚本用于配置集群的时间同步

03.jdk_install.sh
此脚本用于 jdk 的下载配置

04.zookeeper_install.sh
此脚本用于集群的 zookeeper 部署

05.zkServer-all.sh
此脚本是 zookeeper 的启停脚本,可做启动,停止脚本,以及查看状态

06.hadoop_install.sh
此脚本是 hadoop 的部署脚本

ip.txt
此文件最为重要,其中包含三个字段,$1 是主机的IP,$2 是主机密码,$3 是主机的hostname
字段之间以一个空格隔开
此文件应严格按照要求配置,以上所有脚本不同提供 IP 的检测,故在配置的时候做到仔细认真。

软件包

  • JDK 版本:1.8.0_141

    1
    2
    下载链接:
    http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz
  • zookeeper 版本:zookeeper-3.7.1

    1
    2
    下载链接:
    https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.7.1/apache-zookeeper-3.7.1-bin.tar.gz
  • hadoop 版本:hadoop-3.3.1

    1
    2
    下载链接:
    https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/hadoop-3.3.1/hadoop-3.3.1.tar.gz

00.network_conf.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
########################################################################
# Desc : 网络配置
# Path : /root/cluster
# Auther : Jermyn
# Date : 2022-07-26
# Version : 1.0
########################################################################

NETWORK_DIR=/etc/sysconfig/network-scripts
NETWORK_NIC=`ls -al /sys/class/net/ | grep pci | awk -F '/' '{print $8}'`
read -p "Please Input Server Name Is:" HOSTNAME
read -p "Please Input Server IP Address Is:" IP_ADDRESS
read -p "Please Input Server Netmask Is:" NETMASK_ADDRESS
read -p "Please Input Server Gateway Is:" GATEWAY_ADDRESS
read -p "Please Input Server DNS Address Is:" DNS_ADDRESS
echo "${HOSTNAME}" > /etc/hostname

sed -i "s/BOOTPROTO=dhcp/BOOTPROTO=static/g" ${NETWORK_DIR}/ifcfg-${NETWORK_NIC}
sed -i "s/ONBOOT=no/ONBOOT=yes/g" ${NETWORK_DIR}/ifcfg-${NETWORK_NIC}

cat >>${NETWORK_DIR}/ifcfg-${NETWORK_NIC} <<-EOF
IPADDR=${IP_ADDRESS}
NETMASK=${NETMASK_ADDRESS}
GATEWAY=${GATEWAY_ADDRESS}
DNS1=${DNS_ADDRESS}
EOF
echo "网络配置完成,重启后即可生效"

01.set_ssh_hosts.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
##############################################################
# Desc : 此脚本包括配置集群的ssh免密登录和集群内主机的 hosts 映射写入
# Path : /root
# Auther : Jermyn
# Date : 2022-07-27
# Version : 1.0
##############################################################
yum -y install expect
setSSH(){
/usr/bin/expect <<-END
set timeout 30
spawn ssh-keygen
expect {
"Enter file in which to save the key (/root/.ssh/id_rsa):"
{ send "\r";exp_continue ;exp_continue }
"Enter passphrase (empty for no passphrase):"
{ send "\r";exp_continue }
"Enter same passphrase again: "
{ send "\r" }
}
expect eof
END
}

cpSSH(){
while read ip password hostname
do
/usr/bin/expect <<-END
spawn ssh-copy-id $ip
expect {
"Are you sure you want to continue connecting (yes/no)?"
{ send "yes\r";exp_continue }
"root@"
{ send "${password}\r"}
}
expect eof
END
done < ip.txt
}

setHOSTS(){
while read ip password hostname
do
/usr/bin/expect <<-END
spawn scp -r /root/ip.txt root@${ip}:/root
spawn ssh $ip
expect "#"
send "cat ip.txt | cut -d ' ' -f1,3 | tee -a /etc/hosts\r"
send "exit\r"
expect eof
END
done < ip.txt
}

echo "************************************************************************"
echo "此脚本用于集群中主机的免密登录,以及主机hosts 映射"
echo "运行此脚本前,请注意需要创建名为 ip.txt 的文件用于存放用户信息"
echo "此文件中需注意,字段之间以空格 $1 为用户IP,$2 为主机密码,$3 为主机名"
echo "************************************************************************"
sleep 5
echo
echo "正在运行脚本,请稍后......."

setSSH
cpSSH
setHOSTS

02.ntp_server_install.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
############################################################################
# Desc : 此脚本用于集群中时间同步,同步目标主机是 ip.txt 中的 master
# Path : /root
# Auther : Jermyn
# Date : 2022-07-28
# Version : 1.0
############################################################################
if [ `id -u` -ne 0 ];then
echo "YOU MUST BE ROOT!!!"
exit
fi


ntp_cnf_master(){
yum -y install ntp
cp /etc/ntp.conf /etc/ntp.conf.bak_$(date +%F_%T)
ntp_server_ip=`cat ip.txt | grep 'master' | awk -F ' ' '{print $1}'`
echo "server 127.127.1.0 iburst" >> /etc/ntp.conf
sed -i 's/^server/# & /g' /etc/ntp.conf
sed -r -i '/3.centos/a server 120.25.115.20\nserver 203.107.6.88' /etc/ntp.conf
sed -r -i '/restrict 127.0.0.1/i restrict 120.25.115.20\nrestrict 203.107.6.88' /etc/ntp.conf
echo "restrict ${ntp_server_ip} mask 255.255.255.0 nomodify notrap" >> /etc/ntp.conf

systemctl enable ntpd && systemctl start ntpd
if [ "lsof -i:123 | wc -l" != "0" ];then
echo "NTP Service Is Ok!"
else
echo "NTP Service Is Error!"
fi
}

ntp_cnf_slave(){
while read ip password hostname
do
[ $hostname = master ] && continue
/usr/bin/expect <<-END
set timeout 30
spawn ssh $ip
expect "#"
send "yum -y install ntp\r"
send "sed -r -i '/restrict 127.0.0.1/i restrict master' /etc/ntp.conf\r"
send "sed -i 's/^server/# & /g' /etc/ntp.conf \r"
send "sed -r -i '/3.centos/a server master' /etc/ntp.conf\r"
send "systemctl enable ntpd && systemctl start ntpd \r"
send "exit\r"
expect eof
END
done < ip.txt
}

#########################################分割线#############################################################
echo "********************************************************************"
echo -e "*******************************\033[41;30m请注意\033[0m*******************************"
echo -e "\033[31m1.此脚本需要root身份运行!\033[0m"
echo -e "\033[31m2.此脚本需要运行前需要按照要求配置 ip.txt 文件!\033[0m"
echo -e "\033[31m3.运行此脚本前需要配置好 hosts 映射 和 ssh免密! \033[0m"
echo -e "\033[31m4.默认文件以主机 master 作为集群中时间同步的服务器,其他 slave 以 \033[0m"
echo -e "\033[31mmaster 为同步对象,请在 ip.txt 文件中做出规范!\033[0m"
echo -e "\033[31m5.您有 30 秒的时间思考是否严格按照上方要求配置文件!\033[0m"
echo -e "\033[31m如果没有请执行 Ctrl^c 中断执行此脚本!\033[0m"
echo "********************************************************************"
for i in {30..1}
do
if [ $i -le 5 ];then
echo -e "\033[31m时间还剩 $i 秒\033[0m"
elif [[ $i%5 -eq 0 ]];then
echo "时间还剩 $i 秒"
fi
sleep 1
done


ntp_cnf_master
sleep 2
ntp_cnf_slave

03.jdk_install.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
###############################
# Desc : jdk 部署安装
# Path : /root/cluster
# Auther : Jermyn
# Date : 2022-07-26
# Version : 2.0
###############################
if [ `id -u` -ne 0 ];then
echo "YOU MUST BE ROOT!!!"
exit
fi

java -version &>/dev/null
[ $? -eq 0 ] && echo -e "您已经安装了JDK,执行命令:\033[41;30m java -version \033[0m 查看版本信息"


jdk_dir=/var/local
jdk=jdk-8u141-linux-x64.tar.gz

to_source(){
yum -y install expect &>/dev/null
wget -P ${jdk_dir} --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/${jdk}" &>/dev/null
[ $? -eq 0 ] && echo -e "\033[32m 下载安装包成功 \033[0m" || echo -e "\033[32m 下载失败 \033[0m"
}

install(){
cd ${jdk_dir} && echo -e "\033[33m 正在解压安装包...... \033[0m"
tar -zxvf ${jdk} &>/dev/null
[ $? -eq 0 ] && echo -e "\033[32m 解压成功 \033[0m" || echo -e "\033[32m 解压失败 \033[0m"
echo -e "\033[33m 正在删除压缩包.... \033[0m"
rm -rf ${jdk}
ln -s `ls | grep 'jdk'` jdk
}


conf_master_jdk(){
cat >>/etc/profile <<-EOF
# JAVA_HOME
export JAVA_HOME=${jdk_dir}/jdk
export PATH=\$JAVA_HOME/bin:\$PATH
export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar
EOF
}

send2slave_jdk(){
cd
while read ip password hostname
do
[ $hostname = master ] && continue
scp -r ${jdk_dir}/jdk root@${ip}:${jdk_dir} &>/dev/null
scp -r /etc/profile root@${ip}:/etc/
/usr/bin/expect <<-END
spawn ssh $ip
expect "#"
send "source /etc/profile\r"
send "sleep 2\r"
send "java -version\r"
send ""
send "exit\r"
expect eof
END
done < ip.txt
}


#================================分割线======================================
echo -e "\033[33m 正在为您校验jdk安装包... \033[0m"
if [ -f "${jdk_dir}/${jdk}" ];then
echo -e "\033[32m 校验成功!! \033[0m"
install
else
echo -e "\033[32m 校验失败!未找到安装包,正在下载安装包\033[0m"
to_source
install
conf_master_jdk
source /etc/profile
java -version
[ $? -eq 0 ] && echo "``已经安装了JDK" || echo "jdk 部署失败"
echo
echo -e "\033[32m正在为其他节点部署jdk,请稍等......\033[0m"
send2slave_jdk
fi

echo "================================================="
echo "JDK 安装完成!!!"
echo -e "文件存储在:\033[41;30m ${jdk_dir} \033[0m"
echo -e "执行命令:\033[41;30m source /etc/profile \033[0m 重新加载环境变量"
echo -e "执行命令:\033[41;30m java -version \033[0m 查看版本信息"
echo "================================================="

04.zookeeper_install.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
############################################################################
# Desc : 此脚本用于集群中zookeeper 部署
# Path : /root
# Auther : Jermyn
# Date : 2022-07-28
# Version : 1.0
############################################################################
if [ `id -u` -ne 0 ];then
echo "YOU MUST BE ROOT!!!"
exit
fi

zk_dir=/var/local
zkdatas_dir=/var/local/zookeeper
zk=apache-zookeeper-3.7.1-bin.tar.gz
link_zk="https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.7.1/apache-zookeeper-3.7.1-bin.tar.gz"


to_source(){
wget -P ${zk_dir} --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" ${link_zk}
[ $? -eq 0 ] && echo -e "\033[32m 下载安装包成功 \033[0m" || echo -e "\033[32m 下载失败 \033[0m"
}

install(){
cd ${zk_dir} && echo -e "\033[33m 正在解压安装包...... \033[0m"
tar -zxvf ${zk} &>/dev/null
[ $? -eq 0 ] && echo -e "\033[32m 解压成功 \033[0m" || echo -e "\033[32m 解压失败 \033[0m"
echo -e "\033[33m 正在删除压缩包.... \033[0m"
rm -rf ${zk}
ln -s `ls | grep 'zookeeper'` zookeeper
}

master_conf(){
cd ${zk_dir}/zookeeper/conf
cp zoo_sample.cfg zoo.cfg
mkdir ${zkdatas_dir}/zkdatas
cat >>zoo.cfg<<-EOF
# Zookeeper的数据存放目录
dataDir=${zkdatas_dir}/zkdatas
# 保留多少个快照
autopurge.snapRetainCount=3
# 日志多少小时清理一次
autopurge.purgeInterval=1
# 集群中服务器地址"
EOF

while read ip password hostname
do
server_num=`grep -n -o "${hostname}" /root/ip.txt | awk -F ':' '{print $1}'`
echo "server.${server_num}=${hostname}:2888:3888" >>${zk_dir}/zookeeper/conf/zoo.cfg
done < /root/ip.txt
server_id=$(grep -n -o "`hostname`" /root/ip.txt | awk -F ':' '{print $1}')
echo ${server_id} >> ${zkdatas_dir}/zkdatas/myid
}

env_conf(){
cat >>/etc/profile <<-EOF
# zookeeper 环境变量
export ZOOKEEPER_HOME=${zk_dir}/zookeeper
export PATH=\$PATH:\$ZOOKEEPER_HOME/bin
EOF
source /etc/profile
}

send2slave_zk(){
while read ip password hostname
do
[ $hostname = master ] && continue
echo -e "\033[32m 正在为 ${hostname} 传输 zk包,请稍等.......\033[0m"
scp -r ${zk_dir}/zookeeper root@${ip}:${zk_dir} &>/dev/null
[ $? -eq 0 ] && echo -e "\033[32m 传输成功!!\033[0m" || echo -e "\033[32m传输失败\033[0m"
scp -r /etc/profile root@${ip}:/etc/
/usr/bin/expect <<-END
spawn ssh $ip
expect "#"
send "hostname=`hostname`\r"
send "grep -n -o $hostname /root/ip.txt |cut -d ':' -f1 >${zkdatas_dir}/zkdatas/myid\r"
send "sleep 2\r"
send "exit\r"
expect eof
END
done < /root/ip.txt
}

main(){
echo -e "\033[33m 正在为您校验zookeeper安装包... \033[0m"
if [ -f "${zk_dir}/${zk}" ];then
echo -e "\033[32m 校验成功!! \033[0m"
install
else
echo -e "\033[32m 校验失败!未找到安装包,正在下载安装包\033[0m"
to_source
sleep 2
install
fi
master_conf
env_conf
send2slave_zk
echo "====================================================================================="
echo "集群 zookeeper 部署完成,如需要启动脚本请执行 ${zk_dir}/zookeeper/bin/zkServer.sh 文件 "
echo "如需要一键启动脚本请执行 05.zkServer-all.sh (请注意此脚本需要传递参数 start|stop|status)"
echo "====================================================================================="
}

#########################################分割线#############################################################
echo "********************************************************************"
echo -e "*******************************\033[41;30m请注意\033[0m*******************************"
echo -e "\033[31m1.此脚本需要root身份运行!\033[0m"
echo -e "\033[31m2.此脚本需要运行前需要按照要求配置 ip.txt 文件!\033[0m"
echo -e "\033[31m3.运行此脚本前需要配置好 hosts 映射 和 ssh免密! \033[0m"
echo -e "\033[31m4.默认文件以主机 master 作为脚本的起始机 \033[0m"
echo -e "\033[31m5.您有 30 秒的时间思考是否严格按照上方要求配置文件!\033[0m"
echo -e "\033[31m如果没有请执行 Ctrl^c 中断执行此脚本!\033[0m"
echo "********************************************************************"
for i in {30..1}
do
if [ $i -le 5 ];then
echo -e "\033[31m时间还剩 $i 秒\033[0m"
elif [[ $i%5 -eq 0 ]];then
echo "时间还剩 $i 秒"
fi
sleep 1
done
main

05.zkServer-all.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
#####################################################
# Desc : 此脚本用于集群中zookeeper 一键启停
# Path : /root
# Auther : Jermyn
# Date : 2022-07-28
# Version : 1.0
####################################################
echo "此脚本用于集群中的 zookeeper 的启停及查看状态"
read -p "请输入您执行的操作(start|stop|status):" CHOICE
zk_dir=/var/local
operate(){
while read ip password hostname
do
/usr/bin/expect <<-END
spawn ssh $ip
expect "#"
send "cd ${zk_dir}/zookeeper/bin\r"
send "./zkServer.sh $CHOICE\r"
send "exit\r"
expect eof
END
done < /root/ip.txt
}


if [ $CHOICE = "start" ];then
echo -e "\033[42;34m集群中 zookeeper 正在开启......\033[0m"
operate
elif [ $CHOICE = "stop" ];then
echo -e "\033[42;34m集群中 zookeeper 正在关闭......\033[0m"
operate
elif [ $CHOICE = "status" ];then
echo -e "\033[42;34m查看集群中 zookeeper 运行状态\033[0m"
operate
fi

06.hadoop_install.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/bash
#####################################################
# Desc : 此脚本用于部署 Hadoop 集群
# Path : /root
# Auther : Jermyn
# Date : 2022-07-28
# Version : 1.0
####################################################
if [ `id -u` -ne 0 ];then
echo "YOU MUST BE ROOT!!!"
exit
fi

hadoop_dir=/var/local
hadoop=hadoop-3.3.1.tar.gz
hd_conf_dir=${hadoop_dir}/hadoop/etc/hadoop
hd_data_dir=/var/local/data
snn_host=slave1
web_history_host=master
yarn_lead_role=master
link_hadoop="https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/hadoop-3.3.1/hadoop-3.3.1.tar.gz"

to_source(){
wget -P ${hadoop_dir} --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" ${link_hadoop}
}

install(){
cd ${hadoop_dir} && echo -e "\033[33m 正在解压安装包...... \033[0m"
tar -zxvf ${hadoop} &>/dev/null
[ $? -eq 0 ] && echo -e "\033[32m 解压成功 \033[0m" || echo -e "\033[32m 解压失败 \033[0m"
echo -e "\033[33m 正在删除压缩包.... \033[0m"
rm -rf ${hadoop}
ln -s `ls | grep 'hadoop'` hadoop
}

hadoop_master_cnf(){
cd $hd_conf_dir
cat >>hadoop-env.sh<<-EOF
export JAVA_HOME=${hadoop_dir}/jdk
export HDFS_NAMENODE_USER=root
export HDFS_DATANODE_USER=root
export HDFS_SECONDARYNAMENODE_USER=root
export YARN_RESOURCEMANAGER_USER=root
export YARN_NODEMANAGER_USER=root
EOF

sed -i '/configuration/d' core-site.xml
cat >>core-site.xml<<-EOF
<configuration>
<!-- 设置默认使用的文件系统 Hadoop支持file、HDFS、GFS、ali|Amazon云等文件系统 -->
<property>
<name>fs.defaultFS</name>
<value>hdfs://master:8020</value>
</property>

<!-- 设置Hadoop本地保存数据路径 -->
<property>
<name>hadoop.tmp.dir</name>
<value>${hd_data_dir}/hadoop</value>
</property>

<!-- 设置HDFS web UI用户身份 -->
<property>
<name>hadoop.http.staticuser.user</name>
<value>root</value>
</property>

<!-- 整合hive 用户代理设置 -->
<property>
<name>hadoop.proxyuser.root.hosts</name>
<value>*</value>
</property>

<property>
<name>hadoop.proxyuser.root.groups</name>
<value>*</value>
</property>

<!-- 文件系统垃圾桶保存时间 -->
<property>
<name>fs.trash.interval</name>
<value>1440</value>
</property>
</configuration>
EOF

sed -i '/configuration/d' hdfs-site.xml
cat >>hdfs-site.xml<<-EOF
<configuration>
<!-- 设置SNN进程运行机器位置信息 -->
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>${snn_host}:9868</value>
</property>
</configuration>
EOF

sed -i '/configuration/d' mapred-site.xml
cat >>mapred-site.xml<<-EOF
<configuration>
<!-- 设置MR程序默认运行模式: yarn集群模式 local本地模式 -->
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>

<!-- MR程序历史服务地址 -->
<property>
<name>mapreduce.jobhistory.address</name>
<value>${web_history_host}:10020</value>
</property>

<!-- MR程序历史服务器web端地址 -->
<property>
<name>mapreduce.jobhistory.webapp.address</name>
<value>${web_history_host}:19888</value>
</property>

<property>
<name>yarn.app.mapreduce.am.env</name>
<value>HADOOP_MAPRED_HOME=\${HADOOP_HOME}</value>
</property>

<property>
<name>mapreduce.map.env</name>
<value>HADOOP_MAPRED_HOME=\${HADOOP_HOME}</value>
</property>

<property>
<name>mapreduce.reduce.env</name>
<value>HADOOP_MAPRED_HOME=\${HADOOP_HOME}</value>
</property>
</configuration>
EOF

sed -i '/configuration/d' yarn-site.xml
cat >>yarn-site.xml<<-EOF
<configuration>
<!-- 设置YARN集群主角色运行机器位置 -->
<property>
<name>yarn.resourcemanager.hostname</name>
<value>${yarn_lead_role}</value>
</property>

<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>

<!-- 是否将对容器实施物理内存限制 -->
<property>
<name>yarn.nodemanager.pmem-check-enabled</name>
<value>false</value>
</property>

<!-- 是否将对容器实施虚拟内存限制。 -->
<property>
<name>yarn.nodemanager.vmem-check-enabled</name>
<value>false</value>
</property>

<!-- 开启日志聚集 -->
<property>
<name>yarn.log-aggregation-enable</name>
<value>true</value>
</property>

<!-- 设置yarn历史服务器地址 -->
<property>
<name>yarn.log.server.url</name>
<value>http://${web_history_host}:19888/jobhistory/logs</value>
</property>

<!-- 历史日志保存的时间 7-->
<property>
<name>yarn.log-aggregation.retain-seconds</name>
<value>604800</value>
</property>
</configuration>
EOF

sed -i '/localhost/d' workers
while read ip password hostname
do
echo $hostname >>workers
done < /root/ip.txt
}

env_conf(){
cat >>/etc/profile <<-EOF
# HADOOP环境变量
export HADOOP_HOME=${hadoop_dir}/hadoop
export PATH=\$PATH:\$HADOOP_HOME/bin:\$HADOOP_HOME/sbin
EOF
source /etc/profile
}


send2slave_hd(){
while read ip password hostname
do
[ $hostname = master ] && continue
echo -e "\033[32m 正在为 ${hostname} 传输 hadoop包,请稍等.......\033[0m"
echo -e "\033[32m 请注意此过程所需时间较长请耐心等待\033[0m"
scp -r ${hadoop_dir}/hadoop root@${ip}:${hadoop_dir} &>/dev/null
[ $? -eq 0 ] && echo -e "\033[32m 传输成功!!\033[0m" || echo -e "\033[32m传输失败\033[0m"
scp -r /etc/profile root@${ip}:/etc/
/usr/bin/expect <<-END
spawn ssh $ip
expect "#"
send "source /etc/profile\r"
send "sleep 2\r"
send "exit\r"
expect eof
END
done < /root/ip.txt
}

main(){
echo -e "\033[33m 正在为您校验hadoop安装包... \033[0m"
if [ -f "${hadop_dir}/${hadoop}" ];then
echo -e "\033[32m 校验成功!! \033[0m"
install
hadoop_master_cnf
env_conf
hdfs namenode -format
send2slave_hd
else
echo -e "\033[32m 校验失败!未找到安装包,正在下载安装包\033[0m"
to_source
install
hadoop_master_cnf
env_conf
hdfs namenode -format
send2slave_hd
sleep 2
fi

echo "===================================================================================="
echo -e "\033[31m您已经成功部署hadoop\033[0m"
echo -e "\033[31m开启 hadoop 前请先开启 zookeeper \033[0m"
echo -e "\033[31m请前往${hadoop_dir}/hadoop/sbin \033[0m"
echo -e "\033[31m执行start-all.sh 文件开启 hadoop 集群\033[0m"
echo -e "\033[31m执行 mr-jobhistory-daemon.sh start historyserver文件开启历史服务器\033[0m"
echo -e "\033[31m开启后即可访问 web 页面:http://master:9870/ \033[0m"
echo -e "\033[31m开启后即可访问 web 页面:http://master:8088/ \033[0m"
echo -e "\033[31m开启后即可访问 web 页面:http://master:19888/ \033[0m"
echo -e "\033[31m执行 jps 查看开启的进程\033[0m"
echo "===================================================================================="
}


#==================================分割线=======================================
echo "===================================================================================="
echo -e "*******************************\033[41;30m请注意\033[0m*******************************"
echo -e "\033[31m1.此脚本需要root身份运行!\033[0m"
echo -e "\033[31m2.此脚本需要运行前需要按照要求配置 ip.txt 文件,请先阅读readme!\033[0m"
echo -e "\033[31m3.运行此脚本前需要配置好 hosts 映射 和 ssh免密! \033[0m"
echo -e "\033[31m4.默认文件以主机 master 作为脚本的起始机 \033[0m"
echo -e "\033[31m5.您有 30 秒的时间思考是否严格按照上方要求配置文件!\033[0m"
echo -e "\033[31m如果没有请执行 Ctrl^c 中断执行此脚本!\033[0m"
echo "===================================================================================="
for i in {30..1}
do
if [ $i -le 5 ];then
echo -e "\033[31m时间还剩 $i 秒\033[0m"
elif [[ $i%5 -eq 0 ]];then
echo "时间还剩 $i 秒"
fi
sleep 1
done
clear
main

ip.txt

1
2
3
192.168.88.135 Ccu2021@ master
192.168.88.136 Ccu2021@ slave1
192.168.88.137 Ccu2021@ slave2